获取有关所有 Amazon S3 存储桶的信息 - 适用于 Ruby 的 Amazon 开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

获取有关所有 Amazon S3 存储桶的信息

以下示例列出了最多 50 个可用 Amazon S3 存储桶的名称,用于Amazon区域。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 require 'aws-sdk-s3' # Lists the Amazon Simple Storage Service (Amazon S3) buckets owned by the # authenticated sender of the request. # # @param s3_client [Aws::S3::Client] An initialized S3 client. # @param max_buckets [Integer] The maximum number of buckets to list. The # number must be between 1 and 50. If not specified, only up to the # first 50 objects will be listed. # @example # list_buckets(Aws::S3::Client.new(region: 'us-east-1'), 25) def list_buckets(s3_client, max_buckets = 50) if max_buckets < 1 || max_buckets > 50 puts 'Maximum number of buckets to request must be between 1 and 50.' return end buckets = s3_client.list_buckets.buckets if buckets.count.zero? puts 'No buckets.' return else if buckets.count > max_buckets puts "First #{max_buckets} buckets:" i = 0 max_buckets.times do puts "#{i + 1}) #{buckets[i].name}" i += 1 end else puts "#{buckets.count} buckets:" i = 0 buckets.count.times do puts "#{i + 1}) #{buckets[i].name}" i += 1 end end end rescue StandardError => e puts "Error listing buckets: #{e.message}" end def run_me region = 'us-east-1' s3_client = Aws::S3::Client.new(region: region) list_buckets(s3_client, 25) end run_me if $PROGRAM_NAME == __FILE__