Use PutBucketVersioning
with an Amazon SDK or CLI
The following code examples show how to use PutBucketVersioning
.
- CLI
-
- Amazon CLI
-
The following command enables versioning on a bucket named
amzn-s3-demo-bucket
:aws s3api put-bucket-versioning --bucket
amzn-s3-demo-bucket
--versioning-configurationStatus=Enabled
The following command enables versioning, and uses an mfa code
aws s3api put-bucket-versioning --bucket
amzn-s3-demo-bucket
--versioning-configurationStatus=Enabled
--mfa"SERIAL 123456"
-
For API details, see PutBucketVersioning
in Amazon CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. /** * Enables bucket versioning for the specified S3 bucket. * * @param s3Client the S3 client to use for the operation * @param bucketName the name of the S3 bucket to enable versioning for */ public static void enableBucketVersioning(S3Client s3Client, String bucketName){ VersioningConfiguration versioningConfiguration = VersioningConfiguration.builder() .status(BucketVersioningStatus.ENABLED) .build(); PutBucketVersioningRequest versioningRequest = PutBucketVersioningRequest.builder() .bucket(bucketName) .versioningConfiguration(versioningConfiguration) .build(); s3Client.putBucketVersioning(versioningRequest); System.out.println("Bucket versioning has been enabled for "+bucketName); }
-
For API details, see PutBucketVersioning in Amazon SDK for Java 2.x API Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: The command enables versioning for the given S3 bucket.
Write-S3BucketVersioning -BucketName 'amzn-s3-demo-bucket' -VersioningConfig_Status Enabled
-
For API details, see PutBucketVersioning
in Amazon Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: The command enables versioning for the given S3 bucket.
Write-S3BucketVersioning -BucketName 'amzn-s3-demo-bucket' -VersioningConfig_Status Enabled
-
For API details, see PutBucketVersioning
in Amazon Tools for PowerShell Cmdlet Reference (V5).
-
For a complete list of Amazon SDK developer guides and code examples, see Developing with Amazon S3 using the Amazon SDKs. This topic also includes information about getting started and details about previous SDK versions.