Uploading images to s3 bucket and then uploading images' links to my server in background

307 views Asked by At

I have a generic question, experienced developers may laugh at me when they read that but I couldn't manage it somehow.

Basically, I want to upload images from filesystem to my amazon s3 bucket and after that upload links of these images to my server. After I upload images to s3, I got links of them and store them in an array. Then I want to upload those links to my server. But, in order to cover uploading against application suspending I want to make this process run in background. The schema is below.

Upload images to s3 -> get s3 links of these images -> upload links to server

I'm trying to use presignedURL utility of Amazon SDK since Amazon recommends to use it for background uploading.

iOS API docs says NSURLSessionUploadTask does what I'm trying to do. Firstly, I couldn't manage to upload images to s3 with presignedURL (I managed to upload with TransferManagerUploadRequest). Secondly, this process should be running even if application is suspended. But I don't know how to do that actually. I put my "upload-to-s3" code below which is not working right now.

Sorry for the complicated question, it indeed shows my mind right now.

 NSString *fileContentTypeStr = @"image/png";
AWSS3GetPreSignedURLRequest *urlRequest = [AWSS3GetPreSignedURLRequest new];
urlRequest.bucket = @"bounty-app";
urlRequest.key =[NSString stringWithFormat:@"submissions/photos/%@",nameOfImage];
urlRequest.HTTPMethod = AWSHTTPMethodPUT;
urlRequest.expires = [NSDate dateWithTimeIntervalSinceNow:3600];
urlRequest.contentType = fileContentTypeStr;
[[[AWSS3PreSignedURLBuilder defaultS3PreSignedURLBuilder] getPreSignedURL:urlRequest]
 continueWithBlock:^id(BFTask *task) {

     if (task.error) {
         NSLog(@"Error: %@",task.error);
     } else {
         NSURL *presignedURL = task.result;
         NSLog(@"upload presignedURL is: \n%@", presignedURL);

         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:presignedURL];
         request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
         [request setHTTPMethod:@"PUT"];
         [request setValue:fileContentTypeStr forHTTPHeaderField:@"Content-Type"];

         NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"unique_name_of_tttask"];
         NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
         NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:savedPath];

         [uploadTask resume];
     }         
     return nil;
 }];
0

There are 0 answers