AWSS3TransferManager NSCocoaErrorDomain代码= 2错误

时间:2015-01-20 08:58:11

标签: ios amazon-s3 nsfilemanager

我的代码出现以下奇怪错误。 NSFilemanager中的文件路径是否有限制?真正奇怪的是,通过AWS S3上传时,此路径可以正常工作

AWSURLSessionManager URLSession:dataTask:didReceiveResponse:completionHandler:] |错误:无法使用文件路径创建文件:/profilePicture12.jpeg

    AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:AWS_Access_Key secretKey:AWS_Secret_Key];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSWest1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
self.downloadRequest = [AWSS3TransferManagerDownloadRequest new];

//bucket and image name
self.downloadRequest.bucket = AWS_Bucket_Name;
self.downloadRequest.key = S3_Link;


//unique customer path to store download
NSString* uniqueCustomerPath = [NSString stringWithFormat:@"profilePicture%ld.jpeg", (long)self.current_customer.id];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString  *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:uniqueCustomerPath];
self.downloadRequest.downloadingFileURL = [NSURL fileURLWithPath:uniqueCustomerPath];

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[transferManager download:self.downloadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task){
    if (task.error != nil) {
        NSLog(@"Error: [%@]", task.error);}

    else {
      ///do stuff
    }
    return nil;
}];

1 个答案:

答案 0 :(得分:0)

以下代码剪辑适用于我。我怀疑你正在尝试将文件写入不可写路径。另一个猜测是S3_Link设置为关键。如果它是完整的S3路径,它可能无法工作。在那里尝试文件名。

NSURL *bucketFileURL = [NSURL URLWithString:url];
NSString *key = [bucketFileURL lastPathComponent];
NSString *downloadingFilePath = [DOCUMENTS_DIRECTORY stringByAppendingPathComponent:key];

NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];

AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
downloadRequest.bucket = @"my-bucket-name";
downloadRequest.key = key;
downloadRequest.downloadingFileURL = downloadingFileURL;

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[transferManager download:downloadRequest] continueWithBlock:^id(BFTask *task) {

     if (task.result) {
         UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:downloadRequest.downloadingFileURL]];
         callback(img);
     } else if (task.error) {
         NSLog(@"Download Error: %@", task.error);
     }

     return nil;
 }];