关于阿里云oss获取sts凭证 app直传 python的实例

  • 时间:
  • 浏览:17

起首装置需求的模块

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

获得需求的5个参数

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('LTAIcWKeZ0k***xx', 'Kax2Hx1Yf5cqi5nwmg4q5GSNE***xx', REGINID)

  req = AssumeRoleRequest.AssumeRoleRequest()

  req.set_RoleArn('acs:ram::11605935******xx: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

前往的参数

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

django 利用的views.py

import getsts

def test(request):

  req = getsts.getStsToken()

  print req
  return HttpResponse(req)

安卓或苹果的app取得那5个参数就能够间接背oss上传图片了

以上那篇闭于阿里云oss获得sts凭据 app曲传 python的真例便是小编分享给各人的全数内容了,期望能给各人1个参考,也期望各人多多撑持剧本之家。