Salman Shaikh
Salman Shaikh

Reputation: 595

S3 Download from a specific folder

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

Upvotes: 1

Views: 2522

Answers (2)

Sagar Chilukuri
Sagar Chilukuri

Reputation: 1448

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

Upvotes: 0

Frederic Henri
Frederic Henri

Reputation: 53703

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> + "/"

Upvotes: 4

Related Questions