I have server that generates parameters like AWSAccessKeyID, acl, policy, signature for uploading file to S3 using POST like here: http://doc.s3.amazonaws.com/proposals/post.html . Now having these parameters I need to run this request on Amazon server somehow. Seems that I can't use native AWS iOS SDK, because it's S3 client can only be initialized with AWS key and secret, which are stored on server and not on device.
What is the best way to call this POST request with all parameters on S3 to upload file? Or maybe there are ways to use AWS SDK for that.
Ok, in case it helps someone, I managed to do it this way:
NSDictionary* parametersDictionary = @{@"AWSAccessKeyId" : @"YOUR_KEY",
@"acl" : @"public-read", // or whatever you need
@"key" : @"filename.ext", // file name on server, without leading /
@"policy" : @"YOUR_POLICY_DOCUMENT_BASE64_ENCODED",
@"signature" : @"YOUR_CALCULATED_SIGNATURE"
};
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString: @"http://s3-bucket.s3.amazonaws.com"]];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
path:nil
parameters:parametersDictionary
constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:fileData
name:@"file" //N.B.! To post to S3 name should be "file", not real file name
fileName:@"your_filename"
mimeType:@"mime/type"];
}];
AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
You can use any other kind of operation like AFXMLRequestOperation, if needed, because S3 returns response in XML format.
If any parameters is missing or contains invalid data - request won't be accepted.
Here is a link to background info on this: Browser Uploads to S3 using HTML POST Forms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With