How to add or change the Content-Disposition of an existing object in Amazon S3?

We have hundreds of objects in the AWS S3 bucket that don't have a content set.

I am using Ruby aws-sdk gem.

How do you add or reorder content for these objects WITHOUT reloading files again?

I tried

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

as well as copy_to (), move_to (), but none of them seem to work when adding content locations to objects. In some cases, the objects do not seem to have been altered at all (the modification time has not changed), in other cases the object file is corrupted!


I know of an alternative to using: response_content_disposition when requesting an s3 object via HTTP, which sets the Content-Disposition header,

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

Thanks!

0
2

, aws-sdk.

s3/s3_object.rb

copy_from() ( , : content_type )

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

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

, , , :

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

, cli. /mybucket/brand_img/ios/, - . , "@", URL-.

aws s3 ls s3://mybucket/brand_img/ios/|awk {'print $4'} > objects.txt

while read line; do aws s3api copy-object --bucket mybucket  \
--copy-source /mybucket/brand_img/ios/$line --key brand_img/ios/$line \
--metadata-directive REPLACE --metadata Content-Disposition=$line --acl public-read; done < objects.txt
0

All Articles