创建存储桶的示例 - Amazon Simple Storage Service

本指南不再进行更新。有关当前信息和说明,请参阅新的 Amazon S3 用户指南

创建存储桶的示例

以下代码示例使用适用于 Java、.NET 和 Ruby 的 AWS 开发工具包以编程方式创建存储桶。这些代码示例执行以下任务:

  • 创建存储桶(如果存储桶尚不存在)— 示例将通过执行以下任务来创建存储桶:

    • 通过显式指定 AWS 区域来创建客户端 (示例使用 s3.eu-west-1 区域)。因此,客户端使用 s3.eu-west-1.amazonaws.com 终端节点与 Amazon S3 进行通信。您可以指定任何其他 AWS 区域。有关 AWS 区域的列表,请参阅 AWS 一般参考 中的区域和终端节点

    • 通过仅指定存储桶名称来发送创建存储桶请求。创建存储桶请求未指定其他 AWS 区域。客户端将请求发送到 Amazon S3 以在创建客户端时所指定的区域中创建存储桶。创建存储桶之后,您无法更改其区域。

      注意

      如果在创建存储桶请求中显式指定了与创建客户端时指定的区域不同的 AWS 区域,您可能会收到错误。有关更多信息,请参阅 创建存储桶

      开发工具包库会将 PUT Bucket 请求发送到 Amazon S3 以创建存储桶。有关更多信息,请参阅 PUT Bucket

  • 检索有关存储桶位置的信息 — Amazon S3 将存储桶位置信息存储在与存储桶关联的位置 子资源中。开发工具包库发送 GET Bucket location 请求(请参阅 GET Bucket location)以检索此信息。

使用 Amazon S3 控制台

要使用 Amazon S3 控制台创建存储桶,请参阅《Amazon Simple Storage Service 控制台用户指南》中的如何创建 S3 存储桶?

使用适用于 Java 的 AWS 开发工具包

此示例说明如何使用适用于 Java 的 AWS 开发工具包创建 Amazon S3 存储桶。有关创建和测试有效示例的说明,请参阅测试 Amazon S3 Java 代码示例

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.CreateBucketRequest; import com.amazonaws.services.s3.model.GetBucketLocationRequest; import java.io.IOException; public class CreateBucket2 { public static void main(String[] args) throws IOException { Regions clientRegion = Regions.DEFAULT_REGION; String bucketName = "*** Bucket name ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); if (!s3Client.doesBucketExistV2(bucketName)) { // Because the CreateBucketRequest object doesn't specify a region, the // bucket is created in the region specified in the client. s3Client.createBucket(new CreateBucketRequest(bucketName)); // Verify that the bucket was created by retrieving it and checking its location. String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName)); System.out.println("Bucket location: " + bucketLocation); } } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it and returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }

使用适用于 .NET 的 AWS 开发工具包

有关如何创建和测试有效示例的信息,请参阅 运行 Amazon S3 .NET 代码示例

using Amazon; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Util; using System; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class CreateBucketTest { private const string bucketName = "*** bucket name ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 s3Client; public static void Main() { s3Client = new AmazonS3Client(bucketRegion); CreateBucketAsync().Wait(); } static async Task CreateBucketAsync() { try { if (!(await AmazonS3Util.DoesS3BucketExistAsync(s3Client, bucketName))) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; PutBucketResponse putBucketResponse = await s3Client.PutBucketAsync(putBucketRequest); } // Retrieve the bucket location. string bucketLocation = await FindBucketLocationAsync(s3Client); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } static async Task<string> FindBucketLocationAsync(IAmazonS3 client) { string bucketLocation; var request = new GetBucketLocationRequest() { BucketName = bucketName }; GetBucketLocationResponse response = await client.GetBucketLocationAsync(request); bucketLocation = response.Location.ToString(); return bucketLocation; } } }

使用适用于 Ruby 的 AWS 开发工具包版本 3

有关如何创建和测试有效示例的信息,请参阅 使用适用于 Ruby 的 AWS 开发工具包(版本 3)

require 'aws-sdk-s3' # Creates a bucket in Amazon S3. # # @param s3_client [Aws::S3::Client] An initialized Amazon S3 client. # @param bucket_name [String] The bucket's name. # @return [Boolean] true if the bucket was created; otherwise, false. # @example # s3_client = Aws::S3::Client.new(region: 'us-east-1') # exit 1 unless bucket_created?(s3_client, 'doc-example-bucket') def bucket_created?(s3_client, bucket_name) s3_client.create_bucket(bucket: bucket_name) rescue StandardError => e puts "Error while creating the bucket named '#{bucket_name}': #{e.message}" end

使用其他 AWS 开发工具包

有关使用其他 AWS 开发工具包的信息,请参阅示例代码和库