|
|
@@ -0,0 +1,118 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+
|
|
|
+from aliyunsdkcore import client
|
|
|
+from aliyunsdkcore.request import CommonRequest
|
|
|
+import json
|
|
|
+import oss2
|
|
|
+import pytz
|
|
|
+import datetime
|
|
|
+import time
|
|
|
+
|
|
|
+class OssClient:
|
|
|
+ endpoint = 'https://oss-cn-nanjing.aliyuncs.com'
|
|
|
+ area = 'cn-nanjing'
|
|
|
+ # 填写步骤1创建的RAM用户AccessKey。
|
|
|
+ access_key_id = 'LTAI5tHhtNM66nQwRKwyYotL'
|
|
|
+ access_key_secret = 'fTznJXTLMfySeMFRWWnghAaPAtaceV'
|
|
|
+ # role_Arn填写步骤3获取的角色ARN,例如acs:ram::175708322470****:role/ramtest。
|
|
|
+ role_arn = 'acs:ram::1807432849970905:role/monitorossadmin'
|
|
|
+ bucket_name = 'monitor-file-bucket'
|
|
|
+
|
|
|
+ sts_access_key_id = None
|
|
|
+ sts_access_key_secret = None
|
|
|
+ security_token = None
|
|
|
+ expiration = None
|
|
|
+
|
|
|
+
|
|
|
+ def getSTSToken(self):
|
|
|
+ # yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
|
|
|
+
|
|
|
+ # 创建权限策略。
|
|
|
+ # policy_text = ''
|
|
|
+
|
|
|
+ clt = client.AcsClient(self.access_key_id, self.access_key_secret, self.area)
|
|
|
+ request = CommonRequest(product="Sts", version='2015-04-01', action_name='AssumeRole')
|
|
|
+ request.set_method('POST')
|
|
|
+ request.set_protocol_type('https')
|
|
|
+ request.add_query_param('RoleArn', self.role_arn)
|
|
|
+ # 指定自定义角色会话名称,用来区分不同的令牌,例如填写为sessiontest。
|
|
|
+ request.add_query_param('RoleSessionName', 'sessiontest')
|
|
|
+ # 指定临时访问凭证有效时间单位为秒,最小值为900,最大值为3600。
|
|
|
+ request.add_query_param('DurationSeconds', '3000')
|
|
|
+ # 如果policy为空,则RAM用户默认获得该角色下所有权限。如果有特殊权限控制要求,请参考上述policy_text设置。
|
|
|
+ # request.add_query_param('Policy', policy_text)
|
|
|
+ request.set_accept_format('JSON')
|
|
|
+
|
|
|
+ body = clt.do_action_with_exception(request)
|
|
|
+
|
|
|
+ # 使用RAM用户的AccessKey ID和AccessKey Secret向STS申请临时访问凭证。
|
|
|
+ token = json.loads(oss2.to_unicode(body))
|
|
|
+ # 打印STS返回的临时访问密钥(AccessKey ID和AccessKey Secret)、安全令牌(SecurityToken)以及临时访问凭证过期时间(Expiration)。
|
|
|
+ self.sts_access_key_id = token['Credentials']['AccessKeyId']
|
|
|
+ self.sts_access_key_secret = token['Credentials']['AccessKeySecret']
|
|
|
+ self.security_token = token['Credentials']['SecurityToken']
|
|
|
+ self.expiration = self.utc_to_time(token['Credentials']['Expiration'])
|
|
|
+
|
|
|
+ return {
|
|
|
+ 'sts_access_key_id': self.sts_access_key_id,
|
|
|
+ 'sts_access_key_secret': self.sts_access_key_secret,
|
|
|
+ 'security_token': self.security_token,
|
|
|
+ 'expiration': self.expiration
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ def getBucket(self):
|
|
|
+ if self.expiration == None:
|
|
|
+ self.getSTSToken()
|
|
|
+ elif self.expiration <= datetime.datetime.now():
|
|
|
+ self.getSTSToken()
|
|
|
+ # yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
|
|
|
+ endpoint = self.endpoint
|
|
|
+ # 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
|
|
|
+ sts_access_key_id = self.sts_access_key_id
|
|
|
+ sts_access_key_secret = self.sts_access_key_secret
|
|
|
+ # 填写Bucket名称。
|
|
|
+ bucket_name = self.bucket_name
|
|
|
+ # 从STS服务获取的安全令牌(SecurityToken)。
|
|
|
+ security_token = self.security_token
|
|
|
+
|
|
|
+
|
|
|
+ # 使用临时访问凭证中的认证信息初始化StsAuth实例。
|
|
|
+ auth = oss2.StsAuth(sts_access_key_id,
|
|
|
+ sts_access_key_secret,
|
|
|
+ security_token)
|
|
|
+
|
|
|
+ # 使用StsAuth实例初始化存储空间。
|
|
|
+ bucket = oss2.Bucket(auth, endpoint, bucket_name)
|
|
|
+ return bucket
|
|
|
+
|
|
|
+ def downloadFileStream(self, object_path):
|
|
|
+ bucket = self.getBucket()
|
|
|
+ object_stream = bucket.get_object(object_path)
|
|
|
+ return object_stream
|
|
|
+
|
|
|
+ def downloadFileToLocal(self, object_path, file_path):
|
|
|
+ bucket = self.getBucket()
|
|
|
+ bucket.get_object_to_file(object_path, file_path)
|
|
|
+
|
|
|
+ def uploadObj(self, object_path, file_obj):
|
|
|
+ bucket = self.getBucket()
|
|
|
+ # 上传Object。
|
|
|
+ result = bucket.put_object(object_path, file_obj)
|
|
|
+ return result.status
|
|
|
+
|
|
|
+ def uploadFile(self, object_path, file_path):
|
|
|
+ bucket = self.getBucket()
|
|
|
+ # 上传File。
|
|
|
+ result = bucket.put_object_from_file(object_path, file_path)
|
|
|
+ return result.status
|
|
|
+
|
|
|
+ def utc_to_time(self,utc_time_str, utc_format='%Y-%m-%dT%H:%M:%SZ'):
|
|
|
+ return datetime.datetime.strptime(utc_time_str, utc_format) #把世界时间的格式转化为datetime.
|
|
|
+
|
|
|
+if __name__=='__main__':
|
|
|
+ ossclient = OssClient()
|
|
|
+ # ossclient.uploadObj('development/test.txt', 'lalala')
|
|
|
+ # ossclient.downloadFileToLocal('development/test.txt', './test.txt')
|
|
|
+ ossclient.uploadFile('development/test1.png', './img_2_big_small.png')
|
|
|
+ ossclient.uploadFile('development/test2.png', './img_2_small_big.png')
|