我想使用预先签名的URL将文件从iOS App上传到AWS S3存储桶. URL是正确的,因为它在命令行上使用curl.
curl -v -k --upload-file FILENAME "https://MYBUCKET.amazonaws.com:443/KEYNAME?Signature=...&Expires=1391691489&AWSAccessKeyId=..."
使用以下Objective-C代码……
- (void)upload:(NSString *)url fileData:(NSData *)fileData { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"PUT"]; [request setHTTPBody:fileData]; [request setValue:[NSString stringWithFormat:@"%d",[fileData length]] forHTTPHeaderField:@"Content-Length"]; [request setValue:@"audio/mpeg" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"public-read" forHTTPHeaderField:@"x-amz-acl"]; [request setValue:@"iPhone-OS/6.0 fr_FR NE" forHTTPHeaderField:@"User-Agent"]; _connection = [NSURLConnection connectionWithRequest:request delegate:self]; [_connection start]; }
…我收到此错误:
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x9c49560 {NSErrorFailingURLStringKey=https://MYBUCKET.s3.amazonaws.com:443/KEYNAME?Signature=...&Expires=1391703958&AWSAccessKeyId=...,NSErrorFailingURLKey=https://MYBUCKET.amazonaws.com:443/KEYNAME?Signature=...&Expires=1391703958&AWSAccessKeyId=...,NSLocalizedDescription=The request timed out.,NSUnderlyingError=0x9c48c80 "The request timed out."}
我使用WireShark查看是否有任何流量,并且流量很大.
解决方法
我自己解决了这个问题Content-Type标头是罪魁祸首.在绝望中,我用一个非常小的文本文件测试了我的代码,并从S3获得了403作为HTTP状态代码.没有超时.这样的进步.我还收到了一条非常丰富的错误消息:
<?xml version="1.0" encoding="UTF-8"?> <Error> <Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>...</StringToSignBytes><RequestId>...</RequestId><HostId>...</HostId><SignatureProvided>...</SignatureProvided> <StringToSign>PUT text/plain 1391784394 KEYNAME</StringToSign> <AWSAccessKeyId>...</AWSAccessKeyId> </Error>
显然,如果从客户端提供内容类型字符串(在本例中为text / plain),则在字符串到符号中是预期的.不要问我为什么这会导致大(5.5MB?)文件超时.我希望这能为其他人节省几个小时的生命.
[request setValue:@"..." forHTTPHeaderField:@"Content-Type"];