I wanted to copy a file from one folder to another in the same bucket using aws-sdk gem (version 3). I was able to copy the file successfully.
The code as follows Controller:
saved_file = path for the source file
source_bucket_name = 'bucket-name'
target_bucket_name = 'same as source bucket'
source_key = "#{saved_file.path.path}"
target_key = "path of the target"
s3 = Aws::S3::Client.new(credentials)
byebug
obj = s3.copy_object({bucket: target_bucket_name, copy_source: source_bucket_name + '/' + source_key, key: target_key})
ps: Don't bother about the source_key and path, since i've used carrierwave for that. How can i get the path/url of the copied object. I need the url because if i delete file in source bucket after copying, the destination has to have the copy independently. and that file can be accessed through the url. This is the output of obj in command line
, expiration=nil, copy_source_version_id=nil, version_id=nil, server_side_encryption="AES256", sse_customer_algorithm=nil, sse_customer_key_md5=nil, ssekms_key_id=nil, request_charged=nil>
Any help would be great and TIA.
UPDATE:
aws_client = Aws::S3::Client.new('aws credentials')
s3 = Aws::S3::Client.new('aws credentials')
s3.copy_object({bucket: target_bucket_name, copy_source: source_bucket_name + '/'
+ source_key, key: target_key}, options = {acl: :public_read})
s3 = Aws::S3::Resource.new(client: aws_client)
bucket = s3.bucket('bucket-name')
obj = bucket.object("#{target_key}")
url = obj.presigned_url(:get)
used that url to view/access the copied object. This worked for me.