简体   繁体   English

如何为Amazon S3存储桶中的每个文件夹执行命令(如副本)?

[英]How can I execute a command (like copy) for each folder in an Amazon S3 bucket?

I have an S3 bucket with roughly a thousand folders. 我有一个大约有1000个文件夹的S3存储桶。 Inside each folder, I want to copy a file to a new folder a level in. Something like: 在每个文件夹中,我想将文件复制到某个级别的新文件夹中。类似于:

foreach (folder in myBucket){
  cp folder/myFile.abc folder/myNewFolder/myFile.abc;
}

How can I execute this one-off logic in S3? 如何在S3中执行这种一次性逻辑?

I ended up just using bash and Amazon's own CLI : 我最终只使用了bash和Amazon自己的CLI

BUCKET=myBucketName
for dir in $(aws s3 ls $BUCKET | awk '{print $2}'); do
    aws s3 cp s3://$BUCKET/${dir}myFile.abc s3://$BUCKET/${dir}myNewFolder/myFile.abc
done

aws s3 ls does not include any helpful flags to modify its output, and so I used awk to cut off the "PRE" that came with the directory output. aws s3 ls不包含任何有用的标志来修改其输出,因此我使用awk切断了目录输出随附的“ PRE”。

Place the above in myScript and execute it with bash myScript . 将以上内容放入myScript并使用bash myScript执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM