[ruby-on-rails] AWS S3: The bucket you are attempting to access must be addressed using the specified endpoint

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?

This question is related to ruby-on-rails ruby amazon-web-services amazon-s3

The answer is


I live in uk was keep on trying for 'us-west-2'region. So redirected to 'eu-west-2'. The correct region for S3 is 'eu-west-2'


After a long search, I found a working solution. The issue was because of the wrong region-code.

below is the list of region-codes, set the appropriate one and your issue will be solved.

Code                         Name
US East (Ohio)               us-east-2

US East (N. Virginia)       us-east-1

US West (N. California)     us-west-1

US West (Oregon)            us-west-2

Asia Pacific (Hong Kong)    ap-east-1

Asia Pacific (Mumbai)       ap-south-1

Asia Pacific (Osaka-Local)  ap-northeast-3

Asia Pacific (Seoul)        ap-northeast-2

Asia Pacific (Singapore)    ap-southeast-1

Asia Pacific (Sydney)       ap-southeast-2

Asia Pacific (Tokyo)        ap-northeast-1

Canada (Central)            ca-central-1

Europe (Frankfurt)          eu-central-1

Europe (Ireland)            eu-west-1

Europe (London)             eu-west-2

Europe (Paris)             eu-west-3

Europe (Stockholm)         eu-north-1

Middle East (Bahrain)      me-south-1

South America (São Paulo)   sa-east-1

You can find your region-code on click of bucket name right corner.

enter image description here

For mode details Click


I had same error. It occurred when s3 client was created with different endpoint than the one which was set up while creating bucket.

  • ERROR CODE - The bucket was set up with EAST Region.

s3Client = New AmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY, RegionEndpoint.USWest2)

  • FIX

s3Client = New AmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY, RegionEndpoint.USEast1)


I Have faced the same issue.After a lot of struggle I found that the real issue is with the com.amazonaws dependencies.After adding dependencies this error got disappeared.


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.


In my case, I selected wrong RegionEndpoint. After selecting the correct RegionEndpoint, it started working :)


None of the above answers fixed my issue.

The above answers are probably more likely the cause of your problem but my issue was that I was using the wrong bucket name. It was a valid bucket name, it just wasn't my bucket.

The bucket I was pointing to was in a different region that my lambda function so check your bucket name!


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

I got this error when I tried to access a bucket that didn't exist.

I mistakenly switched a path variable with the bucket name variable and so the bucket name had the file path value. So maybe double-check, if the bucket name that you set on your request is correct.


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.


This occurred for me when I had a source ip constraint on the policy being used by the user (access key / secret key) to create the s3 bucket. My IP was accurate--but for some reason it wouldn't work and gave this error.


I encountered this issue when using a different AWS profile. I saw the error when I was using an account with admin permissions, so the possibility of permissions issues seemed unlikely.

It's really a pet peeve of mine that AWS is so prone to issuing error messages that have such little correlation with the required actions, from a user perspective.


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.


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


Examples related to ruby-on-rails

Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Where do I put a single filter that filters methods in two controllers in Rails Empty brackets '[]' appearing when using .where How to integrate Dart into a Rails app Rails 2.3.4 Persisting Model on Validation Failure How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Rails: Can't verify CSRF token authenticity when making a POST request Uncaught ReferenceError: React is not defined

Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to amazon-web-services

How to specify credentials when connecting to boto3 S3? Is there a way to list all resources in AWS Access denied; you need (at least one of) the SUPER privilege(s) for this operation Job for mysqld.service failed See "systemctl status mysqld.service" What is difference between Lightsail and EC2? AWS S3 CLI - Could not connect to the endpoint URL boto3 client NoRegionError: You must specify a region error only sometimes How to write a file or data to an S3 object using boto3 Missing Authentication Token while accessing API Gateway? The AWS Access Key Id does not exist in our records

Examples related to amazon-s3

How to specify credentials when connecting to boto3 S3? AWS S3 CLI - Could not connect to the endpoint URL How to write a file or data to an S3 object using boto3 The AWS Access Key Id does not exist in our records AccessDenied for ListObjects for S3 bucket when permissions are s3:* Save Dataframe to csv directly to s3 Python Listing files in a specific "folder" of a AWS S3 bucket How to get response from S3 getObject in Node.js? Getting Access Denied when calling the PutObject operation with bucket-level permission Read file content from S3 bucket with boto3