Sagar Chilukuri
Sagar Chilukuri

Reputation: 1448

Download content of a specific folder from AWS

I am new to AWS and facing difficulty downloading files from a specific folder in AWS. I know there is no folder concept in AWS.

I am able to download file which is there in root folder of the bucket by below code but not with in any specific folder:

String fileName = "savings.pdf";
private static String bucketName = "XXXXXX.statement";
S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketName.concat("APL420/"), fileName));
final BufferedInputStream i = new BufferedInputStream(fetchFile.getObjectContent());
InputStream objectData = fetchFile.getObjectContent();
Files.copy(objectData, new File(fileName).toPath()); 
objectData.close();

But not able to download all the contents of a folder in the bucket. Please suggest a proper way.

I tried this answer but appending anything to bucketName is not notworking:

S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketName + "APL420/", fileName));

OR

S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketName + "/APL420/", fileName));

is not working.

Upvotes: 2

Views: 3855

Answers (2)

Abhilekh Singh
Abhilekh Singh

Reputation: 2963

Please don't use slash at the end then it will work if key exist. S3 service append the slash by default.

Edited the solution as per @Michael comment. Using prefix in filename is the correct solution. Not in the bucket name.

S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketName, "APL420/" + fileName));

Adding a snippet from S3RequestEndpointResolver:

private String getPathStyleResourcePath() {
  return bucketName + "/" + (key != null ? key : "");
}

Upvotes: 2

Khalid T.
Khalid T.

Reputation: 10567

getObject gets a single object stored in Amazon S3 under the specified bucket and key. Returns null if the specified constraints weren't met.

You might want to take a look at the MultipleFileDownload interface in the TransferManager class instead.

Upvotes: 0

Related Questions