AWS S3: how to check if a file exists in a bucket using bash

I would like to know if it is possible to check if there are certain files in a specific bucket.

Here is what I found:

Checking for a file in an S3 bucket using s3cmd

It should fix my problem, but for some reason, it keeps returning that the file does not exist while it does. This solution is also a bit outdated and does not use the method doesObjectExist.

Summary of all the methods that can be used in the Amazon S3 web service

This gives the syntax for using this method, but I cannot get it to work.

Do they expect that you should make a boolean variable to save the status of the method, or , does the function directly give you an output / give an error?

This is the code I use in my bash script:

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

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

, , , . , , , , , .

, - , .

!

, doesObjectExist .

+4
2

getObjectMetadata - , . AWS cli, head-object, :

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": {}
}
+2

- aws s3 ls

exists=$(aws s3 ls $path_to_file)
if [ -z "$exists" ]; then
  echo "it does not exist"
else
  echo "it exists"
fi
+5

Source: https://habr.com/ru/post/1667865/


All Articles