S3 Download from a specific folder

2019-08-15 01:54发布

问题:

I have two folders inside my s3 bucket. How can I download all the files only from only bucket_one.

This is the code I am using to download from bucket. Now I want to download from a folder inside that bucket.

GetObjectRequest request = new GetObjectRequest(BucketName(),KeyName());
       S3Object object = s3Client.getObject(request);
       S3ObjectInputStream objectContent = object.getObjectContent();
       IOUtils.copy(objectContent, new FileOutputStream(System.getProperty("user.home")+"//downloads//"+KeyName()));

Thanks

回答1:

Just indicate the full path of the folder in question

Change your BucketName() method to be something like folderName() which will be bucketName() + "/" + <yourfolder> + "/"

the "/" is important at the end otherwise S3 will search for a file within your bucket root folder

You can use the same to go deeper in your folder structure : bucketName() + "/" + <folder1> + "/" + <folder2> + "/"



回答2:

The above answer provided by Frederic Henri is correct but you should not include / if yourFolder is the last folder .i.e, if directory is <yourBucket>/<lvlOne>/<lvlTwo>/, then at the end of folder lvlTwo, you should not include \

You can list all files in the bucket of a particular folder and download them individually or you may take a loot at MultipleFileDownload interface in the TransferManager class