S3Bucket versioningBucket = s3Service.getOrCreateBucket(
"test-versioning");
String vBucketName = versioningBucket.getName();
// Check bucket versioning status for the bucket
S3BucketVersioningStatus versioningStatus =
s3Service.getBucketVersioningStatus(vBucketName);
System.out.println("Versioning enabled ? "
+ versioningStatus.isVersioningEnabled());
// Suspend (disable) versioning for a bucket -- will have no
// effect if bucket versioning is not yet enabled.
// This will not delete any existing object versions.
s3Service.suspendBucketVersioning(vBucketName);
// Enable versioning for a bucket.
s3Service.enableBucketVersioning(vBucketName);
// Once versioning is enabled you can GET, PUT, copy and
// delete objects as normal. Every change to an object will
// cause a new version to be created.
// Store and update and delete an object in the versioning bucket
S3Object versionedObject = new S3Object("versioned-object", "Initial version");
s3Service.putObject(vBucketName, versionedObject);
versionedObject = new S3Object("versioned-object", "Second version");
s3Service.putObject(vBucketName, versionedObject);
versionedObject = new S3Object("versioned-object", "Final version");
s3Service.putObject(vBucketName, versionedObject);
// If you retrieve an object with the standard method you will
// get the latest version, and if the object is in a versioned
// bucket its Version ID will be available
versionedObject = s3Service.getObject(vBucketName, "versioned-object");
String finalVersionId = versionedObject.getVersionId();
System.out.println("Version ID: " + finalVersionId);
// If you delete a versioned object it is no longer available using
// standard methods...
s3Service.deleteObject(vBucketName, "versioned-object");
try {
s3Service.getObject(vBucketName, "versioned-object");
} catch (S3ServiceException e) {
if (e.getResponseCode() == 404) {
System.out.println("Is deleted object versioned? "
+ e.getResponseHeaders().get(Constants.AMZ_DELETE_MARKER));
System.out.println("Delete marker version ID: "
+ e.getResponseHeaders().get(Constants.AMZ_VERSION_ID));
}
}
// ... but you can use a versioning-aware method to retrieve any of
// the prior versions by Version ID.
versionedObject = s3Service.getVersionedObject(finalVersionId,
vBucketName, "versioned-object");
String versionedData = ServiceUtils.readInputStreamToString(
versionedObject.getDataInputStream(), "UTF-8");
System.out.println("Data from prior version of deleted document: "
+ versionedData);
// List all the object versions in the bucket, with no prefix
// or delimiter restrictions. Each result object will be one of
// S3Version or S3DeleteMarker.
BaseVersionOrDeleteMarker[] versions =
s3Service.listVersionedObjects(vBucketName, null, null);
for (int i = 0; i < versions.length; i++) {
System.out.println(versions[i]);
}
// List versions of objects that match a prefix.
String versionPrefix = "versioned-object";
versions = s3Service.listVersionedObjects(vBucketName, versionPrefix, null);
// JetS3t includes a convenience method to list only the versions
// for a specific object, even if it shares a prefix with other objects.
versions = s3Service.getObjectVersions(vBucketName, "versioned-object");
// There are versioning-aware methods corresponding to all S3 operations
versionedObject = s3Service.getVersionedObjectDetails(
finalVersionId, vBucketName, "versioned-object");
// Confirm that S3 returned the versioned object you requested
if (!finalVersionId.equals(versionedObject.getVersionId())) {
throw new Exception("Incorrect version!");
}
s3Service.copyVersionedObject(finalVersionId,
vBucketName, "versioned-object",
"destination-bucket", new S3Object("copied-from-version"),
false, null, null, null, null);
AccessControlList versionedObjectAcl =
s3Service.getVersionedObjectAcl(finalVersionId,
vBucketName, "versioned-object");
s3Service.putVersionedObjectAcl(finalVersionId,
vBucketName, "versioned-object", versionedObjectAcl);
// To delete an object version once-and-for-all you must use the
// versioning-specific delete operation, and you can only do so
// if you are the owner of the bucket containing the version.
s3Service.deleteVersionedObject(finalVersionId,
vBucketName, "versioned-object");
// You can easily delete all the versions of an object using
// one of JetS3t's multi-threaded services.
versions = s3Service.getObjectVersions(vBucketName, "versioned-object");
// Convert version and delete marker objects into versionId strings.
String[] versionIds = BaseVersionOrDeleteMarker.toVersionIds(versions);
(new S3ServiceSimpleMulti(s3Service)).deleteVersionsOfObject(
versionIds, vBucketName, "versioned-object");
//////////////////////////////////////////////////////////////
// For additional data protection you can require multi-factor
// authentication (MFA) to delete object versions.
//////////////////////////////////////////////////////////////
// Require multi-factor authentication to delete versions.
s3Service.enableBucketVersioningAndMFA(vBucketName);
// Check MFA status for the bucket
versioningStatus = s3Service.getBucketVersioningStatus(vBucketName);
System.out.println("Multi-factor auth required to delete versions ? "
+ versioningStatus.isMultiFactorAuthDeleteRequired());
// If MFA is enabled for a bucket you must provide the serial number
// for your multi-factor authentication device and a recent code to
// delete object versions.
String multiFactorSerialNumber = "#111222333";