问题
I'm using this code to upload an image to S3
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = @"my-photo-bucket";
uploadRequest.key = @"test_upload";
long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.imageUrl.path error:nil][NSFileSize] longLongValue];
uploadRequest.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];
uploadRequest.body = self.imageUrl.absoluteURL;
[[transferManager upload:uploadRequest] continueWithBlock:^id(BFTask *task) {
NSLog(@"%@", task.error);
return nil;
}];
When I try to upload the file, it fails because the URL doesn't seem to point to something my app can access:
2014-09-08 11:57:47.014 myapp[1551:60b] Url: assets-library://asset/asset.JPG?id=721E68A8-DF94-4404-A37D-FECDCDC60C1D&ext=JPG, File Size: (null)
2014-09-08 11:57:47.025 myapp[1551:60b] Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x17827c2c0 {NSFilePath=/asset.JPG, NSUnderlyingError=0x178256e60 "The operation couldn’t be completed. No such file or directory"}
It seems like a valid URL to me. What am I doing wrong?
回答1:
It looks like this is currently not supported by the AWS-SDK-IOS project. See the github bug report.
Their suggested work around is to copy the assets to your app directory before initiating uploads.
回答2:
This working perfectly with me, check it.
//Configuration
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
initWithRegionType:AWSRegionEUWest1
identityPoolId:CognitoIdentityPoolId];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType
credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
//Create temporary directory
NSError *error = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"upload"]
withIntermediateDirectories:YES
attributes:nil
error:&error]) {
NSLog(@"reading 'upload' directory failed: [%@]", error);
}
//write the image to the created directory
UIImage *image = [your image]; //Check below how do I get it
NSString *fileName = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingString:@".jpg"];
NSString *filePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:@"upload"] stringByAppendingPathComponent:fileName];
NSData * imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
//Create upload request
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.body = [NSURL fileURLWithPath:filePath];
uploadRequest.key = [NSString stringWithFormat:@"%@", fileName]; //You can add you custom path here. Example: [NSString stringWithFormat:@"public/myImages/%@", fileName];
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
uploadRequest.bucket = S3BucketName;
uploadRequest.body = [NSURL fileURLWithPath:filePath];
[[transferManager upload:uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock:^id(BFTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;
default:
NSLog(@"Error: %@", task.error);
break;
}
} else {
// Unknown error.
NSLog(@"Error: %@", task.error);
}
}
if (task.result) {
// The file uploaded successfully.
}
return nil;
}];
}
And here is how to get the image form the saved photos using NSURL:
NSURL* imageURL = [NSURL URLWithString:urlString];
ALAssetsLibrary * _library = [[ALAssetsLibrary alloc] init];
[_library assetForURL:imageURL resultBlock:^(ALAsset *asset) {
UIImage *bigImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
}
failureBlock:^(NSError *error)
{
// error handling
NSLog(@"failure-----");
}];
来源:https://stackoverflow.com/questions/25731267/proper-way-to-access-image-in-saved-photos-using-nsurl-and-upload-to-s3