简体   繁体   中英

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?

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)

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM