AWS S3:如何使用bash检查存储桶中是否存在文件 [英] AWS S3: How to check if a file exists in a bucket using bash
问题描述
我想知道是否可以检查某个存储桶中是否有某些文件.
I'd like to know if it's possible to check if there are certain files in a certain bucket.
这是我发现的东西:
它应该可以解决我的问题,但是由于某种原因,它会不断返回该文件不存在,而实际上却不存在.该解决方案也有点过时,并且不使用
doesObjectExist
方法.
这给出了如何使用此方法的语法,但我似乎无法使其正常工作.
This gives the syntax of how to use this method, but I can't seem to make it work. 他们是否希望您创建一个布尔变量来保存方法的状态?还是函数是否直接为您提供输出/抛出错误? Do they expect you to make a boolean variable to save the status of the method, or does the function directly give you an output / throw an error? 这是我当前在bash脚本中使用的代码: This is the code I'm currently using in my bash script: 我仅使用文件名而不是完整路径进行了测试.但是由于我得到的错误是语法错误,所以我可能只是在错误地使用它. I tested it using only the name of the file, instead of giving the full path. But since the error I'm getting is a syntax error, I'm probably just using it wrong. 希望有人可以帮助我,并告诉我我在做错什么. Hopefully someone can help me out and tell me what I'm doing wrong. !编辑 由于使用 I ended up looking for another way to do this since using 最后一次,我看到了性能比较 Last time I saw performance comparisons 返回: 这篇关于AWS S3:如何使用bash检查存储桶中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
existBool=doesObjectExist(${BucketName}, backup_${DomainName}_${CurrentDate}.zip)
if $existBool ; then
echo 'No worries, the file exists.'
fi
doesObjectExist
并不是最快或最简单的方法,所以我最终寻找了另一种方法.doesObjectExist
isn't the fastest or easiest.推荐答案
getObjectMetadata
是检查对象是否存在的最快方法.使用将 head-object
方法,例如:getObjectMetadata
was the fastest way to check if an object exists. Using the AWS cli that would be the head-object
method, example:aws s3api head-object --bucket www.codeengine.com --key index.html
{
"AcceptRanges": "bytes",
"ContentType": "text/html; charset=utf-8",
"LastModified": "Sun, 08 Jan 2017 22:49:19 GMT",
"ContentLength": 38106,
"ContentEncoding": "gzip",
"ETag": "\"bda80810592763dcaa8627d44c2bf8bb\"",
"StorageClass": "REDUCED_REDUNDANCY",
"CacheControl": "no-cache, no-store",
"Metadata": {}
}