Objective c 使用AFAmazonS3Client从Amazon S3下载的损坏文件
我创建了一个从AmazonS3下载plist文件的应用程序。我使用的是基于框架的客户端Objective c 使用AFAmazonS3Client从Amazon S3下载的损坏文件,objective-c,amazon-s3,plist,afnetworking-2,Objective C,Amazon S3,Plist,Afnetworking 2,我创建了一个从AmazonS3下载plist文件的应用程序。我使用的是基于框架的客户端 -(void) getFile:(NSString *)fileName{ self.s3Manager = [[AFAmazonS3Manager alloc] initWithAccessKeyID:@"..." secret:@"..."]; self.s3Manager.requestSerializer.region = AFAmazonS3SAEast1Region; se
-(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);
}];
}
然后我检查plist文件是否在文档文件夹中。的确如此。因此,我尝试打开plist文件,结果为零:
-(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"];
return listName;
}
所以我尝试手动添加plist文件。我下载并复制到documents文件夹,然后dictionaryWithContentsOfFile
可以打开该文件。因此,我假设当我使用下载文件时,plist文件已损坏
我做错了什么
更新1
我意识到我从S3下载的每个文件都已损坏。我不知道我是否以正确的方式处理NSOutputStream,或者是其他东西。请小心,因为在Xcode的最后一个版本中,每次在模拟器中重新启动应用程序时,由于某种原因,
AFAmazonS3Manager
中的getObjectWithPath
方法无法正常工作
因此,我直接从中使用AFHTTPRequestOperation
重写了我的方法
要进行调试,请将文件作为
NSString
和NSSLog
读取。如果问题不明显,请尝试使用应用程序验证plist。或者,如果你的问题不太长或不太机密,可以在问题中添加内容条。
- (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 as audio/mp4", fileName);
operation.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"audio/mp4"];
} else if ([[fileName pathExtension] isEqualToString:@"png"]) {
NSLog(@"%@ set as image/png", fileName);
operation.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"image/png"];
} else if ([[fileName pathExtension] isEqualToString:@"plist"]) {
NSLog(@"%@ set as 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];
}