Reputation: 2833
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?
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?
Upvotes: 11
Views: 51769
Reputation: 994
According to the documentation: https://docs.aws.amazon.com/AmazonS3/latest/userguide/RestoringPreviousVersions.html, there are two ways:
## Download a older version of data
import boto3
from operator import attrgetter
import os
import subprocess
s3_client = boto3.client("s3")
s3 = boto3.resource('s3')
bucket_name = 'my_bucket' # CHANGE
prefix = "my_prefix" # CHANGE
local_folder = './temp' #CHANGE
s3_path = f"s3://{bucket_name}/{prefix}"
os.makedirs(local_folder, exsit_ok=True)
# List objects in the bucket with filtering on last modified date
bucket = s3.Bucket(bucket_name)
versions = sorted(
bucket.object_versions.filter(Prefix=prefix),
key=attrgetter("last_modified"),
reverse=True,
)
all_modify_hour = []
for version in versions:
modify_hour = version.last_modified.strftime('%Y-%m-%d-%H')
if modify_hour not in all_modify_hour:
all_modify_hour.append(modify_hour)
print(all_modify_hour)
## Can also hard code this to specific update hour time
# correct_modify_hour = '2024-03-01-08'
# Auto configure to the earliest one
correct_modify_hour = all_modify_hour[-1]
print(correct_modify_hour)
correct_versions = []
for version in versions:
if version.last_modified.strftime('%Y-%m-%d-%H') == correct_modify_hour:
correct_versions.append(version)
print("count files:", len(correct_versions))
# download all files locally
for version in correct_versions:
local_file = local_folder+'/'+version.object_key.split('/')[-1]
s3_client.download_file(version.bucket_name, version.object_key, local_file, ExtraArgs={'VersionId': version.id,})
cmd='aws s3 cp '+local_folder+' '+s3_path+' --recursive'
p=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
p.communicate()
Upvotes: 0
Reputation: 1259
I wasn't able to get answer I was looking to get for this question. I figured out myself by going to aws s3 console and would like to share here.
So, the quickest way is to simply navigate to:
--AWS Console -> to s3 console -> the bucket -> the s3 object
You will see the following:
At this point you can simpyl navigate to all your object
versions by clicking at the "Versions" and pick (download or move)
whichever version of the object you are interested in
Upvotes: 2
Reputation: 13025
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.
Upvotes: 6
Reputation: 586
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.
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.
Upvotes: 6
Reputation: 372
S3 allows you to enable versioning for your bucket. If you have versioning on, you should be able to find previous versions back. If not, you are out of luck.
See the following page for more information: https://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html
Upvotes: 0