objective c - Corrupted files downloaded from Amazon S3 using AFAmazonS3Client -
i created app download plist file amazon s3. using afamazons3client client based in afnetworking framework.
-(void) getfile:(nsstring *)filename{ self.s3manager = [[afamazons3manager alloc] initwithaccesskeyid:@"..." secret:@"..."]; self.s3manager.requestserializer.region = afamazons3saeast1region; self.s3manager.requestserializer.bucket = @"verba"; nsstring* documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; documentspath = [documentspath stringbyappendingpathcomponent:filename]; nsoutputstream *stream = [[nsoutputstream alloc] inittofileatpath:documentspath append:no]; [self.s3manager getobjectwithpath:@"" outputstream:stream progress:^(nsuinteger bytesread, long long totalbytesread, long long totalbytesexpectedtoread) { nslog(@"%f%% downloaded", (totalbytesread / (totalbytesexpectedtoread * 1.0f) * 100)); } success:^(id responseobject) { nslog(@"download complete"); } failure:^(nserror *error) { nslog(@"error: %@", error); }]; }
then checked if plist file in document folder. , was. tried open plist file , result nil:
-(nsstring*) loadlistname:(nsstring*)filename{ nsstring* documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring* filepath = [documentspath stringbyappendingpathcomponent:filename]; nsdictionary *temp; if ([[nsfilemanager defaultmanager] fileexistsatpath: filepath]){ temp = [nsdictionary dictionarywithcontentsoffile:filepath]; } else { nslog(@"file not found."); } nsstring *listname = [temp objectforkey:@"name"]; homecoming listname; }
so tried add together plist file manually. downloaded , copied documents folder , dictionarywithcontentsoffile
open file. suppose plist file corrupted when download file using afamazons3client.
what doing wrong ?
update 1
i realize every single file downloaded s3 corrupted. don't know if handle nsoutputstream in right way or maybe stuff.
for reason getobjectwithpath
method afamazons3manager
not working properly.
so rewrite method using afhttprequestoperation
straight afnetworking
- (void)downloadfile:(nsstring *)filename block:(void (^)(nserror *error))block { nsstring *urlstring = @"https://[bucket].[server area].amazonaws.com/"; urlstring = [urlstring stringbyappendingpathcomponent:filename]; nsurl *url = [nsurl urlwithstring:urlstring]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request]; nsset *set = operation.responseserializer.acceptablecontenttypes; if ([[filename pathextension] isequaltostring:@"m4a"]) { nslog(@"%@ set audio/mp4", filename); operation.responseserializer.acceptablecontenttypes = [set setbyaddingobject:@"audio/mp4"]; } else if ([[filename pathextension] isequaltostring:@"png"]) { nslog(@"%@ set image/png", filename); operation.responseserializer.acceptablecontenttypes = [set setbyaddingobject:@"image/png"]; } else if ([[filename pathextension] isequaltostring:@"plist"]) { nslog(@"%@ set application/x-plist", filename); operation.responseserializer.acceptablecontenttypes = [set setbyaddingobject:@"application/x-plist"]; } nsstring* documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *fullpath = [documentspath stringbyappendingpathcomponent:[url lastpathcomponent]]; [operation setoutputstream:[nsoutputstream outputstreamtofileatpath:fullpath append:no]]; [operation setdownloadprogressblock:^(nsuinteger bytesread, long long totalbytesread, long long totalbytesexpectedtoread) { nslog(@"bytesread: %lu, totalbytesread: %lld, totalbytesexpectedtoread: %lld", (unsigned long)bytesread, totalbytesread, totalbytesexpectedtoread); }]; [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { if (block) { block(nil); } nslog(@"res: %@", [[[operation response] allheaderfields] description]); nserror *error; nsdictionary *fileattributes = [[nsfilemanager defaultmanager] attributesofitematpath:fullpath error:&error]; if (error) { nslog(@"err: %@", [error description]); } else { nsnumber *filesizenumber = [fileattributes objectforkey:nsfilesize]; long long filesize = [filesizenumber longlongvalue]; nslog(@"%lld", filesize); } } failure:^(afhttprequestoperation *operation, nserror *error) { if (block) { block(error); } nslog(@"err: %@", [error description]); }]; [operation start]; }
objective-c amazon-s3 plist afnetworking-2
No comments:
Post a Comment