在过去的一周里,我一直在努力让它发挥作用。我正在尝试做的是将图像上传到 S3 服务,作为我正在使用的这个 API 用于上传图像的非常hacky方式的一部分。API 记录在这里:http ://www.thingiverse.com/developers/upload-guide 。我在第 2 步遇到问题。他们希望您上传到 S3,然后告诉他们的其他服务器该文件已上传到 S3,而不是为您完成。无论如何,这是我用来上传图片的代码:
+ (NSString *)uploadPictureWithParameters:(NSDictionary *)parameters toURL:(NSString *)actionURL withImage:(UIImage *)sourceImage {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSString *boundaryConstant = @"-------------------------acebdf13572468";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSLog(@"%@", [parameters allKeys]);
if ([parameters objectForKey:@"AWSAccessKeyId"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"AWSAccessKeyId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"AWSAccessKeyId"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"bucket"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"bucket\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"bucket"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"key"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"key"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"acl"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"acl"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"success_action_redirect"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"success_action_redirect\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"success_action_redirect"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"policy"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"policy\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"policy"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"signature"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"signature\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"signature"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"Content-Type"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:@"Content-Type"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:@"Content-Disposition"]) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Content-Disposition\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-disposition: attachment; filename=photo.png\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
// This would put them in the wrong order:
/*for (NSString *param in [parameters allKeys]) {
[body appendData:[[NSString stringWithFormat:@"---------------------------%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}*/
NSLog(@"%d", body.length);
UIImage *resizedImage = [sourceImage resizedImageToFitInSize:CGSizeMake(700, 700) scaleIfSmaller:NO];
NSData *imageData = UIImagePNGRepresentation(resizedImage);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"photo.png\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *requestBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(@"Request Body: %@", requestBody);
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%d", body.length);
NSString *requestBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(@"Request Body After Image: %@", requestBody);
[request setHTTPBody:body];
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSString * urlString = [NSString stringWithFormat:@"%@", actionURL];
NSLog(@"%@", urlString);
NSURL *aUrl = [NSURL URLWithString: urlString];
[request setURL:aUrl];
NSError * error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &error];
NSLog(@"%p, %@", error, error.localizedDescription);
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
return responseString;
}
我从中得到的错误是:
根据策略无效:策略条件失败:["eq"、"$Content-Disposition"、""]
我一直在网上对此进行研究,似乎 S3 产生的错误通常与错误没有太大关系。那么,谁能告诉我我在这里做错了什么?
谢谢