如何为S3文件指定ContentType ?



我正在尝试将文件转换为gzip文件,然后将它们上传到S3。当我签入S3时,文件在那里,但它们没有指定类型。如何指定内容类型?

for i in testList:
with contextlib.ExitStack() as stack:
source_file = stack.enter_context(open(i , mode="rb"))
destination_file = io.BytesIO()
destination_file_gz = stack.enter_context(gzip.GzipFile(fileobj=destination_file, mode='wb'))
while True:
chunk = source_file.read(1024)
if not chunk:
break
destination_file_gz.write(chunk)
destination_file_gz.close()
destination_file.seek(0)

bucket.upload_fileobj(destination_file, fileName, ContentType='application/gzip')

如果我将ContentType作为参数添加到最后一行,我会得到一个错误:

"errorMessage": "bucket_upload_fileobj() got an unexpected keyword argument 'ContentType'",

使用

bucket.upload_fileobj(destination_file, fileName,ExtraArgs={'ContentType': "application/gzip"})

请参阅使用Boto3在S3中设置AWS内容类型

最新更新