【问题标题】:boto.s3: copy() on a key object loses 'Content-Type' metadataboto.s3:关键对象上的 copy() 丢失“内容类型”元数据
【发布时间】:2012-02-26 12:48:27
【问题描述】:

这里是一些复制 S3 密钥的示例代码。您可能想要这样做的原因有很多,其中之一是更新关键元数据。虽然这似乎是被广泛接受的解决方案,但存在一个大问题。问题是当我执行下面的示例时,我实际上丢失了我的 Content-Type,它默认为“application/octet-stream”(如果尝试提供网络图像,则不是很有用)。

# Get bucket
conn = S3Connection(self._aws_key, self._aws_secret)
bucket = conn.get_bucket(self._aws_bucket)

# Create key
k = Key(bucket)
k.key = key

# Copy old key
k.metadata.update({ meta_key: meta_value })
k2 = k.copy(k.bucket.name, k.name, k.metadata, preserve_acl=True)
k = k2

有什么想法吗?谢谢。

【问题讨论】:

  • copy 只返回对象的浅拷贝。尝试使用deepcopy
  • @Joel:boto 不提供 deepcopy。

标签: python amazon-s3 content-type boto


【解决方案1】:

看看this的帖子

你需要做一个

key = bucket.get_key(key.name)

然后:

metadata['Content-Type'] = key.content_type will work

否则,key.content_type 将返回 application/octet-stream

【讨论】:

    【解决方案2】:

    以下GitHub Gist 为我工作:

    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
    

    虽然跑了很长时间!

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多