使用 boto,在 s3 上已存在的文件上设置 content_type

发布于 2025-01-04 14:32:59 字数 701 浏览 3 评论 0 原文

我正在使用 django 存储和 s3boto 后端。根据此问题, http://code.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth- by 我有一堆文件(全部),其内容类型为“application/octet-stream”。鉴于我有一个 实例,如何设置 content_type?

In [29]: a.file.file.key.content_type
Out[29]: 'application/octet-stream'

In [30]: mimetypes.guess_type(a.file.file.key.name)[0]
Out[30]: 'image/jpeg'

In [31]: type(a.file.file.key)
Out[31]: <class 'boto.s3.key.Key'>

I'm using django storages with the s3boto backend. As per this issue, http://code.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by I have a bunch of files (all of them) that have content type 'application/octet-stream'. Given that I have an instance of <class 'boto.s3.key.Key'>, how can I set the content_type?

In [29]: a.file.file.key.content_type
Out[29]: 'application/octet-stream'

In [30]: mimetypes.guess_type(a.file.file.key.name)[0]
Out[30]: 'image/jpeg'

In [31]: type(a.file.file.key)
Out[31]: <class 'boto.s3.key.Key'>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2025-01-11 14:32:59

创建文件后,无法修改与该文件关联的内容类型(或任何其他元数据)。但是,您可以在服务器端复制文件并在此过程中修改元数据。以下是 github 上的要点,应该会有所帮助:

https://gist.github.com/1791086

内容:

import boto

s3 = boto.connect_s3()
bucket = s3.lookup('mybucket')
key = bucket.lookup('mykey')

# Copy the key onto itself, preserving the ACL but changing the content-type
key.copy(key.bucket, key.name, preserve_acl=True,
    metadata={'Content-Type': 'text/plain'})

key = bucket.lookup('mykey')
print key.content_type

米奇

There is no way to modify the content type (or any other metadata) associated with a file after it has been created. You can, however, copy the file on the server side and modify the metadata in the process. Here is a gist on github that should help:

https://gist.github.com/1791086

Contents:

import boto

s3 = boto.connect_s3()
bucket = s3.lookup('mybucket')
key = bucket.lookup('mykey')

# Copy the key onto itself, preserving the ACL but changing the content-type
key.copy(key.bucket, key.name, preserve_acl=True,
    metadata={'Content-Type': 'text/plain'})

key = bucket.lookup('mykey')
print key.content_type

Mitch

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文