【问题标题】:sync local folder structure to s3 root structure将本地文件夹结构同步到 s3 根结构
【发布时间】:2019-07-21 15:06:05
【问题描述】:

在我的管道中,我正在尝试将我的本地文件夹(或者我应该说存储库文件夹)同步到 s3 存储桶。现在我可以执行aws s3 sync . s3:// 了,但这当然会出错,因为未指定存储桶。但基本上这正是我想要的。我在本地的文件夹结构到底是怎样的;这就是我在 S3 中想要的样子。

所以在本地:

  • bucket1/file1.txt
  • bucket1/file2.txt
  • bucket1/subbucket1/file3.txt

需要精确到我的 s3 帐户的根目录...如何解决这个问题?

顺便说一句; sync 可能有点矫枉过正,因为我只想从根目录复制(并覆盖!)到 s3 文件夹。 (尚)对删除等不感兴趣。

我能做什么..?

【问题讨论】:

    标签: amazon-s3


    【解决方案1】:

    AWS Command-Line Interface (CLI)aws s3 sync 命令需要存储桶名称。

    因此,您需要编写一个脚本来提取存储桶名称并将其插入到aws s3 sync 命令中,或者您需要编写自己的程序来代替 AWS CLI。

    如果您的存储桶数量有限并且它们不经常更改,您可以编写一个反复调用 AWS CLI 的脚本,例如:

    aws s3 sync bucket1/ s3://bucket1/
    aws s3 sync bucket2/ s3://bucket2/
    etc.
    

    【讨论】:

    • 它很可能是手动存储桶:see_no_evil: 但是需要几个步骤,所以我可以提取存储桶名称;但是如果存储桶名称尚不存在,则如果现在失败。作为测试aws s3 sync . s3://bucketdoesnotexist-test --exclude "*" --include "*.txt"
    • 这是否意味着对于同步操作,存储桶必须始终存在?我以为没有“桶”..
    • 是的,存储桶必须存在才能与之交互。有存储桶,但没有“文件夹”(您可以将文件复制到不存在的路径,它会正常工作)。
    【解决方案2】:

    如果有人提出同样的问题:

    for file in `find -type f`;
    do
    newFilename="${file#./}"
    dirName=$ENVIRONMENT-$(dirname "$newFilename")
    
    #get first part of dir (only root)
    dirName="${dirName%%/*}"
    
    echo bucket: $dirName
    
    if aws s3api head-bucket --bucket "$dirName" 2>/dev/null; then
       echo "bucket already exists"
    else
      if [[ $dirName == *"/"* ]]; then
         echo $dirName
         echo "This bucket is a subfolder and will not be created"
      else
         aws s3 mb s3://$dirName
      fi
    fi
    
    aws s3 cp $newFilename s3://$ENVIRONMENT-$newFilename
    done
    

    脚本检索它可以找到的所有文件; 然后它将检查根目录(相对于当前文件夹) 它将检查该目录作为存储桶存在。如果不;它将被创建。 然后每个文件都会被复制。

    由于我不知道根目录是否存在(作为存储桶),我们必须手动检查它。 我无法使用sync,因为我可能没有现有的存储桶。 如果您确实知道您的根目录作为存储桶存在;然后我会使用同步,一个班轮与 10 个班轮:see_no_evil:.

    不管怎样,我就是这样!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 2017-05-20
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      相关资源
      最近更新 更多