将文件从 S3 子文件夹移动到 S3 存储桶根目录
Move files from S3 subfolder to the S3 bucket root
我需要将子文件夹的所有文件移动到它的 s3 存储桶根目录。
现在我正在使用 cmd AWS CLI
aws s3 mv s3:\testbucket\testsubfolder\testsubfolder2\folder s3:\testbucket\
我的主要问题是子文件夹“文件夹”在 TeamCity 运行 之后每天都在变化。有什么方法可以知道“testsubfolder2”里面有没有新的文件夹,然后把里面的内容复制到S3bucket根目录?
我想自动执行此操作,因为我们每天 运行 报告并存储在 S3 中,但 TeamCity 创建项目文件夹树,我们需要 S3 根目录中的所有文件
谢谢。
在此 testsubfolder2 目录中上传文件时,您可以触发 Lambda。
查看来自 AWS 的教程:https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html
小心您的 S3 规则,因为您可以创建一个循环并增加您的账单,因为 AWS S3 MV 在命令行后面使用 COPY 和 DELETE。
这里有一些代码可以将给定前缀(以及该前缀下的 sub-folders)中的任何对象移动到存储桶的根目录中。 (实际上,它复制对象然后删除它。)
import boto3
BUCKET = 'stack-move'
PREFIX = 'foo1/foo2/'
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(BUCKET)
for object in bucket.objects.filter(Prefix=PREFIX):
print(object.key)
copy_source = {
'Bucket': BUCKET,
'Key': object.key
}
target_key = object.key[object.key.rfind('/')+1:] # Get just the name after the last slash
# Copy the object
target = bucket.Object(target_key)
target.copy(copy_source)
# Delete the object
object.delete()
我需要将子文件夹的所有文件移动到它的 s3 存储桶根目录。
现在我正在使用 cmd AWS CLI
aws s3 mv s3:\testbucket\testsubfolder\testsubfolder2\folder s3:\testbucket\
我的主要问题是子文件夹“文件夹”在 TeamCity 运行 之后每天都在变化。有什么方法可以知道“testsubfolder2”里面有没有新的文件夹,然后把里面的内容复制到S3bucket根目录?
我想自动执行此操作,因为我们每天 运行 报告并存储在 S3 中,但 TeamCity 创建项目文件夹树,我们需要 S3 根目录中的所有文件
谢谢。
在此 testsubfolder2 目录中上传文件时,您可以触发 Lambda。
查看来自 AWS 的教程:https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html
小心您的 S3 规则,因为您可以创建一个循环并增加您的账单,因为 AWS S3 MV 在命令行后面使用 COPY 和 DELETE。
这里有一些代码可以将给定前缀(以及该前缀下的 sub-folders)中的任何对象移动到存储桶的根目录中。 (实际上,它复制对象然后删除它。)
import boto3
BUCKET = 'stack-move'
PREFIX = 'foo1/foo2/'
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(BUCKET)
for object in bucket.objects.filter(Prefix=PREFIX):
print(object.key)
copy_source = {
'Bucket': BUCKET,
'Key': object.key
}
target_key = object.key[object.key.rfind('/')+1:] # Get just the name after the last slash
# Copy the object
target = bucket.Object(target_key)
target.copy(copy_source)
# Delete the object
object.delete()