Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a file inside an S3 bucket (ruby)

I'm using gem aws-sdk-ruby and I want to copy a file /images/image_a.png to /profile.png all resides inside the same bucket.

How can I do that?

like image 483
kambi Avatar asked Apr 21 '13 13:04

kambi


People also ask

How do I copy data from one S3 bucket to another S3 bucket?

Create and attach an S3 bucket policy. Sign in to the AWS Management Console for your source account and open the Amazon S3 console. Choose your source S3 bucket and then choose Permissions. Under Bucket policy, choose Edit and then paste the bucket policy from the sourcebucket-policy.

Why can't I copy an object between two Amazon S3 buckets?

If the object that you can't copy between buckets is owned by another account, then the object owner can do one of the following: The object owner can grant the bucket owner full control of the object. After the bucket owner owns the object, the bucket policy applies to the object.

Does S3 copy overwrite?

If you send the file to the existing key, it will overwrite that file once the upload is complete.


2 Answers

Extracted from the documentation, you'll have to use the copy_to. For instance:

s3 = AWS::S3.new

# Upload a file and set server-side encryption.
bucket1 = s3.buckets[source_bucket]
bucket2 = s3.buckets[target_bucket]
obj1 = bucket1.objects[source_key]
obj2 = bucket2.objects[target_key]

obj1.copy_to(obj2)
like image 141
fmendez Avatar answered Sep 26 '22 06:09

fmendez


to do this with aws-sdk-v2 you can do the following:

bucket = Aws::S3::Bucket.new('my_aws_bucket')
bucket.object("new_photo_uploads/logos/my_new_copied_photo.png")
aws_response = object.copy_from(bucket.object("my_original_photo.png"))
like image 25
harrisjb Avatar answered Sep 23 '22 06:09

harrisjb