Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Amazon S3 Transfer Acceleration 示例

本节提供了有关如何对存储桶启用 Amazon S3 Transfer Acceleration 和对启用的存储桶使用加速终端节点的示例。一些 AWS 开发工具包支持的语言 (例如,Java 和 .NET) 使用加速终端节点客户端配置标记,这样一来,您便无需显式将 Transfer Acceleration 的终端节点设置为 bucketname.s3-accelerate.amazonaws.com。有关 Transfer Acceleration 的更多信息,请参阅 Amazon S3 Transfer Acceleration

使用 Amazon S3 控制台

有关使用 Amazon S3 控制台对存储桶启用 Transfer Acceleration 的信息,请参阅 Amazon Simple Storage Service 控制台用户指南 中的启用 Transfer Acceleration

从 AWS Command Line Interface (AWS CLI) 使用 Transfer Acceleration

本节提供了用于 Transfer Acceleration 的 AWS CLI 命令的示例。有关设置 AWS CLI 的说明,请参阅设置 AWS CLI

使用 AWS CLI 对存储桶启用 Transfer Acceleration

使用 AWS CLI put-bucket-accelerate-configuration 命令对存储桶启用或暂停 Transfer Acceleration。以下示例设置 Status=Enabled 以对存储桶启用 Transfer Acceleration。可使用 Status=Suspended 暂停 Transfer Acceleration。

Copy
$ aws s3api put-bucket-accelerate-configuration --bucket bucketname --accelerate-configuration Status=Enabled

从 AWS CLI 使用 Transfer Acceleration

在 AWS Config 文件的配置文件中将配置值 use_accelerate_endpoint 设置为 true 会将通过 s3 和 s3api AWS CLI 命令发起的所有 Amazon S3 请求定向到加速终端节点:s3-accelerate.amazonaws.com。必须对存储桶启用 Transfer Acceleration 才能使用加速终端节点。

使用存储桶寻址的虚拟样式发送所有请求:my-bucket.s3-accelerate.amazonaws.com。不会将任何 ListBucketsCreateBucketDeleteBucket 请求发送到加速终端节点,因为此终端节点不支持这些操作。有关 use_accelerate_endpoint 的更多信息,请参阅 AWS CLI S3 配置

以下示例在默认配置文件中将 use_accelerate_endpoint 设置为 true

Copy
$ aws configure set default.s3.use_accelerate_endpoint true

如果您需要对某些 AWS CLI 命令使用加速终端节点,但不对其他此类命令使用加速终端节点,则可使用以下两种方法中的任一方法:

  • 您可以通过将任何 s3 或 s3api 命令的 --endpoint-url 参数设置为 https://s3-accelerate.amazonaws.comhttp://s3-accelerate.amazonaws.com 来对每条命令使用加速终端节点。

  • 可以在 AWS Config 文件中设置单独的配置文件。例如,创建一个将 use_accelerate_endpoint 设置为 true 的配置文件和一个不设置 use_accelerate_endpoint 的配置文件。在执行一条命令时,根据是否需要使用加速终端节点来指定要使用的配置文件。

将对象上传到已启用 Transfer Acceleration 的存储桶的 AWS CLI 示例

以下示例通过使用已配置为使用加速终端节点的默认配置文件来将文件上传到已启用 Transfer Acceleration 的存储桶。

Copy
$ aws s3 cp file.txt s3://bucketname/keyname --region region

以下示例通过使用 --endpoint-url 参数指定加速终端节点来将文件上传到已启用 Transfer Acceleration 的存储桶。

Copy
$ aws configure set s3.addressing_style virtual $ aws s3 cp file.txt s3://bucketname/keyname --region region --endpoint-url http://s3-accelerate.amazonaws.com

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

本节提供了对 Transfer Acceleration 使用适用于 Java 的 AWS 开发工具包的示例。有关如何创建和测试有效 Java 示例的信息,请参阅测试 Java 代码示例

在由适用于 Java 的 AWS 开发工具包所提供的存储桶上启用 Amazon S3 Transfer Acceleration

以下 Java 示例说明如何对存储桶启用 Transfer Acceleration。

Copy
import java.io.IOException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.BucketAccelerateConfiguration; import com.amazonaws.services.s3.model.BucketAccelerateStatus; import com.amazonaws.services.s3.model.GetBucketAccelerateConfigurationRequest; import com.amazonaws.services.s3.model.SetBucketAccelerateConfigurationRequest; public class BucketAccelertionConfiguration { public static String bucketName = "*** Provide bucket name ***"; public static AmazonS3Client s3Client; public static void main(String[] args) throws IOException { s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); s3Client.setRegion(Region.getRegion(Regions.US_WEST_2)); // 1. Enable bucket for Amazon S3 Transfer Acceleration. s3Client.setBucketAccelerateConfiguration(new SetBucketAccelerateConfigurationRequest(bucketName, new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled))); // 2. Get the acceleration status of the bucket. String accelerateStatus = s3Client.getBucketAccelerateConfiguration(new GetBucketAccelerateConfigurationRequest(bucketName)).getStatus(); System.out.println("Acceleration status = " + accelerateStatus); } }

创建 Amazon S3 客户端,以使用由适用于 Java 的 AWS 开发工具包所提供的 Amazon S3 Transfer Acceleration 终端节点

在创建 AmazonS3Client 实例时,您可以借助由 AWS Java 开发工具包所提供的 setS3ClientOptions 方法,使用传输加速终端节点进行创建。

创建 Amazon S3 Java 客户端,以使用 Transfer Acceleration 终端节点

以下示例展示了在创建 setS3ClientOptions 实例时,如何借助由 AWS Java 开发工具包所提供的 AmazonS3Client 方法,使用传输加速终端节点进行创建。

Copy
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); s3Client.setRegion(Region.getRegion(Regions.US_WEST_2)); s3Client.setS3ClientOptions(S3ClientOptions.builder().setAccelerateModeEnabled(true).build());

创建 Amazon S3 Java 客户端,以使用 Transfer Acceleration 双堆栈终端节点

以下示例展示了在创建 setS3ClientOptions 实例时,如何借助由 AWS Java 开发工具包所提供的 AmazonS3Client 方法,使用 Transfer Acceleration 双堆栈终端节点进行创建。

Copy
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); s3Client.setRegion(Region.getRegion(Regions.US_WEST_2)); s3Client.setS3ClientOptions(S3ClientOptions.builder().enableDualstack().setAccelerateModeEnabled(true).build());

如果您在 Microsoft Windows 中使用 AWS Java 开发工具包,以使用 Transfer Acceleration 双堆栈终端节点,您可能需要设置以下 Java 虚拟机 (JVM) 属性。

Copy
java.net.preferIPv6Addresses=true

使用适用于 Java 的 AWS 开发工具包,将对象上传到已启用 Transfer Acceleration 的存储桶

本部分的 Java 示例旨在介绍如何使用加速终端节点上传对象。您可以通过修改创建 Amazon S3 Java 客户端,以使用 Transfer Acceleration 双堆栈终端节点中介绍的可创建 AmazonS3Client 实例的代码,使用 Transfer Acceleration 双堆栈终端节点的相关示例。

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

Java 示例:将单个对象上传到已启用 Transfer Acceleration 的存储桶

以下 Java 示例说明如何使用加速终端节点来上传单个对象。

Copy
import java.io.File; 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.S3ClientOptions; import com.amazonaws.services.s3.model.PutObjectRequest; public class AcceleratedUploadSingleObject { private static String bucketName = "*** Provide bucket name ***"; private static String keyName = "*** Provide key name ***"; private static String uploadFileName = "*** Provide file name with full path ***"; public static void main(String[] args) throws IOException { AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); s3Client.setRegion(Region.getRegion(Regions.US_WEST_2)); // Use Amazon S3 Transfer Acceleration endpoint. s3Client.setS3ClientOptions(S3ClientOptions.builder().setAccelerateModeEnabled(true).build()); try { System.out.println("Uploading a new object to S3 from a file\n"); File file = new File(uploadFileName); s3Client.putObject(new PutObjectRequest( bucketName, keyName, file)); } 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()); } } }

Java 示例:分段上传到已启用 Transfer Acceleration 的存储桶

以下 Java 示例说明如何使用终端节点进行分段上传。

Copy
import java.io.File; import com.amazonaws.AmazonClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.S3ClientOptions; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.Upload; public class AccelerateMultipartUploadUsingHighLevelAPI { private static String EXISTING_BUCKET_NAME = "*** Provide bucket name ***"; private static String KEY_NAME = "*** Provide key name ***"; private static String FILE_PATH = "*** Provide file name with full path ***"; public static void main(String[] args) throws Exception { AmazonS3Client s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); s3Client.configureRegion(Regions.US_WEST_2); // Use Amazon S3 Transfer Acceleration endpoint. s3Client.setS3ClientOptions(S3ClientOptions.builder().setAccelerateModeEnabled(true).build()); TransferManager tm = new TransferManager(s3Client); System.out.println("TransferManager"); // TransferManager processes all transfers asynchronously, // so this call will return immediately. Upload upload = tm.upload( EXISTING_BUCKET_NAME, KEY_NAME, new File(FILE_PATH)); System.out.println("Upload"); try { // Or you can block and wait for the upload to finish upload.waitForCompletion(); System.out.println("Upload complete"); } catch (AmazonClientException amazonClientException) { System.out.println("Unable to upload file, upload was aborted."); amazonClientException.printStackTrace(); } } }

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

本节提供了对 Transfer Acceleration 使用适用于 .NET 的 AWS 开发工具包的示例。有关如何创建和测试有效 .NET 示例的信息,请参阅运行 Amazon S3 .NET 代码示例

.NET 示例 1:对存储桶启用 Transfer Acceleration

以下 .NET 示例说明如何对存储桶启用 Transfer Acceleration。

Copy
using System; using System.Collections.Generic; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Util; namespace s3.amazon.com.docsamples { class SetTransferAccelerateState { private static string bucketName = "Provide bucket name"; public static void Main(string[] args) { using (var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2)) try { EnableTransferAcclerationOnBucket(s3Client); BucketAccelerateStatus bucketAcclerationStatus = GetBucketAccelerateState(s3Client); Console.WriteLine("Acceleration state = '{0}' ", bucketAcclerationStatus); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "To sign up for the service, go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when setting transfer acceleration", amazonS3Exception.Message); } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static void EnableTransferAcclerationOnBucket(IAmazonS3 s3Client) { PutBucketAccelerateConfigurationRequest request = new PutBucketAccelerateConfigurationRequest { BucketName = bucketName, AccelerateConfiguration = new AccelerateConfiguration { Status = BucketAccelerateStatus.Enabled } }; PutBucketAccelerateConfigurationResponse response = s3Client.PutBucketAccelerateConfiguration(request); } static BucketAccelerateStatus GetBucketAccelerateState(IAmazonS3 s3Client) { GetBucketAccelerateConfigurationRequest request = new GetBucketAccelerateConfigurationRequest { BucketName = bucketName }; GetBucketAccelerateConfigurationResponse response = s3Client.GetBucketAccelerateConfiguration(request); return response.Status; } } }

.NET 示例 2:将单个对象上传到已启用 Transfer Acceleration 的存储桶

以下 .NET 示例说明如何使用加速终端节点来上传单个对象。

Copy
using System; using System.Collections.Generic; using Amazon; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Util; namespace s3.amazon.com.docsamples { public class UploadtoAcceleratedBucket { private static RegionEndpoint TestRegionEndpoint = RegionEndpoint.USWest2; private static string bucketName = "Provide bucket name"; static string keyName = "*** Provide key name ***"; static string filePath = "*** Provide filename of file to upload with the full path ***"; public static void Main(string[] args) { using (var client = new AmazonS3Client(new AmazonS3Config { RegionEndpoint = TestRegionEndpoint, UseAccelerateEndpoint = true })) { WriteObject(client); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } static void WriteObject(IAmazonS3 client) { try { PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, FilePath = filePath, }; client.PutObject(putRequest); } 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); } } } } }

.NET 示例 3:分段上传到已启用 Transfer Acceleration 的存储桶

以下 .NET 示例说明如何使用终端节点进行分段上传。

Copy
using System; using System.IO; using Amazon; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Transfer; namespace s3.amazon.com.docsamples { class AcceleratedUploadFileMPUHAPI { private static RegionEndpoint TestRegionEndpoint = RegionEndpoint.USWest2; private static string existingBucketName = "Provide bucket name"; private static string keyName = "*** Provide your object key ***"; private static string filePath = "*** Provide file name with full path ***"; static void Main(string[] args) { try { var client = new AmazonS3Client(new AmazonS3Config { RegionEndpoint = TestRegionEndpoint, UseAccelerateEndpoint = true }); using (TransferUtility fileTransferUtility = new TransferUtility(client)) { // 1. Upload a file, file name is used as the object key name. fileTransferUtility.Upload(filePath, existingBucketName); Console.WriteLine("Upload 1 completed"); // 2. Specify object key name explicitly. fileTransferUtility.Upload(filePath, existingBucketName, keyName); Console.WriteLine("Upload 2 completed"); // 3. Upload data from a type of System.IO.Stream. using (FileStream fileToUpload = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { fileTransferUtility.Upload(fileToUpload, existingBucketName, keyName); } Console.WriteLine("Upload 3 completed"); // 4.Specify advanced settings/options. TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest { BucketName = existingBucketName, FilePath = filePath, StorageClass = S3StorageClass.ReducedRedundancy, PartSize = 6291456, // 6 MB. Key = keyName, CannedACL = S3CannedACL.PublicRead }; fileTransferUtilityRequest.Metadata.Add("param1", "Value1"); fileTransferUtilityRequest.Metadata.Add("param2", "Value2"); fileTransferUtility.Upload(fileTransferUtilityRequest); Console.WriteLine("Upload 4 completed"); } } catch (AmazonS3Exception s3Exception) { Console.WriteLine("{0} {1}", s3Exception.Message, s3Exception.InnerException); } } } }

通过适用于 JavaScript 的 AWS 开发工具包使用 Transfer Acceleration

有关通过使用适用于 JavaScript 的 AWS 开发工具包来启用 Transfer Acceleration 的示例,请参阅适用于 JavaScript 的 AWS 开发工具包 API 参考中的调用 putBucketAccelerateConfiguration 操作

通过 AWS SDK for Python (Boto) 使用 Transfer Acceleration

有关通过使用 SDK for Python 来启用 Transfer Acceleration 的示例,请参阅 AWS SDK for Python (Boto 3) API Reference中的 put_bucket_accelerate_configuration

使用其他 AWS 开发工具包

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