AWS Developer Tools Blog
Working with Amazon S3 Object Versions and the AWS SDK for .NET
Amazon S3 allows you to enable versioning for a bucket. You can enable or disable versioning with the SDK by calling the PutBucketVersioning
method. Note, all code samples were written for our new version 2 of the SDK. Users of version 1 of the SDK will notice some slight name changes.
Once versioning is enabled, every PutObject
call with the same key will add a new version of the object with a different version ID instead of overwriting the object. For example, running the code below will create three versions of the “sample.txt” object. The sleeps are added to give a more obvious difference in the timestamps.
Now, if you call the GetObject
method without specifying a version ID like this:
It will print out the contents of the last object that was put into the bucket.
Use the ListVersions
method to get the list of versions.
To get a specific version of an object, you simply need to specify the VersionId
property when performing a GetObject
.
Deleting an object that is versioned works differently than the non-versioned objects. If you call delete like this:
and then try to do a GetObject
for the “sample.txt” object, S3 will return an error that the object doesn’t exist. What S3 actually does when you call delete for a versioned object is insert a delete marker. You can see this if you list the versions again.
If you want to delete a specific version of an object, when calling DeleteObject
, set the VersionId
property. This is also how you can restore an object by deleting the delete marker.
Now, calls to GetObject
for the “sample.txt” object will succeed again.