AFNetworking POST to Amazon S3

Viewed 1129

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}
})
1 Answers
Related