我创建了一个可从Amazon S3下载plist文件的应用程序。我用的是AFAmazonS3Client基于客户端AFNetworking框架。
-(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文件,结果为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"];
return listName;
}
因此,我尝试手动添加plist文件。我下载并将其复制到documents文件夹,然后dictionaryWithContentsOfFile
可以打开文件。所以我想当我使用AFAmazonS3Client下载文件时plist文件已损坏。
我做错了什么?
更新1
我意识到从S3下载的每个文件都已损坏。我不知道我是否以正确的方式或其他方法处理NSOutputStream。
由于某种原因,getObjectWithPath
from的AFAmazonS3Manager
方法无法正常工作。
所以我AFHTTPRequestOperation
直接使用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 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];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句