You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
5.5 KiB
141 lines
5.5 KiB
6 years ago
|
import hashlib
|
||
|
import json
|
||
|
import os
|
||
|
import time
|
||
|
|
||
|
import aliyunsdkcore
|
||
|
import oss2 as oss2
|
||
|
from aliyunsdkcloudphoto.request.v20170711 import ListPhotoStoresRequest, FetchLibrariesRequest, \
|
||
|
CreateTransactionRequest, CreatePhotoRequest, EditPhotosRequest, GetPublicAccessUrlsRequest, ListPhotosRequest, \
|
||
|
DeletePhotosRequest, InactivatePhotosRequest, GetLibraryRequest
|
||
|
from aliyunsdkcore.auth.credentials import RamRoleArnCredential
|
||
|
from aliyunsdkcore.client import AcsClient
|
||
|
from aliyunsdkcore.profile import region_provider
|
||
|
from aliyunsdkcore.request import CommonRequest
|
||
|
|
||
|
region_id = 'cn-shanghai'
|
||
|
ak = 'LTAIWzPnmkJs2qpL'
|
||
|
secret = 'LIIq3HumctXPp0WT8c06yDiFbKKiVe'
|
||
|
region_provider.add_endpoint('Oss', region_id, 'oss-cn-shanghai.aliyuncs.com')
|
||
|
region_provider.add_endpoint('CloudPhoto', region_id, 'cloudphoto.cn-shanghai.aliyuncs.com')
|
||
|
aliyunsdkcore.request.set_default_protocol_type("https")
|
||
|
|
||
|
|
||
|
class MyClient(AcsClient):
|
||
|
|
||
|
def __init__(self, arn, sessionName):
|
||
|
super().__init__(region_id=region_id, credential=RamRoleArnCredential(ak, secret, arn, sessionName))
|
||
|
self.StoreName = None
|
||
|
|
||
|
def get_md5_01(self, file_path):
|
||
|
md5 = None
|
||
|
if os.path.isfile(file_path):
|
||
|
f = open(file_path, 'rb')
|
||
|
md5_obj = hashlib.md5()
|
||
|
md5_obj.update(f.read())
|
||
|
hash_code = md5_obj.hexdigest()
|
||
|
f.close()
|
||
|
md5 = str(hash_code).lower()
|
||
|
return md5
|
||
|
|
||
|
def do_action_with_exception(self, acs_request):
|
||
|
return json.loads(super().do_action_with_exception(acs_request).decode())
|
||
|
|
||
|
def showOption(self, options, key, desc):
|
||
|
choose = []
|
||
|
for index, option in enumerate(options):
|
||
|
choose.append(option[key])
|
||
|
print('{index}:{name}'.format(index=index + 1, name=option[key]))
|
||
|
return choose[(int)(input('输入{desc}\n'.format(desc=desc))) - 1]
|
||
|
|
||
|
def listPhotoStores(self):
|
||
|
request = ListPhotoStoresRequest.ListPhotoStoresRequest()
|
||
|
response = self.do_action_with_exception(request)
|
||
|
print('PhotoStores:')
|
||
|
photoStores = response['PhotoStores']
|
||
|
self.StoreName = self.showOption(photoStores, 'Name', 'StoreName')
|
||
|
|
||
|
def listLibraries(self):
|
||
|
request = FetchLibrariesRequest.FetchLibrariesRequest()
|
||
|
request.set_StoreName(self.StoreName)
|
||
|
response = self.do_action_with_exception(request)
|
||
|
self.Libraries = response['Libraries']
|
||
|
|
||
|
def uploadPhoto(self):
|
||
|
if self.StoreName is None:
|
||
|
self.listPhotoStores()
|
||
|
request = CreateTransactionRequest.CreateTransactionRequest()
|
||
|
filePath = input('输入上传文件路径\n').replace('\\', '/')
|
||
|
fileName = filePath.split('/')[-1]
|
||
|
request.set_Size(os.path.getsize(filePath))
|
||
|
request.set_Ext(fileName[-fileName[::-1].index('.'):])
|
||
|
request.set_Md5(self.get_md5_01(filePath))
|
||
|
request.set_StoreName(self.StoreName)
|
||
|
response = self.do_action_with_exception(request)
|
||
|
print(response)
|
||
|
Upload = response['Transaction']['Upload']
|
||
|
FileId = Upload['FileId']
|
||
|
SessionId = Upload['SessionId']
|
||
|
Bucket = Upload['Bucket']
|
||
|
OssEndpoint = Upload['OssEndpoint']
|
||
|
ObjectKey = Upload['ObjectKey']
|
||
|
|
||
|
auth = oss2.StsAuth(self._signer._session_credential[0], self._signer._session_credential[1],
|
||
|
self._signer._session_credential[2])
|
||
|
bucket = oss2.Bucket(auth, OssEndpoint, Bucket)
|
||
|
with open(filePath, 'rb') as fileobj:
|
||
|
result = bucket.put_object(ObjectKey, fileobj)
|
||
|
print('文件上传状态{status}'.format(status=result.status))
|
||
|
|
||
|
request = CreatePhotoRequest.CreatePhotoRequest()
|
||
|
request.set_FileId(FileId)
|
||
|
request.set_PhotoTitle(fileName)
|
||
|
request.set_SessionId(SessionId)
|
||
|
request.set_StoreName(self.StoreName)
|
||
|
request.set_UploadType('manual')
|
||
|
response = self.do_action_with_exception(request)
|
||
|
print(response)
|
||
|
|
||
|
def listPhotos(self):
|
||
|
if self.StoreName == None:
|
||
|
self.listPhotoStores()
|
||
|
request = ListPhotosRequest.ListPhotosRequest()
|
||
|
request.set_StoreName(self.StoreName)
|
||
|
request.set_State('all')
|
||
|
response = self.do_action_with_exception(request)
|
||
|
print(response)
|
||
|
return response['Photos']
|
||
|
|
||
|
def getPhoto(self):
|
||
|
return self.showOption(self.listPhotos(), 'IdStr', '照片Id')
|
||
|
|
||
|
def sharePhoto(self):
|
||
|
IdStr = self.getPhoto()
|
||
|
request = EditPhotosRequest.EditPhotosRequest()
|
||
|
request.set_PhotoIds([IdStr])
|
||
|
request.set_StoreName(self.StoreName)
|
||
|
request.set_ShareExpireTime((int(round(time.time())) + 60 * 60) * 1000)
|
||
|
response = self.do_action_with_exception(request)
|
||
|
print(response)
|
||
|
|
||
|
request = GetPublicAccessUrlsRequest.GetPublicAccessUrlsRequest()
|
||
|
request.set_DomainType('OSS')
|
||
|
request.set_PhotoIds([IdStr])
|
||
|
request.set_StoreName(self.StoreName)
|
||
|
request.set_ZoomType('style/2')
|
||
|
response = self.do_action_with_exception(request)
|
||
|
print(response)
|
||
|
|
||
|
|
||
|
def client(arn, sessionName):
|
||
|
ram_role_arn_credential = RamRoleArnCredential('LTAIWzPnmkJs2qpL', 'LIIq3HumctXPp0WT8c06yDiFbKKiVe',
|
||
|
arn, sessionName)
|
||
|
return AcsClient(region_id='cn-shanghai', credential=ram_role_arn_credential)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
|
||
|
myClient = MyClient('acs:ram::1098806312754985:role/aliyunosstokengeneratorrole', 'pqh001')
|
||
|
myClient.listPhotoStores()
|
||
|
myClient.sharePhoto()
|