简体   繁体   中英

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];

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];
AWSS3ListObjectsRequest *listObjectsRequest = [AWSS3ListObjectsRequest new];
    listObjectsRequest.bucket = @"YourBucketName";

[[s3 listObjects:listObjectsRequest] continueWithBlock:^id(AWSTask *task)
 {
    if (task.error)
    {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        NSLog(@"listObjects failed: [%@]", task.error);
    }
    else
    {
        AWSS3ListObjectsOutput *listObjectsOutput = task.result;

        for (AWSS3Object *s3Object in listObjectsOutput.contents)
        {
            downloadingFilePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:@"download"] stringByAppendingPathComponent:s3Object.key];

            NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];

            AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
            downloadRequest.bucket = @"YourBucketName";
            downloadRequest.key = s3Object.key;
            downloadRequest.downloadingFileURL = downloadingFileURL;

            AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
            [[transferManager download:downloadRequest] continueWithBlock:^id(AWSTask *task)
             {
                 if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]
                     && task.error.code == AWSS3TransferManagerErrorPaused)
                 {
                     NSLog(@"Download paused.");
                 }
                 else if (task.error)
                 {
                     NSLog(@"Upload failed: [%@]", task.error);
                 }
                 else
                 {
                     dispatch_async(dispatch_get_main_queue(), ^
                                    {
                                        //_imgNew.image = [ UIImage imageNamed:[NSString stringWithFormat:@"%@",downloadRequest.downloadingFileURL]];
                                    });
                 }
                 return nil;
             }];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [self setUpScrollViews];
            NSLog(@"Set images called");
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        });
    }
    return nil;
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM