【问题标题】:Using boto, set content_type on files which are already present on s3使用 boto,在 s3 上已经存在的文件上设置 content_type
【发布时间】:2012-03-03 02:44:16
【问题描述】:

我正在使用带有 s3boto 后端的 django 存储。根据这个问题,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”。鉴于我有一个<class 'boto.s3.key.Key'> 的实例,我该如何设置 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'>

【问题讨论】:

    标签: python django amazon-s3 boto


    【解决方案1】:

    在创建文件后,无法修改与文件关联的内容类型(或任何其他元数据)。但是,您可以在服务器端复制文件并在此过程中修改元数据。这是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
    

    米奇

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      相关资源
      最近更新 更多