I’m encountering a MalformedXML error when calling the CompleteMultipartUpload operation with AWS S3. Here’s a summary of the situation:
I am trying to upload a large file to S3 using multipart upload. The file is larger than 1GB, so I am splitting it into parts and uploading each part separately. After uploading all parts, I call complete_multipart_upload to finalize the upload. However, I’m receiving the following error:
“An error occurred (MalformedXML) when calling the CompleteMultipartUpload operation: The XML you provided was not well-formed or did not validate against our published schema”
I try this,
def save_uploaded_file_data(self, data): # data - file object
try:
file_extension = Path(data.filename).suffix[1:]
content = data.read()
file_size = len(content)
file_data = FileData(name=data.filename, size=file_size, extension=file_extension)
s3_client = boto3.client(
service_name='s3',
region_name=Config.AWS_REGION,
aws_access_key_id=Config.AWS_ACCESS_KEY_ID,
aws_secret_access_key=Config.AWS_SECRET_ACCESS_KEY
)
bucket_name = 'uploaderbuck'
key = data.filename
if file_size >= 1 * 1024 * 1024 * 1024:
part_size = 500 * 1024 * 1024
response = s3_client.create_multipart_upload(Bucket=bucket_name, Key=key, StorageClass='DEEP_ARCHIVE')
upload_id = response['UploadId']
parts = []
part_number = 1
with data.stream as f:
while True:
part_data = f.read(part_size)
if not part_data:
break
part_response = s3_client.upload_part(
Bucket=bucket_name,
Key=key,
PartNumber=part_number,
UploadId=upload_id,
Body=part_data
)
parts.append({'PartNumber': part_number, 'ETag': part_response['ETag']})
print(f"Uploaded part {part_number}")
part_number += 1
s3_client.complete_multipart_upload(
Bucket=bucket_name,
Key=key,
UploadId=upload_id,
MultipartUpload={'Parts': parts}
)
else:
s3_client.upload_fileobj(data, bucket_name, key, ExtraArgs={'StorageClass': 'DEEP_ARCHIVE'})
file_data.save()
return {}, 200
except Exception as e:
return jsonify(str(e)), 500
Aghvan Aslanyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.