我正在使用AWS IOS SDK从S3下载文件,并且无法在S3存储桶的特定文件夹中列出对象。我可以使用listObjectsInBucket方法列出整个存储桶的所有文件,但我只需要列出存储桶中特定文件夹中的文件。
所以我尝试使用listObjects方法并指定存储桶名称和前缀(表示S3上的文件夹名称)。
但以下代码无效。
S3ListObjectsRequest *lor = [S3ListObjectsRequest alloc];
lor.bucket = @"bucketName";
lor.prefix = @"/folderName1/foldername2";
S3ListObjectsResponse *ListObjectResponse = [self.s3 listObjects:lor];
答案 0 :(得分:1)
只是不要把“/”放在folderName1的前面,事情就会解决。
夫特:
let listObjectsRequest = AWSS3ListObjectsRequest()
listObjectsRequest.bucket = "(your bucket name)"
listObjectsRequest.prefix = "(subfolder1)/(subfolder2)"
s3.listObjects(listObjectsRequest).continueWithBlock { (task) -> AnyObject! in .......
目标-C:
S3ListObjectsRequest *lor = [S3ListObjectsRequest alloc];
lor.bucket = @"bucketName";
lor.prefix = @"folderName1/foldername2";
S3ListObjectsResponse *ListObjectResponse = [self.s3 listObjects:lor];
答案 1 :(得分:-1)
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;
}];