#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved. @AUTHOR: ASJRD018 @NAME: AnsjerFormal @software: PyCharm @DATE: 2018/11/8 9:23 @Version: python3.6 @MODIFY DECORD:ansjer dev @file: oss_sts.py @Contact: chanjunkai@163.com """ # -*- coding: utf-8 -*- ''' 生成sts上传授权 ''' from aliyunsdkcore import client from aliyunsdksts.request.v20150401 import AssumeRoleRequest import json import oss2 import base64 ''' http://test.dvema.com/cloudVod/getSts?uidToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjaGFubmVsIjoiNCIsInVpZCI6IkZUU0xMOEhNNDM3WjM4V1UxMTFBIn0.wkrwYvIYf5qEukOSTxALSAgSqop-gNBdEvSwScOgYB8 ''' tokenss = {"AccessKeyId": "STS.NHni76oniM27eB6mLEmw67Kr8", "AccessKeySecret": "F6mNGwMyz8cMbhHJm26QiPxrZAtu2AC3yXBbkAXa5PJh", "SecurityToken": "CAISxwJ1q6Ft5B2yfSjIr4vbIo2CgrFI+vDcZ2SHiUwQYfga2I7Z2jz2IHFIf3NhAe0bv/kzm2lX7/YYlr1dd6RhYRzkSJcrsaYMoTycOtOYkpQAX3LuHNn3d1KIAjvXgeUtCoeQFaEkE5XAQlTAkTAJkNmeXD6+XlujHISUgJp8FLo+VRW5ajw0TbUzIRB5+vcHKVzbN/umLnyShXHLXmZlvgdghER166m03re5iHzkgUb91/UeqqnoP5GgdLMMBppkVMqv1+EEL8iG2SVLuR9R7/U03u4W9jLZud3ERHdt2DGiY8rxroYwHlQpWZVjQvdt6+b7maQi/a7x792mk08WYbsKAn6DGN78mZf+dbn3ZopjR9HSIG/Q192DDJ7xvj4/bGgTXAEwIIt9cSUhUUFxG2yGdfb4oQCQeGm4V7OCy7omdxHoSpAoJbEagAEmSR8slfOl+iU3tlgyeBuH6aywtWa3kIjqTFBl0Jdko4mCbbbxgpMjRIftC+5vDAELOOhZm1epHDtF6aeQ4MDR0NHzLApvmn2i3pUMmI5azPxgTh24LuemCEWpPQjUPLFytvtzbXiJdAl5HpvYWLtuVwjEyMZDDy4YIom5cTLUAA==", "Expiration": "2018-12-18T07:48:33Z", "expire": "3600", "endpoint": "oss-eu-central-1.aliyuncs.com", "bucket_name": "gmvod1", "arn": "acs:ram::1901342792446414:role/stsoss/FTSLL8HM437Z38WU111A", "code": 0, "storage": "FTSLL8HM437Z38WU111A/vod12/", "ip": "192.168.136.40"} # 使用RAM账号的AccessKeyId和AccessKeySecret向STS申请临时token。 # 使用临时token中的认证信息初始化StsAuth实例。 auth = oss2.StsAuth(tokenss['AccessKeyId'], tokenss['AccessKeySecret'], tokenss['SecurityToken']) print(auth) ############### callback callback_dict = {} callback_dict['callbackUrl'] = 'http://www.dvema.com:3000/?token=token&filename=shijianchuo&size=1234&len=1234' # 设置回调请求消息头中Host的值, 如oss-cn-hangzhou.aliyuncs.com。 callback_dict['callbackHost'] = 'oss-cn-shenzhen.aliyuncs.com' # 设置发起回调时请求body的值。 callback_dict['callbackBody'] = 'filename=${object}&size=${size}&mimeType=${mimeType}' # 设置发起回调请求的Content-Type。 callback_dict['callbackBodyType'] = 'application/x-www-form-urlencoded' # 回调参数是Json格式,并且需要Base64编码。 callback_param = json.dumps(callback_dict).strip() base64_callback_body = oss2.utils.b64encode_as_string(callback_param) # 回调参数编码后放在Header中发送给OSS。 headers = {'x-oss-callback': base64_callback_body} ################ # exit() # 使用StsAuth实例初始化存储空间。 endpoint = tokenss['endpoint'] bucket = oss2.Bucket(auth, tokenss['endpoint'], tokenss['bucket_name']) # # 上传一个字符串。 # bucket.delete_object('a-3.txt') # exit() patht=tokenss['storage']+'ggg.1234' print(patht) print(endpoint) bucket.put_object(patht, b'gg') # bucket.put_object('FTSLL8HM437Z38WU111A/vod4/1234.txt', b'12345646',headers) exit() res = bucket.get_object('1234.txt') print(res.read())