观点 :
- 我正在使用我的 iOS 应用程序中的 Amazon Pre-Signed URL 上传图像
- 现在的问题是我每次上传时都会收到状态码 400
- 如果运行良好,它应该给出 204 状态码
- 预签名 URL 在 Postman 上测试时效果很好,但在 App 端不起作用。
这是实现:
- (void)doUploadURL {
UIImage *myimage = [UIImage imageNamed:@"testimage.png"];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSString *boundary = @"---011000010111000001101001";
NSMutableData *body = [NSMutableData data];
// AcL parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAcL dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Content-Type parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strContentType dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// key parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// x-amz-meta-filename parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"x-amz-meta-filename\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strMetaFilename dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Credential parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Credential\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strCredential dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Algorithm parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Algorithm\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAlgorithm dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Date parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Date\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAmzDate dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Policy parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Policy\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strPolicy dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Signature parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Signature\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strSignature dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// file
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"file\"; filename=\"testimage.png\"\\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *headers = @{ @"content-type": @"multipart/form-data"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://visual-showcase.s3-ap-southeast-2.amazonaws.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3600.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@" Final Response : %d",httpResponse.statusCode);
if (error) {
NSLog(@"Error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Failed to upload" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
return;
});
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *err = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}];
[dataTask resume];
}
响应代码:
<NSHTTPURLResponse: 0x7d10e2c0> { status code: 400, headers {
Connection = close;
"Content-Type" = "application/xml";
Date = "Thu, 02 Feb 2017 07:20:18 GMT";
Server = AmazonS3;
"Transfer-Encoding" = Identity;
"x-amz-id-2" = "HIFhwBaj/ejjGoCgD2vacG2AtoxEFDXxSr6eulZtzUbuLoTcLT+AA9k6093Jyv5OIuK2wpi7+9c=";
"x-amz-request-id" = C221CD8DA2D80BCD;
} }
问题 :
- 我的问题是,在使用 Amazon Pre-Signed URL 上传文件时我们需要设置什么吗?或者我们可以像普通的 API 调用一样实现它?
- 据我所知,它应该像普通 API 一样以类似的方式实现
- 请让我得到任何提示,或者如果我在请求格式中做错了什么
提前致谢。