Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exists in s3 using ls and wildcard

Seems so simple, but not getting the syntax right. I want to know if a file exists in my s3 bucket using wildcards. Something like

aws s3 ls s3://my-bucket/folder/*myfile* 

The goal is to see if a file called 2016_myfile.txt or a file called 2011_myfile.csv exists within this bucket.

If I run the command, it doesn't return anything even though I know this file exists there.

like image 650
Nicros Avatar asked Oct 04 '16 16:10

Nicros


People also ask

How do I find files on AWS S3?

Perhaps you're looking for something more complex, but if you landed here trying to figure out how to simply find an object (file) by it's title, it's crazy simple: open the bucket, select "none" on the right hand side, and start typing in the file name.

How do you check if S3 bucket exists or not?

To check whether a bucket already exists before attempting to create one with the same name, call the doesBucketExist method. It will return true if the bucket exists, and false otherwise. if (s3. doesBucketExistV2(bucket_name)) { System.

What is checksum in S3?

Amazon S3 uses checksum values to verify the integrity of data that you upload to or download from Amazon S3. In addition, you can request that another checksum value be calculated for any object that you store in Amazon S3.

What is prefix in S3 bucket?

A key prefix is a string of characters that can be the complete path in front of the object name (including the bucket name). For example, if an object (123. txt) is stored as BucketName/Project/WordFiles/123. txt, the prefix might be “BucketName/Project/WordFiles/123.


2 Answers

aws s3 ls does not support globs, but sync does and has a dry run mode. So if you do this (from an empty directory) you should get the results you want:

aws s3 sync s3://my-bucket . --exclude "*" --include "folder/*myfile*" --dryrun 

It will produce lines like this for matching files:

(dryrun) download s3://my-bucket/folder/myfile.txt to folder/myfile.txt (dryrun) download s3://my-bucket/folder/_myfile-foo.xml to folder/_myfile-foo.xml 
like image 139
Chrs Avatar answered Sep 19 '22 16:09

Chrs


(re-drafted from comment as it appears this answered the question)

I myself tried, and failed to use wildcards in the aws-cli, and according to the docs, this is not currently supported. Simplest (though least efficient) solution would be to use grep:

aws s3 ls s3://my-bucket/folder/ | grep myfile 

Alternatively, you could write a short python/other script to do this more efficiently (but not in a single command)

like image 40
Gil Adirim Avatar answered Sep 17 '22 16:09

Gil Adirim