#!/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 tokenss = {"AccessKeyId": "STS.NK9QEdKycTqd6JDyprKzbrcLL", "AccessKeySecret": "HXB8Aug13472E8YrvVPC7jSfgAz42dwqHbBZQEQWEWNx", "SecurityToken": "CAIS0wJ1q6Ft5B2yfSjIr4iMGv/QpqZC47OPNGz1nXAnR/VOnabnrjz2IHFIf3NhAe0bv/kzm2lX7/YYlqN4S5ZDR1HCbsJxtmOEMNYwJ9ivgde8yJBZoljMewHKedGSvqL7Z+H+U6mMGJOEYEzFkSle2KbzcS7YMXWuLZyOj+wuDLEQRRLqVSdaI91UKwB+yrcdLmCDGfupPwLnpWDSAUF0wFce71ly8qOi2MaRxwPDhVnhsI8vqp/2P4KvYrsdXuh2WMzn2/dtJOiTknxb7x9X+LlxgPUDu02D3LC8G3Bd7w+LKPGultRkN11CYbUgEKNltIqF95oh4LSVzd+ojU8QYqcMA3j9PNnxmJKfRLn2Z4xlKeinYij3v4rRZsWvgWQNemkGMQ5GQd0lJ0JrBAYkIjOgcf77owqbOlr+GvXZj/hpiMov1Tvz4cGNPEOf5T4EVOPY4vQagAG0T92WVVVpbT+yvK+aV0f1E46g6p/f7/JlQn0v3coLRbl4XfjTWH9NlUNTnYTljAsBRKPaYMZWJQGGSlACgE8pN+AtyZDsyKX8nCvs/utaMxVwcI1fq9GZLlw1MLhOJK7mkpGqDduoPy/tC1w6EGUZtLng/PwUNu68CXvu8rz/BA==", "Expiration": "2018-12-10T10:29:43Z", "expire": "3600", "endpoint": "oss-cn-shenzhen.aliyuncs.com", "bucket_name": "cloudvod1", "arn": "acs:ram::1901342792446414:role/stsoss/chanjunkai"} # 使用RAM账号的AccessKeyId和AccessKeySecret向STS申请临时token。 # 使用临时token中的认证信息初始化StsAuth实例。 auth = oss2.StsAuth(tokenss['AccessKeyId'], tokenss['AccessKeySecret'], tokenss['SecurityToken']) print(auth) '''''' 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实例初始化存储空间。 bucket = oss2.Bucket(auth, tokenss['endpoint'], tokenss['bucket_name']) # # 上传一个字符串。 # bucket.delete_object('a-3.txt') # exit() bucket.put_object('123456.txt', b'241234124',headers) exit() res = bucket.get_object('1234.txt') print(res.read())