可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to delete uploaded image files with the AWS-SDK-Core Ruby Gem.
I have the following code:
require 'aws-sdk-core' def pull_picture(picture) Aws.config = { :access_key_id => ENV["AWS_ACCESS_KEY_ID"], :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"], :region => 'us-west-2' } s3 = Aws::S3::Client.new test = s3.get_object( :bucket => ENV["AWS_S3_BUCKET"], :key => picture.image_url.split('/')[-2], ) end
However, I am getting the following error:
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
I know the region is correct because if I change it to us-east-1
, the following error shows up:
The specified key does not exist.
What am I doing wrong here?
回答1:
It seems likely that this bucket was created in a different region, IE not us-west-2. That's the only time I've seen "The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint."
US Standard is us-east-1
回答2:
Check your bucket location in the console, then use this as reference to which endpoint to use: http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
回答3:
I was facing a similar error because the bucket was in region us-west-2
and the URL pattern had bucketname in the path. Once, I changed the URL pattern to have bucketname as URL subdomain to grab the files and it worked.
For eg previous URL was
https://s3.amazonaws.com/bucketname/filePath/filename
Then I replaced it as
https://bucketname.s3.amazonaws.com/filePath/filename
回答4:
For many S3 API packages (I recently had this problem the npm s3 package) you can run into issues where the region is assumed to be US Standard, and lookup by name will require you to explicitly define the region if you choose to host a bucket outside of that region.
回答5:
For ppl who are still facing this issue, try adding s3_host as follows to the config hash
:storage => :s3, :s3_credentials => {:access_key_id => access key, :secret_access_key => secret access key}, :bucket => bucket name here, :s3_host_name => s3-us-west-1.amazonaws.com or whatever comes as per your region}.
This fixed the issue for me.
回答6:
During the creation of S3Client you can specify the endpoint mapping to a particular region. If default of s3.amazonaws.com
then bucket will be created in us-east-1
which is North Virginia.
More details on S3 endpoints and regions in AWS docs: http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region.
So, always make sure about the endpoint/region while creating the S3Client and access S3 resouces using the same client in the same region.
If the bucket is created from AWS S3 Console, then check the region from the console for that bucket then create a S3 Client in that region using the endpoint details mentioned in the above link.