问题
I'm trying to fetch the files from S3.
As usual, my files are organised in the bucket as below:
bucket/<prefixes>/files
I want to retrieve files having specific file names
E.g.:
- Files starting with a given text (myfile-*)
- Files having specific extensions (*.csv)
- File names having specific text (text)
For the first scenario, I can get away using prefix as below:
ObjectListing objectListing = s3Client.listObjects(bucketName, "test/myfile");
But for the other 2 cases, I'm not able to find any solution.
I tried using wildcards as below:
ObjectListing objectListing = s3Client.listObjects(bucketName, "test/*.csv");
But it didn't return any result.
Is there any workaround for this (other than getting the list of keys and filtering within my code)??
Any help is appreciated.
回答1:
Your use-case is pretty common.
I am unaware of any S3 API that allows you to do anything other than prefix-matching.
The way to deal with this is to maintain a separate, searchable copy of the file names (and perhaps other metadata). In other words, have a row in a table in a relational database for each file. Or you might maintain an "index" file that lists all of the file names.
If the file creation/deletion is performed through your code, you can add the required code to keep the file list in sync with your bucket. If not, then you can use S3 event notifications to drive the file list updates.
When you want to search by file names, you look in the file list or database table to get the matching file names. Then hit S3 to do whatever operation is required.
回答2:
You cannot do it, there is no wildcards in S3, and a file can even contain the * character
来源:https://stackoverflow.com/questions/45240056/aws-s3-get-keys-containing-text