Skip to content

Instantly share code, notes, and snippets.

@evanwdavis
Created September 25, 2013 15:04
Show Gist options
  • Save evanwdavis/6700962 to your computer and use it in GitHub Desktop.
Save evanwdavis/6700962 to your computer and use it in GitHub Desktop.
NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
NSString *signature = <signature generated on our server>
NSString *AWSAccessKeyId = <access key generated on our server>
NSString *policy = <encoded policy string generated on our server>
NSString *key = <target relative path + filename of image>
NSString *S3BucketName = @"bucket_name";
NSString *content_type = @"image/jpeg";
NSString *filename = <filename of image>;
// Create the headers.
NSMutableDictionary *headers = [[NSMutableDictionary alloc] init];
[headers setObject:content_type forKey:@"Content-Type"];
[headers setObject:AWSAccessKeyId forKey:@"AWSAccessKeyId"];
[headers setObject:key forKey:@"key"];
[headers setObject:policy forKey:@"policy"];
[headers setObject:signature forKey:@"signature"];
[headers setObject:@"public-read" forKey:@"acl"];
AFHTTPRequestSerializer *serializer = [[AFHTTPRequestSerializer alloc] init];
NSURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com/", S3BucketName] parameters:headers constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:filename mimeType:@"image/jpeg"];
}];
//Use an AFHTTPRequestOperation to execute the request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment