Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

创建存储桶的示例

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

  • 存储桶不存在时创建存储桶 – 示例按如下所示创建存储桶:

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

    • 通过仅指定存储桶名称来发送创建存储桶请求。创建存储桶请求不指定其他 AWS 区域;因此,客户端向 Amazon S3 发送请求以在创建客户端时指定的区域中创建存储桶。

      注意

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

      开发工具包库向 Amazon S3 发送 PUT 存储桶请求 (请参阅 PUT Bucket) 以创建存储桶。

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

使用 Amazon S3 控制台

有关使用 Amazon S3 创建控制台的信息,请参阅如何创建 S3 存储桶? (在 Amazon Simple Storage Service 控制台用户指南 中)。

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

有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

import java.io.IOException; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.CreateBucketRequest; import com.amazonaws.services.s3.model.GetBucketLocationRequest; public class CreateBucket { private static String bucketName = "*** bucket name ***"; public static void main(String[] args) throws IOException { AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); s3client.setRegion(Region.getRegion(Regions.US_WEST_1)); try { if(!(s3client.doesBucketExist(bucketName))) { // Note that CreateBucketRequest does not specify region. So bucket is // created in the region specified in the client. s3client.createBucket(new CreateBucketRequest( bucketName)); } // Get location. String bucketLocation = s3client.getBucketLocation(new GetBucketLocationRequest(bucketName)); System.out.println("bucket location = " + bucketLocation); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } }

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

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

using System; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Util; namespace s3.amazon.com.docsamples { class CreateBucket { static string bucketName = "*** bucket name ***"; public static void Main(string[] args) { using (var client = new AmazonS3Client(Amazon.RegionEndpoint.EUWest1)) { if (!(AmazonS3Util.DoesS3BucketExist(client, bucketName))) { CreateABucket(client); } // Retrieve bucket location. string bucketLocation = FindBucketLocation(client); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static string FindBucketLocation(IAmazonS3 client) { string bucketLocation; GetBucketLocationRequest request = new GetBucketLocationRequest() { BucketName = bucketName }; GetBucketLocationResponse response = client.GetBucketLocation(request); bucketLocation = response.Location.ToString(); return bucketLocation; } static void CreateABucket(IAmazonS3 client) { try { PutBucketRequest putRequest1 = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; PutBucketResponse response1 = client.PutBucket(putRequest1); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "For service sign up go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when writing an object" , amazonS3Exception.Message); } } } } }

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

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

require 'aws-sdk' s3 = Aws::S3::Client.new(region: 'us-west-1') s3.create_bucket(bucket: 'bucket-name')

使用其他 AWS 开发工具包

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