我通过以下方式上传文件夹/文件:
aws s3 cp files s3://my_bucket/
aws s3 cp folder s3://my_bucket/ --recursive
有办法返回/回滚到以前的版本吗?
像 git revert 或者类似的东西?
这是我上传了4次的测试文件。
如何获取以前的版本(使其成为“最新版本”)
例如设置“2018年1月17日12:48:13”或“2018年1月17日12:24:30” 不是在 GUI 中而是通过使用命令行成为“最新版本”?
以下是如何完成此操作:
如果您使用的是 cli,
https://docs.aws.amazon.com/cli/latest/reference/s3api/get-object.html
获取您想要的版本的对象。
然后对下载的对象执行 put 对象。
https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html
您的旧 S3 对象现在将是最新的对象。
AWS S3 对象是不可变的,您只能放置和删除。重命名是使用不同名称对同一对象进行 GET 和 PUT。
希望有帮助。
不。但是,为了防止将来出现这种情况,您可以在存储桶上启用版本控制,甚至配置存储桶以防止自动覆盖和删除。
要在存储桶上启用版本控制,请访问存储桶中的“属性”选项卡并将其打开。完成此操作后,存储桶中每个项目的副本或版本将包含版本元数据,并且您将能够检索已上传的对象的旧版本。
一旦启用版本控制,您将无法将其关闭。
编辑(更新我对更新问题的答案):
您无法以这种方式对对象进行版本控制。您为每个对象提供了一个唯一的 Key,因此 S3 将其视为一个新对象。您将需要对每个对象 PUTS 使用相同的 Key 才能正确使用版本控制。使其发挥作用的唯一方法是从存储桶中获取所有对象,并以编程方式在键中查找最新日期。
编辑2:
https://docs.aws.amazon.com/AmazonS3/latest/dev/RestoringPreviousVersions.html
要恢复以前的版本,您可以:
版本控制的价值主张之一是能够检索 对象的先前版本。有两种方法可以做到这一点:
将对象的先前版本复制到同一个存储桶中 复制的对象 对象成为该对象和所有对象的当前版本 版本被保留。
永久删除当前版本的对象删除时 当前的对象版本,您实际上将以前的版本 进入该对象的当前版本。
我无法得到我想要得到的这个问题的答案。我通过访问 aws s3 控制台找到了自己的答案,并想在这里分享。
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
S3 允许您为存储桶启用版本控制。如果您启用了版本控制,您应该能够找到以前的版本。如果没有,那你就不走运了。
请参阅以下页面了解更多信息:https://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html
根据文档:https://docs.aws.amazon.com/AmazonS3/latest/userguide/RestoringPreviousVersions.html,有两种方法:
## 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()