使用AWS S3同步时排除多个文件夹

96

如何在使用aws s3 syn时排除多个文件夹?

我尝试过:

    # aws s3 sync s3://inksedge-app-file-storage-bucket-prod-env \ 
                  s3://inksedge-app-file-storage-bucket-test-env \
                  --exclude 'reportTemplate/* orders/* customers/*'

但它仍在同步“customer”文件夹。

输出:

    copy: s3://inksedge-app-file-storage-bucket-prod-env/customers/116/miniimages/IMG_4800.jpg
       to s3://inksedge-app-file-storage-bucket-test-env/customers/116/miniimages/IMG_4800.jpg

    copy: s3://inksedge-app-file-storage-bucket-prod-env/customers/116/miniimages/DSC_0358.JPG
       to s3://inksedge-app-file-storage-bucket-test-env/customers/116/miniimages/DSC_0358.JPG

我认为你需要为每个模式使用--exclude选项,例如:--exclude 'reportTemplate/*' --exclude 'orders/*' --exclude 'customers/*'。像这样将整个内容放在引号中很可能会被视为单个模式。 - Florian Castellane
4个回答

168

最后这个方法对我有用:

aws s3 sync s3://my-bucket s3://my-other-bucket \
            --exclude 'customers/*' \
            --exclude 'orders/*' \
            --exclude 'reportTemplate/*'  

提示:为了正常工作,您必须将通配符和特殊字符用单引号或双引号括起来。以下是匹配字符的示例。有关S3命令的更多信息,请在这里查看Amazon文档

*: Matches everything
?: Matches any single character
[sequence]: Matches any character in sequence
[!sequence]: Matches any character not in sequence

1
Amazon提供AWS CLI,这是一款用于与AWS交互的命令行工具。使用AWS CLI,整个过程不到三秒钟:$ aws s3 sync s3://<bucket>/<path> </local/path>例如:aws s3 sync s3://s3.aws-cli.demo/photos/office ~/Pictures/work - Tapan Banker

31

对于那些想要同步存储桶中的某些子文件夹的人来说,排除过滤器适用于正在同步的文件夹内部的文件和文件夹,而不是相对于存储桶的路径。例如:

aws s3 sync s3://bucket1/bootstrap/ s3://bucket2/bootstrap --exclude '*' --include 'css/*'

会同步文件夹bootstrap/css,但不会同步以下文件夹树中的bootstrap/js和bootstrap/fonts:

bootstrap/
├── css/
│   ├── bootstrap.css
│   ├── bootstrap.min.css
│   ├── bootstrap-theme.css
│   └── bootstrap-theme.min.css
├── js/
│   ├── bootstrap.js
│   └── bootstrap.min.js
└── fonts/
    ├── glyphicons-halflings-regular.eot
    ├── glyphicons-halflings-regular.svg
    ├── glyphicons-halflings-regular.ttf
    └── glyphicons-halflings-regular.woff

也就是说,筛选器是'css / *'而不是'bootstrap / css / *'

详见https://docs.aws.amazon.com/cli/latest/reference/s3/index.html#use-of-exclude-and-include-filters


谢谢,这是唯一帮助到我的答案。但它背后的逻辑是什么,为什么一个 bootstrap/css/* 过滤器不起作用? - Itamar Katz
@ItamarKatz 这是因为过滤器适用于所选的文件夹,所以根据您提供的过滤器,它实际上会寻找包含文件夹s3://bucket2/bootstrap/bootstrap/css/*的内容。 - Jon

5

在Windows命令提示符中,单引号'无法使用,只有双引号"才能使用,因此在通配符周围使用" ",例如:

aws s3 sync  s3://bucket-1/ . --exclude "reportTemplate/*" --exclude "orders/*"

在 Windows 10 上使用单引号(已通过 --dryrun 选项测试)无效。


0

我在有多级文件夹结构时使用了一种稍微不同的方法。使用'**'和--include。

命令:

aws s3 sync s3://$SOURCE_BUCKET/dir1/dir2/  s3://$TARGET_BUCKET/dir1/dir2/ --include "\**/**'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接