How to rollback to previous version in Amazon S3 bucket?

26,159

Solution 1

Here is how to get that done:

If you are using cli,

https://docs.aws.amazon.com/cli/latest/reference/s3api/get-object.html

Get the object with the version you want.

Then perform a put object for the downloaded object.

https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html

Your old S3 object will be the latest object now.

AWS S3 object is immutable and you can only put and delete. Rename is GET and PUT of the same object with a different name.

Hope it helps.

Solution 2

No. However, to protect against this in the future, you can enable versioning on your bucket and even configure the bucket to prevent automatic overwrites and deletes.

enter image description here

To enable versioning on your bucket, visit the Properties tab in the bucket and turn it on. After you have done so, copies or versions of each item within the bucket will contain version meta data and you will be able to retrieve older versions of the objects you have uploaded.

Once you have enabled versioning, you will not be able to turn it off.

EDIT (Updating my answer for your updated question):

You can't version your objects in this fashion. You are providing each object a unique Key, so S3 is treating it as a new object. You are going to need to use the same Key for each object PUTS to use versioning correctly. The only way to get this to work would be to GETS all of the objects from the bucket and find the most current date in the Key programmatically.

EDIT 2:

https://docs.aws.amazon.com/AmazonS3/latest/dev/RestoringPreviousVersions.html

To restore previous versions you can:

One of the value propositions of versioning is the ability to retrieve previous versions of an object. There are two approaches to doing so:

Copy a previous version of the object into the same bucket The copied object becomes the current version of that object and all object versions are preserved.

Permanently delete the current version of the object When you delete the current object version, you, in effect, turn the previous version into the current version of that object.

Share:
26,159
whitesiroi
Author by

whitesiroi

Updated on December 14, 2021

Comments

  • whitesiroi
    whitesiroi over 2 years

    I upload folders/files by:

    aws s3 cp files s3://my_bucket/
    aws s3 cp folder s3://my_bucket/ --recursive
    

    Is there a way to return/rollback to previous version?

    Like git revert or something similar?

    UPDATE: here the is test file that I uploaded 4 times.

    How to get to previous version (make it the "Latest version")

    For example make this "Jan 17, 2018 12:48:13" or "Jan 17, 2018 12:24:30" to become the "Latest version" not in gui but by using command line? enter image description here