简体   繁体   English

仅在存储桶存在时才从AWS S3存储桶复制文件

[英]Copy files from AWS S3 Bucket only if the bucket exists

I am trying to create a single command through AWS CLI, which will check if the provided bucket exists and if there are files present inside the bucket, then copy those files in local machine. 我正在尝试通过AWS CLI创建一个命令,该命令将检查提供的存储桶是否存在以及存储桶中是否存在文件,然后将这些文件复制到本地计算机中。

So my requirement is: there is a s3 bucket 'ecs-ref-arch' and this bucket contains a folder called 'jars'. 所以我的要求是:有一个s3存储桶“ ecs-ref-arch”,该存储桶包含一个名为“ jars”的文件夹。 So if /ecs-ref-arch/jars is present and there are files inside this directory, then only I want to copy all files from jars directory to my local machine. 因此,如果存在/ ecs-ref-arch / jars并且此目录中有文件,则只有我想将jars目录中的所有文件复制到本地计算机。

Can this be done in single AWS CLI command? 可以在单个AWS CLI命令中完成吗?

Can this be done in single AWS CLI command ? 可以在单个AWS CLI命令中完成吗?

First, it sounds a lot like 'do the job for me', you did not show a lot of research from your side, although you might have searched, its not clear where you struggle. 首先,这听起来很像“为我做这份工作”,尽管您可能已经进行了搜索,但您并未从自己的角度进行大量研究,但不清楚您在哪里挣扎。

On the single command I am not sure, I am a big fan of xargs and so on, but I think you need at least 2 commands. 对于单个命令,我不确定,我是xargs忠实xargs ,等等,但我认为您至少需要2条命令。 AWS provides some methods to check existence of a bucket ( aws s3api head-bucket --bucket <bucket_name> ) but its not convenient to search for a folder AWS提供了一些检查存储桶是否存在的方法( aws s3api head-bucket --bucket <bucket_name> ),但是搜索文件夹不方便

so the best is to check this in script and run all commands from there, such script could be written as 所以最好的办法是在脚本中检查并从那里运行所有命令,这样的脚本可以写成

#!/bin/bash

CMD="aws s3 ls s3://<bucket-name>"

if eval "$CMD" | grep -q 'PRE jars/'; then
  # jars folder is found, can copy files locally
  aws s3 sync s3://<bucket-name>/jars .
fi

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

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