【问题标题】:Copying multiple file from folder to s3 excluding few folder将多个文件从文件夹复制到 s3,不包括几个文件夹
【发布时间】:2021-03-02 13:05:35
【问题描述】:

我想将文件夹中的多个文件从 ec2 复制到 s3 存储桶。

文件夹结构是这样的,

└───data
    ├───201707
            |
            |__ new
            |
            |__ new1

我只想复制201707 文件夹的内容,所以我使用了以下命令:

aws s3 cp data/ s3://bucket-name/data/201707/ --recursive --exclude 'data/new/*' --exclude 'data/new1/*'

但是 s3 中的输出有一个 new 和 new1 文件夹,排除这些文件夹的正确命令是什么?

【问题讨论】:

    标签: amazon-web-services amazon-s3


    【解决方案1】:

    我有类似的要求,这对我有用。
    这行得通-

    aws s3 cp data/ s3://bucket-name/data/201707/ --recursive --exclude 'data/new/*' --exclude 'data/new1/*'
    

    但我的做法略有不同。 首先我找到我需要的文件并将它们放到 tmp 或不同的文件夹中

    export files_found=`find $file_path -type f -name "*.txt" -mtime -1 -printf "%f\n"`
    

    如果你想让你的路径作为一个变量,那会很好

    mkdir -p $your_path/today_files
    

    然后

    cp $files_found /tmp/today_files
    

    现在 s3 cp 可以轻松工作了

    aws s3 cp /tmp/today_files s3://$bucket_name/folder_name --recursive --region us-east-1`
    

    然后清理您的文件夹。这将确保我们只复制了 s3 中需要的文件。 希望这有效..

    【讨论】:

      【解决方案2】:

      s3aws-cli 子命令的--exclude--include 参数采用模式,而不仅仅是路径前缀。

      --exclude (string) Exclude all files or objects from the command that matches the specified pattern.
      

      来源:https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html

      The following pattern symbols are supported.
      
      *: Matches everything
      ?: Matches any single character
      [sequence]: Matches any character in sequence
      [!sequence]: Matches any character not in sequence
      

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

      根据您的示例,正​​确的调用应该是:

      aws s3 cp data/ s3://bucket-name/data/201707/ --recursive --exclude 'data/new/*' --exclude 'data/new1/*'
      

      【讨论】:

      • 谢谢,Luke - 我仍然看到上述命令也存在同样的问题。
      • 您是否首先从 s3 中删除旧的、不需要的文件夹?此命令不会同步和删除它们。
      • 原来问题已经在排除路径的末尾有*,它只是没有呈现,因为它不在反引号或代码块中。当然,星号是一个降价字符。格式化不仅对易读性很重要;它有时会影响内容的含义。请使用您的反引号和代码块,女士们,先生们。
      • @Luke - s3 目标文件夹是新文件夹 - 它不包含任何内容。
      【解决方案3】:

      在您的情况下,排除路径是相对于 data/ 的,因此您不想通过 --exclude 再次指定它:

      aws s3 cp data/ s3://<BUCKET_NAME>/data/201707/ --recursive --exclude "201707/new/*" --exclude "201707/new1/*"
      

      这会将data/201707/ 中的文件复制到s3://bucket/data/201707/201707/ 也许这是您想要的,但如果不是,您可能需要从目标文件夹中删除201707

      aws s3 cp data/ s3://<BUCKET_NAME>/data/ --recursive --exclude "201707/new/*" --exclude "201707/new1/*"
      

      data/201707/中的文件复制到s3://bucket/data/201707/


      注意: 在 Windows 上,我需要为排除模式使用双引号。单引号不起作用。

      【讨论】:

      • 迈克,我已经编辑了问题,data/201707 有文件和 new,new1 文件夹。我只想将文件复制到 201707,而不是文件夹及其内容(new,new1)
      猜你喜欢
      • 2022-08-04
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多