当块多于一个时,似乎无法使用aiobotocore进行多部分上传。
只有一个块时有效。以下是我编写的使用aiobotocore实现分段上传的代码。
aiobotocore 0.9.4
boto3 1.9.0
botocore 1.10.58
async def s3_upload(app_config, key, encrypted_filebytes):
session = aiobotocore.get_session()
async with session.create_client('s3', region_name=app_config['STORAGE']['S3']["AWS_REGION_NAME"],
aws_secret_access_key=app_config['STORAGE']["AWS_SECRET_KEY"],
aws_access_key_id=app_config['STORAGE']["AWS_ACCESS_KEY"]) as client:
response = await client.create_multipart_upload(Bucket=app_config['STORAGE']['S3']["BUCKET_NAME"], Key=key)
upload_id = response['UploadId']
output = BytesIO()
source_size = output.write(encrypted_file_bytes)
bytes_per_chunk = 1*1024
chunks_count = int(math.ceil(source_size / float(bytes_per_chunk)))
for i in range(chunks_count):
offset = i * bytes_per_chunk
remaining_bytes = source_size - offset
_bytes = min([bytes_per_chunk, remaining_bytes])
part_num = i + 1
print ("uploading part " + str(part_num) + " of " + str(chunks_count))
print(offset)
output.seek(offset)
resp = await client.upload_part(
Body=output,
Bucket=app_config['STORAGE']['S3']["BUCKET_NAME"],
ContentLength=_bytes,
Key=key,
PartNumber=part_num,
UploadId=upload_id
)
print(resp)
await client.complete_multipart_upload(
Bucket=app_config['STORAGE']['S3']["BUCKET_NAME"],
Key=key,
MultipartUpload={
'Parts': [
{
'ETag': resp['ETag'],
'PartNumber': part_num
},
]
},
UploadId=upload_id
)
print("upload_file completed")
output.close()
以下是我收到的错误消息:
ClientError: An error occurred (SignatureDoesNotMatch) when calling the UploadPart operation: The request signature we calculated does not match the signature you provided. Check your key and signing method.