目前,我们有多个具有应用程序前缀和区域后缀的存储桶,例如存储桶名称
有没有办法找到给定一个前缀的所有桶?是否有类似的东西:
s3 = boto3.resource('s3')
buckets = s3.buckets.filter(Prefix="myapp-")
Run Code Online (Sandbox Code Playgroud) 我是我的 S3 存储桶,有很多文件采用不同的文件格式。所以我想从所有具有.JSON扩展名的子文件夹复制到另一个文件夹。
当前结构:
S3://mybucket/f1/file.JPG
S3://mybucket/f1/newfile.JSON
S3://mybucket/f2/Oldfile.JSON
Run Code Online (Sandbox Code Playgroud)
它(JSON FILES)应该被复制到文件夹中:
S3://mybucket/arrange/newfile.JSON
S3://mybucket/arrange/Oldfile.JSON
Run Code Online (Sandbox Code Playgroud)
我试过这个(但没有 JSON 过滤器)来自 stackoverflow
import os
import boto3
old_bucket_name = 'SRC'
old_prefix = 'A/B/C/'
new_bucket_name = 'TGT'
new_prefix = 'L/M/N/'
s3 = boto3.resource('s3')
old_bucket = s3.Bucket(old_bucket_name )
new_bucket = s3.Bucket(new_bucket_name )
for obj in old_bucket.objects.filter(Prefix=old_prefix):
old_source = { 'Bucket': old_bucket_name,
'Key': obj.key}
# replace the prefix
new_key = obj.key.replace(old_prefix, new_prefix)
new_obj = new_bucket.Object(new_key)
new_obj.copy(old_source)
Run Code Online (Sandbox Code Playgroud)