-
Notifications
You must be signed in to change notification settings - Fork 9
/
S3CopyToChina-MPU.py
145 lines (126 loc) · 4.75 KB
/
S3CopyToChina-MPU.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import boto3
import os
import time
import json
from urllib.parse import unquote_plus
s3client = boto3.client('s3')
ddb = boto3.client('dynamodb')
table_parts = 'S3MPU'
table_result = 'S3MPUResult'
credbucket = os.environ['CredBucket']
credobject = os.environ['CredObject']
def lambda_handler(event, context):
bucket = event['bucket']
key = unquote_plus(event['key'])
dst_bucket = event['dst_bucket']
uploadid = event['uploadid']
part = event['part']
range= event['range']
tmp_file = '/tmp/'+ key
# Store s3 parts information in DDB.
start_time = time.time()
ddb.put_item(TableName=table_parts,
Item={
'uploadid':{'S':str(uploadid)},
'part':{'N':str(part)},
'range':{'S':str(range[6:])},
'source_bucket':{'S':str(bucket)},
'source_key':{'S':str(key)},
'destination_bucket':{'S':str(dst_bucket)},
'start_time':{'N':str(start_time)},
'part_complete':{'S':'N'}
})
# Get China credential from global bucket.
response = s3client.get_object(Bucket=credbucket, Key=credobject)
ak = response['Body']._raw_stream.readline().decode("UTF8").strip('\r\n')
sk = response['Body']._raw_stream.readline().decode("UTF8")
s3CNclient = boto3.client('s3', region_name='cn-north-1',
aws_access_key_id=ak,
aws_secret_access_key=sk)
# Download s3 part by range. Upload part to China S3 bucket.
response = s3client.get_object(Bucket=bucket, Key=key, Range=range)
part_content = s3CNclient.upload_part(
Body=response["Body"].read(), Bucket=dst_bucket, Key=key, UploadId=uploadid, PartNumber=int(part))
etag = eval(part_content['ETag'])
if os.path.exists(tmp_file):
os.remove(tmp_file)
print('Complete upload part to China S3 bucket:'+dst_bucket+'/'+key+' part #:'+str(part)+' Upload id: '+uploadid)
# update table 'parts'
finish_time = time.time()
ddb.update_item(TableName=table_parts,
Key={
"uploadid": {"S": uploadid},
"part": {"N": part}
},
UpdateExpression="set part_complete = :complete, finish_time = :finish_time, etag = :etag",
ExpressionAttributeValues={
":complete": {"S": "Y"},
":finish_time": {'N':str(finish_time)},
":etag": {'S':str(etag)}
},
ReturnValues="UPDATED_NEW"
)
# Calculate completed s3 parts count.
response = ddb.query(TableName=table_parts,
KeyConditionExpression="uploadid = :id",
ProjectionExpression='part',
FilterExpression ="part_complete = :part_complete",
ExpressionAttributeValues={
":id": {"S": uploadid},
":part_complete": {"S": 'Y'}
}
)
part_count = response['Count']
response = ddb.update_item(TableName=table_result,
Key={
"uploadid": {"S": uploadid}
},
UpdateExpression="set part_count = :count",
ConditionExpression="complete = :c",
ExpressionAttributeValues={
":count": {"N": str(part_count)},
":c": {"S": "N"}
},
ReturnValues="ALL_NEW"
)
part_qty = response['Attributes']['part_qty']['N']
#If count equals parts quantity, initial S3 complete MPU.
if str(part_count) == str(part_qty) :
parts = []
j = 0
response = ddb.query(
TableName=table_parts,
KeyConditionExpression="uploadid = :id",
ExpressionAttributeValues={
":id": {"S": uploadid}
}
)
items = response['Items']
for i in items:
parts.append({"PartNumber": int(items[j]["part"]['N']), "ETag": items[j]["etag"]['S']})
j += 1
s3CNclient.complete_multipart_upload(Bucket=dst_bucket, Key=key, UploadId=uploadid, MultipartUpload={"Parts": parts})
#Record task status.
ddb.update_item(TableName=table_result,
Key={
"uploadid": {"S": uploadid}
},
UpdateExpression="set complete = :complete, complete_time = :ctime",
ExpressionAttributeValues={
":complete": {"S": "Y"},
":ctime": {"N": str(time.time())}
},
ReturnValues="UPDATED_NEW"
)
# Delete temp ddb items.
k = 1
while k <= int(part_qty):
ddb.delete_item(
TableName=table_parts,
Key={
"uploadid": {"S": uploadid},
"part": {"N": str(k)}
}
)
k += 1
print("Successfully copied whole S3 file "+key+" to China bucket "+dst_bucket)