简体   繁体   中英

AWS::S3::Errors::AccessDenied: Access Denied when trying to do copy_to

I have written a rake task that does an copy_to from one directory in a bucket to another directory within the same bucket. When I test it locally it works fine, but when I deploy it to an environment it returns AWS::S3::Errors::AccessDenied: Access Denied. I assume that it has something to do with the AWS credentials on the environment I am deploying too, I am also confident that the problem is to do with the copy_to as I accessed the bucket from the rails console and had no issues

my copy from statement is as follows

   creds = YAML::load_file(Rails.root.join("config", "s3.yml"))

   AWS.config(aws_access_key_id: creds[:access_key_id],
               aws_secret_access_key: creds[:secret_access_key])

s3.buckets['test-bucket'].objects['path to file'].copy_to('new_path')

The parameters to AWS.config are access_key_id and secret_access_key , without the aws_ prefix.

http://docs.aws.amazon.com/AWSRubySDK/latest/AWS.html#config-class_method

Found this because I also received Access Denied when calling copy_to(). While older SDK versions were happy to accept a pure key path as parameter to copy_to, newer versions require you to specify the bucket, too.

In my case

s3_bucket.object(old_key).copy_to(new_key)

did not work and produced a rather unhelpful "Access Denied" error with the v3 SDK. Instead, this works:

s3_bucket.object(old_key).copy_to( s3_bucket.object(new_key) )

or

s3_bucket.object(old_key).copy_to(bucket_name+'/'+new_key)
   s3.buckets['bucket-name'].objects['source-key'].copy_to('target-key', :bucket_name => 'target-bucket')

A simplified example using the aws-sdk gem:

 AWS.config(:access_key_id => '...', :secret_access_key => '...')
 s3 = AWS::S3.new
 s3.buckets['bucket-name'].objects['source-key'].copy_to('target-key')

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