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

作者:hongzhangzhao 时间:2021-04-29 14:16:11 

首先安装需要的模块


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('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

返回的参数


{
 "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 应用的views.py


import getsts

def test(request):

req = getsts.getStsToken()

print req
 return HttpResponse(req)

安卓或者苹果的app获得这5个参数就可以直接向oss上传图片了

来源:https://blog.csdn.net/hongzhangzhao/article/details/79897518

标签:阿里云oss,sts,app直传,python
0
投稿

猜你喜欢

  • 部署ASP.NET Core程序到Windows系统

    2024-05-09 09:04:38
  • Python正则表达式教程之二:捕获篇

    2023-08-24 00:27:39
  • CI框架出现mysql数据库连接资源无法释放的解决方法

    2023-11-15 07:13:35
  • 微信小程序学习笔记之表单提交与PHP后台数据交互处理图文详解

    2023-11-22 19:29:44
  • 浅析Vue自定义组件的v-model

    2024-04-29 13:10:16
  • HTML编辑器FCKeditor使用详解

    2010-02-28 12:30:00
  • Django集成百度富文本编辑器uEditor攻略

    2021-01-11 21:43:21
  • Go如何实现HTTP请求限流示例

    2023-07-21 00:40:27
  • mysql和oracle默认排序的方法 - 不指定order by

    2024-01-27 17:44:28
  • 基于Python实现微博抓取GUI程序

    2021-06-01 10:59:29
  • 什么是DOM(Document Object Model)文档对象模型

    2024-05-13 09:37:04
  • Python设计模式编程中Adapter适配器模式的使用实例

    2023-11-16 10:02:15
  • Http与https对比详细介绍

    2022-09-10 05:01:38
  • 浅析mysql 共享表空间与独享表空间以及他们之间的转化

    2024-01-28 04:20:53
  • 如何用electron把vue项目打包为桌面应用exe文件

    2024-06-05 10:03:16
  • 配置SQL Server 2000选项

    2010-04-25 11:01:00
  • Python自定义线程池实现方法分析

    2021-12-17 09:13:10
  • CentOS7中使用shell脚本安装python3.8环境(推荐)

    2022-08-24 17:04:24
  • 聊聊Python pandas 中loc函数的使用,及跟iloc的区别说明

    2023-08-11 09:11:36
  • python实现银行账户系统

    2023-05-27 17:49:08
  • asp之家 网络编程 m.aspxhome.com