我想使用预签名 URL 将文件从 iOS 应用程序上传到 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看有没有流量,流量很大。
我不知道我的代码有什么问题。文件传输似乎没有正确终止。