Reputation: 535
I am using AWS Ruby SDK v2 to get S3 Bucket names. Is there a way to get S3 buckets from a specific region?
The way I do it now:
buckets = s3_client.list_buckets.buckets.map(&:name)
buckets.select { |name| s3_client.get_bucket_location(:bucket => name).location_constraint == @region }
This gets all the buckets I have in buckets
. Then I filter ones that are from region I specified with @region
.
I would like to be able to do this with making only one request through AWS SDK. Is this possible somehow?
Upvotes: 3
Views: 2714
Reputation: 259
Well I solved this with the PHP SDK using client->getBucketLocation(). With the multiRegion client you can just use the 'LocationConstraint', with the standard single region client I can handle this by catching the Exception for non-defined Regions.
That said, with the RubySDK (which I'm less familiar with) Amazon suggests you use this (as an example):
require 'aws-sdk-s3' # v2: require 'aws-sdk'
region = 'us-west-2'
s3 = Aws::S3::Resource.new(region: region)
s3.buckets.limit(50).each do |b|
if s3.client.get_bucket_location(bucket: b.name).location_constraint == region
puts "#{b.name}"
end
end
More information can be found here: https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/s3-example-get-buckets-in-region.html#aws-ruby-sdk-s3-example-get-buckets-in-region
Upvotes: 1