简体   繁体   中英

copy_to in s3 using ruby

Am trying to copy an image from bucket to another bucket in s3.

     AWS.config(
          :access_key_id => 'Bucket one key', 
          :secret_access_key => 'bucket one secret key'
     )
     s3 = AWS::S3.new

     bucket1 = s3.buckets["Bucket_one"]
     bucket2 = s3.buckets["Bucket_two"]
     obj1 = bucket1.objects["source_key"]
     obj2 = bucket2.objects["destination_key"]

     obj1.copy_to(obj2)

Can you guide me as to how to retrieve the source key of a file already uploaded in S3? I have the destination_key,bucket_one and bucket_two.

Image on S3 is not a single file but a number of files names of which starts from the name of your image. Thus you need to know the name of your image. Once you get it, the could should look like:

bucket1 = s3.buckets["Bucket_one"]
bucket2 = s3.buckets["Bucket_two"]
bucket1.objects.with_prefix(image_name).each do |source_object|
  source_object.copy_to(bucket2.objects[source_object.key])
end

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