Correct way to access images in saved photos using NSURL and upload to S3?

I am using this code to upload 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 download the file, it fails because the URL does not indicate what my application 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"} 

      

The url seems to be correct to me. What am I doing wrong?

+3


source to share


2 answers


It looks like this project is currently not supported by the AWS-SDK-IOS project. See github bug report .



Their suggested job is to copy the assets into the application directory before starting the download.

+1


source


This works great with me, check it out.

//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's how to get an image from 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-----");
     }];

      

0


source







All Articles