Getting Information about All Amazon S3 Buckets in a Region - AWS SDK for Ruby

Getting Information about All Amazon S3 Buckets in a Region

The following example lists the names of the first 50 buckets for the us-west-2 region. If you don’t specify a limit, Amazon S3 lists all buckets in us-west-2.

# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. 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
Note

If a bucket is not in the region in which you instantiated your Resource object, the SDK emits a warning message when you call get_bucket_location. You can suppress this message by redirecting STDERR.

On Windows, append 2> nul to the command.

On Linux or iOS, append 2> /dev/null to the command.