2014-10-24 4 views
2

Я создал приложение, загружающее файл plist с Amazon S3. Я использовал AFAmazonS3Client клиент, основанный на базе AFNetworking.Поврежденные файлы, загруженные с Amazon S3 с использованием AFAmazonS3Client

-(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 вручную. Я загрузил и скопировал его в папку документов, а затем dictionaryWithContentsOfFile мог открыть файл. Поэтому я предполагаю, что файл plist был поврежден, когда я загружаю файл, используя AFAmazonS3Client.

Что я делаю неправильно?

Update 1

Я понимаю, что каждый файл, который я скачал с S3 повреждены. Я не знаю, обрабатываю ли я NSOutputStream правильно или, возможно, другое.

+0

Для отладки прочитать файл как '' NSString' и NSSLog' он. Если проблема не очевидна, попробуйте проверить plist с приложением. Или добавьте содержимое полосы в свой вопрос, если он не длинный или конфиденциальный. – zaph

ответ

2

По какой-то причине getObjectWithPath метод из 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]; 
} 
-1

Будьте осторожны причиной в последней версии Xcode, каждый раз, когда вы перезапустите приложение в симуляторе, папка документов удаляется

Смежные вопросы