溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

關(guān)于阿里云oss獲取sts憑證 app直傳 python的實例

發(fā)布時間:2020-09-30 12:18:27 來源:腳本之家 閱讀:516 作者:hongzhangzhao 欄目:開發(fā)技術(shù)

首先安裝需要的模塊

pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-sts

獲取需要的5個參數(shù)

getsts.py

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdksts.request.v20150401 import AssumeRoleRequest
import json


def getStsToken():

  REGINID = 'cn-beijing'
  ENDPOINT = 'sts.cn-beijing.aliyuncs.com'
  region_provider.add_endpoint('Sts', REGINID, ENDPOINT)

  clt = client.AcsClient('LTAIcWKeZ0kxxxxx', 'Kax2Hx1Yf5cqi5nwmg4q5GSNExxxxx', REGINID)

  req = AssumeRoleRequest.AssumeRoleRequest()

  req.set_RoleArn('acs:ram::11605935xxxxxxxx:role/aliyunosstokengeneratorrole')

  req.set_RoleSessionName('external-username')
  #req.set_DurationSeconds(Integer) # past time
  #req.set_Policy('')

  resp = clt.do_action_with_exception(req)
  #print resp
  print type(resp)
  resp = json.loads(resp)
  print type(resp)              
  token = dict(status='200', AccessKeyId=resp['Credentials']['AccessKeyId'], AccessKeySecret=resp['Credentials']['AccessKeySecret'], SecurityToken=resp['Credentials']['SecurityToken'], Expiration=resp['Credentials']['Expiration'])

  tokenString = json.dumps(token)
  #print tokenString

  return tokenString

返回的參數(shù)

{
  "AccessKeySecret": "4fSzVwox9v74qEjACfeWLH7CRrYkWG5z92ASJYrJMVRB", 
  "status": "200", 
  "SecurityToken": "CAISiwJ1q6Ft5B2yfSjIpKDUffTcn5xE3Y6OZFzF1nIMduQdvazc0Tz2IHpPenhuAu4bsPsxlG5Q7PkYlrh+W4NIX0rNaY5t9ZlN9wqkbtJdcTlALvhW5qe+EE2/VjTZvqaLEcibIfrZfvCyESOm8gZ43br9cxi7QlWhKufnoJV7b9MRLGLaBHg8c7UwHAZ5r9IAPnb8LOukNgWQ4lDdF011oAFx+wgdgOadupTFtEKD3QankLRO+tqtesGeApMybMslYbCcx/drc6fN6ilU5iVR+b1+5K4+om2c4IDDWAcMvUrYY7SMqII1NnxwYqkrBqhDt+Pgkv51vOPekYntwgpKJ/tSVynP3Q0ITXhd94kagAE7VUD9kf6ZSxi1buTiuoe28f2MAQANEQKP6wrgAKkwinaftUABZolATmUJiHOQZ8pfPgNvwlPrmyDvKe8iT1/uFTnE17UM4VGAgmBry3IGA+gM5IFSe8bQ13tJ6DJzlpuXXcsFxU6oN1zLcr6gKd3UZGGttGMgE9JTC+i7Hoa9Zg==", 
  "Expiration": "2018-04-11T07:41:49Z", 
  "AccessKeyId": "STS.Eca6NhrCejLefzt2rYzk1Riw3"
}

django 應(yīng)用的views.py

import getsts

def test(request):

  req = getsts.getStsToken()

  print req
  return HttpResponse(req)

安卓或者蘋果的app獲得這5個參數(shù)就可以直接向oss上傳圖片了

以上這篇關(guān)于阿里云oss獲取sts憑證 app直傳 python的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI