如何添加或修改在Amazon S3中现有对象的内容处置?对象、内容、Amazon

2023-09-11 10:12:04 作者:执念 °

我们有数百个对象在AWS S3存储桶不具备的内容配置集。

We have hundreds of object in an AWS S3 bucket which don't have content disposition set.

我使用Ruby的AWS-SDK的宝石。

I'm using Ruby aws-sdk gem.

你如何添加或更改内容处置这些对象,而无需再次重新上传文件?

How do you add or change content disposition to these objects, WITHOUT re-uploading the files again?

我已经试过

obj.write(:content_disposition => 'attachment')
obj.copy_from(obj.key, :content_disposition => 'attachment')

和也copy_to(),MOVE_TO(),但没有这些似乎在加入内容部署到对象一起工作。在少数情况下,似乎该对象不已在所有修改(修改时间没有改变),在其他情况下,该对象文件损坏!

and also copy_to(), move_to(), but none of these seem to work in adding the content disposition to the objects. In a few cases, the objects don't seem to have been modified at all (the modification time didn't change), in other cases, the object file is corrupted!

我知道使用的替代品:response_content_disposition 要求通过HTTP的S3对象,设置Content-Disposition头的时候,

I'm aware of the alternative of using :response_content_disposition when requesting the s3 object via HTTP, which sets the Content-Disposition header,

obj.url_for(:read, :response_content_disposition => "attachment")

感谢您!

推荐答案

我们找到了解决方案,通过改变AWS-SDK源$ C ​​$ C。

We found the solution by changing the aws-sdk source code.

在S3 / s3_object.rb

In s3/s3_object.rb

添加以下行copy_from()(类似于如何:CONTENT_TYPE已被处理)

add the following lines to copy_from() (similar to how :content_type was handled)

if options[:content_disposition]
    copy_opts[:content_disposition] = options[:content_disposition]
    copy_opts[:metadata_directive] = "REPLACE"
end

此外,在S3 / client.rb

also, in s3/client.rb

标记中添加以下

object_method(:copy_object, :put,
                    :header_options => {
                    :copy_source => 'x-amz-copy-source',
                    :cache_control => 'Cache-Control',
                    :metadata_directive => 'x-amz-metadata-directive',
                    :storage_class => 'x-amz-storage-class',
                    :server_side_encryption => 'x-amz-server-side-encryption',
                    :content_type => 'Content-Type',
                    :content_disposition => 'Content-Disposition', # add this line here
                }) do

一旦你完成了上面的,你可以做以下内容配置添加到现有的对象:

Once you've done the above, you can do the following to add content disposition to your existing object:

obj.copy_from(obj.key, :content_disposition => 'attachment', :content_type => 'image/png', :acl => :public_read)