首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为S3文件指定ContentType?

如何为S3文件指定ContentType?
EN

Stack Overflow用户
提问于 2021-10-20 21:52:29
回答 1查看 56关注 0票数 1

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

代码语言:javascript
复制
    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作为参数添加到最后一行,我会得到一个错误:

代码语言:javascript
复制
"errorMessage": "bucket_upload_fileobj() got an unexpected keyword argument 'ContentType'",
EN

Stack Overflow用户

回答已采纳

发布于 2021-10-20 23:16:12

使用

代码语言:javascript
复制
bucket.upload_fileobj(destination_file, fileName,ExtraArgs={'ContentType': "application/gzip"})

请参阅AWS Content Type Settings in S3 Using Boto3

票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69653457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档