Check if file exists in s3 using ls and wildcard

烈酒焚心 提交于 2019-12-21 03:52:22

问题


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.


回答1:


(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)




回答2:


S3 doesn't support wildcard listing. You need to list all the files and grep it.

aws s3 ls s3://mybucket/folder --recursive 

Above command will give the list of files under your folder, it searches the files inside the folder as well. Just grep your file name

aws s3 ls s3://mybucket/folder --recursive |grep filename

Suppose if you want to find multiple files, create a regular expression of those and grep it.




回答3:


s3cmd also works for me with grep.

s3cmd ls --recursive s3://mybucket/folder/folder2 | grep filename



回答4:


For Windows User:

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


来源:https://stackoverflow.com/questions/39857802/check-if-file-exists-in-s3-using-ls-and-wildcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!