У меня есть приложение, которое в настоящее время загружает изображения на амазонку S3. Я пытался переключить его с NSURLConnection на NSURLSession, чтобы загрузка могла продолжаться, пока приложение находится в фоновом режиме! Кажется, я немного задеваю проблему. NSURLRequest создается и передается NSURLSession, но amazon отправляет обратно 403-запрещенный ответ, если я передаю тот же запрос NSURLConnection, он полностью загружает файл.
Вот код, который создает ответ:
NSString *requestURLString = [NSString stringWithFormat:@"http://%@.%@/%@/%@", BUCKET_NAME, AWS_HOST, DIRECTORY_NAME, filename];
NSURL *requestURL = [NSURL URLWithString:requestURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:60.0];
// Configure request
[request setHTTPMethod:@"PUT"];
[request setValue:[NSString stringWithFormat:@"%@.%@", BUCKET_NAME, AWS_HOST] forHTTPHeaderField:@"Host"];
[request setValue:[self formattedDateString] forHTTPHeaderField:@"Date"];
[request setValue:@"public-read" forHTTPHeaderField:@"x-amz-acl"];
[request setHTTPBody:imageData];
И затем это знаменует ответ (я думаю, что это произошло из другого ответа SO):
NSString *contentMd5 = [request valueForHTTPHeaderField:@"Content-MD5"];
NSString *contentType = [request valueForHTTPHeaderField:@"Content-Type"];
NSString *timestamp = [request valueForHTTPHeaderField:@"Date"];
if (nil == contentMd5) contentMd5 = @"";
if (nil == contentType) contentType = @"";
NSMutableString *canonicalizedAmzHeaders = [NSMutableString string];
NSArray *sortedHeaders = [[[request allHTTPHeaderFields] allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (id key in sortedHeaders)
{
NSString *keyName = [(NSString *)key lowercaseString];
if ([keyName hasPrefix:@"x-amz-"]){
[canonicalizedAmzHeaders appendFormat:@"%@:%@\n", keyName, [request valueForHTTPHeaderField:(NSString *)key]];
}
}
NSString *bucket = @"";
NSString *path = request.URL.path;
NSString *query = request.URL.query;
NSString *host = [request valueForHTTPHeaderField:@"Host"];
if (![host isEqualToString:@"s3.amazonaws.com"]) {
bucket = [host substringToIndex:[host rangeOfString:@".s3.amazonaws.com"].location];
}
NSString* canonicalizedResource;
if (nil == path || path.length < 1) {
if ( nil == bucket || bucket.length < 1 ) {
canonicalizedResource = @"/";
}
else {
canonicalizedResource = [NSString stringWithFormat:@"/%@/", bucket];
}
}
else {
canonicalizedResource = [NSString stringWithFormat:@"/%@%@", bucket, path];
}
if (query != nil && [query length] > 0) {
canonicalizedResource = [canonicalizedResource stringByAppendingFormat:@"?%@", query];
}
NSString* stringToSign = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@%@", [request HTTPMethod], contentMd5, contentType, timestamp, canonicalizedAmzHeaders, canonicalizedResource];
NSString *signature = [self signatureForString:stringToSign];
[request setValue:[NSString stringWithFormat:@"AWS %@:%@", self.S3AccessKey, signature] forHTTPHeaderField:@"Authorization"];
Тогда, если я использую эту строку кода:
[NSURLConnection connectionWithRequest:request delegate:self];
Он работает и загружает файл, но если я использую:
NSURLSessionUploadTask *task = [self.session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:filePath]];
[task resume];
Я получаю запрещенную ошибку..!?
Кто-нибудь пытался загрузить на S3 с этим и ударил аналогичные проблемы? Интересно, связано ли это с тем, как сеанс приостанавливает и возобновляет загрузку, или он делает что-то смешное для запроса.?
Одним из возможных решений было бы загрузить файл на промежуточный сервер, который я контролирую, и передать его на S3, когда он будет завершен... но это явно не идеальное решение!
Любая помощь очень ценится!
Спасибо!