3

I am trying desperately to upload an image to Amazon S3 from an iPhone's image gallery. I tried the solution here:

Amazon S3 POST upload in iOS

but to no avail. Since then I have adapted my code to the following:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:[uploadResult objectForKey:@"url"]]];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                     path:nil
                                                               parameters:nil // amazonDictionary
                                                constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                                                    /*
                                                    [formData appendPartWithFileData:UIImageJPEGRepresentation(selectedImageView.image, .9) // (selectedImageView.image)
                                                                                name:@"file" // N.B.! To post to S3 name should be "file", not real file name
                                                                            fileName:@"filename.jpg"
                                                                            mimeType:@"image/jpeg"];
                                                     */


                                                    [formData appendPartWithFormData:[[uploadResult objectForKey:@"s3Key"] dataUsingEncoding:NSUTF8StringEncoding] name:@"AWSAccessKeyId"];
                                                    [formData appendPartWithFormData:[[uploadResult objectForKey:@"s3PolicyBase64"] dataUsingEncoding:NSUTF8StringEncoding] name:@"Policy"];
                                                    [formData appendPartWithFormData:[[uploadResult objectForKey:@"s3Signature"] dataUsingEncoding:NSUTF8StringEncoding] name:@"Signature"];
                                                    [formData appendPartWithFormData:[@"image.jpg" dataUsingEncoding:NSUTF8StringEncoding] name:@"key"];
                                                    [formData appendPartWithFormData:[@"image/jpeg" dataUsingEncoding:NSUTF8StringEncoding] name:@"Content-Type"];
                                                    [formData appendPartWithFormData:[@"public-read" dataUsingEncoding:NSUTF8StringEncoding] name:@"acl"];
                                                    [formData appendPartWithFormData:UIImageJPEGRepresentation(selectedImageView.image, .9) name:@"file"];


                                                }];

For clarity, the base URL I provide to init my AFHTTPClient is a URL of the form

"http://" + bucket + ".s3.amazonaws.com"

Currently, I am receiving the error "Invalid Policy: Invalid JSON." when I parse the XML presented by S3. Help resolving this issue is greatly appreciated.

I should point out that I have the following code on a webapp that DOES work for uploading an image:

var fd = new FormData();
fd.append("AWSAccessKeyId", data.s3Key);
fd.append("Policy", data.s3PolicyBase64);
fd.append("Signature", data.s3Signature);
fd.append("key", file.name);
fd.append("acl", "public-read");
fd.append("Content-Type", file.type);
fd.append("file", file);

$http({
    method: 'POST',
    url: data.url,
    data: fd,
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
})
Community
  • 1
  • 1

1 Answers1

1

I am receiving the error "Invalid Policy: Invalid JSON." when I parse the XML

Sounds like you are trying to use the AFNetworking JSON handler instead of your own XML handler. If the response is XML, you cannot use the built in JSON parser from AFNetworking, you have to parse the returned NSData yourself.

UPDATE

Have you tried inspecting the POST json going out using something like HTTPScoop? Perhaps it is actually malformed and not as expected by Amazon.

coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • When I go to the handle the response from S3, I use the AFXMLRequestOperation as follows:AFXMLRequestOperation * operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) { NSLog(@"XML succeeded"); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) { NSLog(@"XML failed with error: %@",error); }]; Sorry, my commenting is epic fail. I'm new. S3 communicates these errors in XML, which is why I use XML parsing. – user3064222 Dec 27 '13 at 18:34
  • "Invalid Policy: Invalid JSON" is not an AFNetworking error, it's an S3 error. I don't think the OP is using the AFNetworking JSON handler. – Aaron Brager Dec 28 '13 at 23:03