How to upload video to Amazon s3 as Multipart video upload iOS on v2.1 -
how upload video amazon s3 multipart upload method. if know or have ideas how solve problem please give me advise.
thanks lot time!
first use urlsession create aws object & handle delegate methods
- (void)initbackgroundurlsessionandaws:(nsstring*)mediaid { videoid= mediaid; awsstaticcredentialsprovider *credentialsprovider = [awsstaticcredentialsprovider credentialswithaccesskey:<your access key> secretkey:<your secret key>]; awsserviceconfiguration *configuration = [awsserviceconfiguration configurationwithregion:awsregionuseast1 credentialsprovider:credentialsprovider]; nsurlsessionconfiguration *urlconfiguration; if ([[[uidevice currentdevice] systemversion] compare:@"8.0" options:nsnumericsearch] == nsorderedascending) { // ios7 or earlier urlconfiguration = [nsurlsessionconfiguration backgroundsessionconfiguration:mediaid]; } else { // ios8 or later urlconfiguration = [nsurlsessionconfiguration backgroundsessionconfigurationwithidentifier:mediaid]; } self.urlsession = [nsurlsession sessionwithconfiguration:urlconfiguration delegate:self delegatequeue:nil]; [awsservicemanager defaultservicemanager].defaultserviceconfiguration = configuration; self.awss3 = [[awss3 alloc] initwithconfiguration:configuration]; nsurl *movieurl = [nsurl urlwithstring:[[nsuserdefaults standarduserdefaults] valueforkey:@"mediaurl"]]; nsstring *path = [nstemporarydirectory() stringbyappendingpathcomponent:@"video.mov"]; nsdata *imagedata = [nsdata datawithcontentsofurl:movieurl]; [imagedata writetofile:path atomically:yes]; nsurl *url = [[nsurl alloc] initfileurlwithpath:path]; awss3getpresignedurlrequest *getpresignedurlrequest = [awss3getpresignedurlrequest new]; getpresignedurlrequest.bucket = <bucket id>; getpresignedurlrequest.key = [nsstring stringwithformat:@"%@.mov",mediaid]; getpresignedurlrequest.httpmethod = awshttpmethodput; getpresignedurlrequest.expires = [nsdate datewithtimeintervalsincenow:3600]; //important: must set contenttype put request getpresignedurlrequest.contenttype = @"movie/mov"; [[[awss3presignedurlbuilder defaults3presignedurlbuilder] getpresignedurl:getpresignedurlrequest] continuewithblock:^id(bftask *task) { if (task.error) { nslog(@"error bftask: %@", task.error); [self showerror]; } else { // [svprogresshud dismiss]; nsurl *presignedurl = task.result; nslog(@"upload presignedurl is: \n%@", presignedurl); nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:presignedurl]; request.cachepolicy = nsurlrequestreloadignoringlocalcachedata; [request sethttpmethod:@"put"]; [request setvalue:@"movie/mov" forhttpheaderfield:@"content-type"]; nsurlsessionuploadtask *uploadtask = [self.urlsession uploadtaskwithrequest:request fromfile:url]; [uploadtask resume]; } return nil; }]; } - (void)urlsession:(nsurlsession *)session task:(nsurlsessiontask *)task didsendbodydata:(int64_t)bytessent totalbytessent:(int64_t)totalbytessent totalbytesexpectedtosend:(int64_t)totalbytesexpectedtosend { // upload progress nslog(@"progress : %f", (float) totalbytessent / totalbytesexpectedtosend); float progress = (float)( totalbytessent / totalbytesexpectedtosend); [svprogresshud showprogress:progress status:@"uploading video..." masktype:svprogresshudmasktypeblack]; } - (void)urlsession:(nsurlsession *)session task:(nsurlsessiontask *)task didcompletewitherror:(nserror *)error { if (error) { nslog(@"s3 uploadtask: %@ completed error: %@", task, [error localizeddescription]); [svprogresshud showerrorwithstatus:@"error in uploding."]; } else { nslog(@"s3 uploadtask: %@ completed @", task); [self preparevideopffile:videoid]; awss3getpresignedurlrequest not contain acl property, has set after file uploaded awss3putobjectaclrequest *aclrequest = [awss3putobjectaclrequest new]; aclrequest.bucket = @"your_bucket"; aclrequest.key = @"yout_key"; aclrequest.acl = <key>; [[self.awss3 putobjectacl:aclrequest] continuewithblock:^id(bftask *bftask) { dispatch_async(dispatch_get_main_queue(), ^{ if (bftask.error) { nslog(@"error putobjectacl: %@", [bftask.error localizeddescription]); } else { nslog(@"acl uploaded file changed successfully!"); } }); return nil; }]; } }
Comments
Post a Comment