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

启用存储桶版本控制的示例

此部分提供对存储桶启用版本控制的示例。这些示例首先对存储桶启用版本控制,然后检索版本控制状态。有关简介,请参阅使用版本控制

使用 Amazon S3 控制台

有关使用 Amazon S3 控制台在存储桶上启用版本控制的更多信息,请参阅如何为 S3 存储桶启用或暂停版本控制? (在 Amazon Simple Storage Service 控制台用户指南 中)。

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

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

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.AmazonS3Exception; import com.amazonaws.services.s3.model.BucketVersioningConfiguration; import com.amazonaws.services.s3.model.SetBucketVersioningConfigurationRequest; public class BucketVersioningConfigurationExample { public static String bucketName = "*** 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_EAST_1)); try { // 1. Enable versioning on the bucket. BucketVersioningConfiguration configuration = new BucketVersioningConfiguration().withStatus("Enabled"); SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest = new SetBucketVersioningConfigurationRequest(bucketName,configuration); s3Client.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest); // 2. Get bucket versioning configuration information. BucketVersioningConfiguration conf = s3Client.getBucketVersioningConfiguration(bucketName); System.out.println("bucket versioning configuration status: " + conf.getStatus()); } catch (AmazonS3Exception amazonS3Exception) { System.out.format("An Amazon S3 error occurred. Exception: %s", amazonS3Exception.toString()); } catch (Exception ex) { System.out.format("Exception: %s", ex.toString()); } } }

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

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

Copy
using System; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class BucketVersioningConfiguration { static string bucketName = "*** bucket name ***"; public static void Main(string[] args) { using (var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { try { EnableVersioningOnBucket(client); string bucketVersioningStatus = RetrieveBucketVersioningConfiguration(client); } 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 service, go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when listing objects", amazonS3Exception.Message); } } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static void EnableVersioningOnBucket(IAmazonS3 client) { PutBucketVersioningRequest request = new PutBucketVersioningRequest { BucketName = bucketName, VersioningConfig = new S3BucketVersioningConfig { Status = VersionStatus.Enabled } }; PutBucketVersioningResponse response = client.PutBucketVersioning(request); } static string RetrieveBucketVersioningConfiguration(IAmazonS3 client) { GetBucketVersioningRequest request = new GetBucketVersioningRequest { BucketName = bucketName }; GetBucketVersioningResponse response = client.GetBucketVersioning(request); return response.VersioningConfig.Status; } } }

使用其他 AWS 开发工具包

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