Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3: List Objects from a specific S3 folder?

I am using the AWS IOS SDK to download files from S3 and am having trouble listing objects in a specific folder of an S3 bucket. I can list all the files of the ENTIRE bucket using the listObjectsInBucket method but I need to list only the files in a specific folder within a bucket.

So I am trying to use the listObjects method and specify a bucket name and prefix (indicating a folder name on S3).

But the following code is not working.

S3ListObjectsRequest *lor = [S3ListObjectsRequest alloc];
lor.bucket = @"bucketName";
lor.prefix = @"/folderName1/foldername2";

S3ListObjectsResponse *ListObjectResponse = [self.s3 listObjects:lor];
like image 563
user3634654 Avatar asked May 14 '14 00:05

user3634654


1 Answers

Just simply DO NOT put "/" in front of folderName1, and things will work out.

Swift:

let listObjectsRequest = AWSS3ListObjectsRequest()
    listObjectsRequest.bucket = "(your bucket name)"
    listObjectsRequest.prefix = "(subfolder1)/(subfolder2)" 
    s3.listObjects(listObjectsRequest).continueWithBlock { (task) -> AnyObject! in .......

Objective-C:

S3ListObjectsRequest *lor = [S3ListObjectsRequest alloc];
lor.bucket = @"bucketName";
lor.prefix = @"folderName1/foldername2"; 

S3ListObjectsResponse *ListObjectResponse = [self.s3 listObjects:lor];
like image 142
Sky Avatar answered Oct 10 '22 19:10

Sky