AWS S3:如何使用bash检查存储桶中是否存在文件 [英] AWS S3: How to check if a file exists in a bucket using bash

查看:1455
本文介绍了AWS S3:如何使用bash检查存储桶中是否存在文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以检查某个存储桶中是否有某些文件.

I'd like to know if it's possible to check if there are certain files in a certain bucket.

这是我发现的东西:

检查文件是否在s3cmd的S3存储桶中

它应该可以解决我的问题,但是由于某种原因,它会不断返回该文件不存在,而实际上却不存在.该解决方案也有点过时,并且不使用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:

existBool=doesObjectExist(${BucketName}, backup_${DomainName}_${CurrentDate}.zip)

if $existBool ; then
        echo 'No worries, the file exists.'
fi

我仅使用文件名而不是完整路径进行了测试.但是由于我得到的错误是语法错误,所以我可能只是在错误地使用它.

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.

!编辑

由于使用doesObjectExist并不是最快或最简单的方法,所以我最终寻找了另一种方法.

I ended up looking for another way to do this since using doesObjectExist isn't the fastest or easiest.

推荐答案

最后一次,我看到了性能比较getObjectMetadata是检查对象是否存在的最快方法.使用将 head-object 方法,例如:

Last time I saw performance comparisons 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": {}
}

这篇关于AWS S3:如何使用bash检查存储桶中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆