如何更新一批 S3 对象?使用 ruby 的元数据?
我需要更改 S3 上数百或数千个对象的一些元数据(内容类型)。用红宝石来做到这一点的好方法是什么?据我所知,无法仅使用 fog.io 保存元数据,必须重新保存整个对象。似乎使用 官方 sdk 库 需要我为此滚动一个包装器环境一项任务。
I need to change some metadata (Content-Type) on hundreds or thousands of objects on S3. What's a good way to do this with ruby? As far as I can tell there is no way to save only metadata with fog.io, the entire object must be re-saved. Seems like using the official sdk library would require me rolling a wrapper environment just for this one task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你是对的,官方的 SDK 允许你修改对象元数据,而无需再次上传。它的作用是复制对象,但它位于服务器上,因此您无需下载文件并重新上传。
包装器很容易实现,比如
You're right, the official SDK lets you modify the object metadata without uploading it again. What it does is copy the object but that's on the server so you don't need to download the file and re-upload it.
A wrapper would be easy to implement, something like
在 v2 API 中,您可以将
Object#copy_from()
或Object.copy_to()
与:metadata
和:metadata_directive 结合使用=> “REPLACE”选项可更新对象的元数据,而无需从 S3 下载元数据。
Joost 的要点 中的代码抛出此错误:
这是因为默认情况下,AWS 会忽略复制操作提供的
:metadata
,因为它会复制元数据。我们必须设置:metadata_directive =>;如果我们想就地更新元数据,请使用“REPLACE”选项。
请参阅 http://docs.aws.amazon。 com/sdkforruby/api/Aws/S3/Object.html#copy_from-instance_method
这是我最近用来执行元数据更新的完整、有效的代码片段操作:
为了方便重复使用:
In the v2 API, you can use
Object#copy_from()
orObject.copy_to()
with the:metadata
and:metadata_directive => 'REPLACE'
options to update an object's metadata without downloading it from S3.The code in Joost's gist throws this error:
This is because by default AWS ignores the
:metadata
supplied with a copy operation because it copies metadata. We must set the:metadata_directive => 'REPLACE'
option if we want to update the metadata in-place.See http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html#copy_from-instance_method
Here's a full, working code snippet that I recently used to perform metadata update operations:
For easy re-use:
对于未来的读者,这里有一个使用 Ruby aws-sdk v1 更改内容的完整示例(另请参阅此 Gist 对于 aws-sdk v2 示例):
For future readers, here's a complete sample of changing stuff using the Ruby aws-sdk v1 (also see this Gist for a aws-sdk v2 sample):
经过一番搜索后,这似乎对我有用
after some search this seems to work for me
使用 sdk 更改内容类型将产生
x-amz-meta-
前缀。我的解决方案是使用 ruby + aws cli。这将直接写入content-type
而不是x-amz-meta-content-type
。Using the sdk to change the content type will result in
x-amz-meta-
prefix. My solution was to use ruby + aws cli. This will directly write to thecontent-type
instead ofx-amz-meta-content-type
.该 API 似乎现已可用:
This API appears to be available now: