选择S3中最后创建的对象

问题描述 投票:0回答:1

我有一个 S3 路径:

my_bucket/object_1/object_2/object-3/object_2023/2023-30-09-15h21/
my_bucket/object_1/object_2/object-3/object_2020/2020-25-11-12h18/
my_bucket/object_1/object_2/object-3/object_2022/2022-03-07-14h22/
my_bucket/object_1/object_2/object-3/object_2023/2023-24-11-15h21/

我正在选择包含最后创建的对象的路径,例如:

my_bucket/object_1/object_2/object-3/object_2023/2023-24-11-15h21/

我已经创建了一个小脚本来选择对象:

object_2023
,我正在寻找一个解决方案来选择
2023-24-11-15h21/
来构建所有路径:

my_bucket/object_1/object_2/object-3/object_2023/2023-24-11-15h21/

请问您有解决方案吗?我该怎么做? 谢谢

session_root = boto3.Session(region_name='eu-west-3', profile_name='my_profile')
s3_client = session_root.client('s3')

var1 = "object_1/object_2/object-3/"
var2 = "object_"
currentYear = str(date.today().year)
#print(currentYear)
var3 = "".join([var2, currentYear]) #object_2023
#print(var2)
var4 = "".join([var1, var3]) #object_2/object_2023/
#print(var4)
var5 = "/2023-24-11-15h21/"
prefix = "".join([var4, var5])
print(prefix)
python-3.x amazon-web-services amazon-s3 boto3
1个回答
0
投票

我不确定,但也许您需要将文件上传到已经存在的文件夹中

s3 = boto3.resource('s3')    
s3.Bucket('bucketname').upload_file('file','folder/object_1/object_2/object-3/object_2023/2023-24-11-15h21/')

这是参考: 如何使用 boto3 将文件或数据写入 S3 对象

© www.soinside.com 2019 - 2024. All rights reserved.