如何为 S3 文件指定 ContentType?

how to specify ContentType for S3 files?

我正在尝试将文件转换为 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"})