Amazon S3 POST上载于iOS

时间:2022-11-18 23:03:58

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.

我有一个服务器,它可以生成像AWSAccessKeyID、acl、策略、签名这样的参数,它们可以通过POST方式将文件上传至S3: http://doc.s3.amazonaws.com/proposals/post.html。现在有了这些参数,我需要在Amazon服务器上以某种方式运行这个请求。看来我不能使用原生的AWS iOS SDK,因为它的S3客户端只能用AWS key and secret进行初始化,它存储在服务器上而不是设备上。

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.

使用S3上的所有参数调用这个POST请求来上传文件的最佳方式是什么?或者也可以使用AWS SDK实现这个目的。

1 个解决方案

#1


18  

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.

如果需要,您可以使用任何其他类型的操作,如AFXMLRequestOperation,因为S3以XML格式返回响应。

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

这里有一个关于背景信息的链接:浏览器使用HTML POST表单上传至S3

#1


18  

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.

如果需要,您可以使用任何其他类型的操作,如AFXMLRequestOperation,因为S3以XML格式返回响应。

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

这里有一个关于背景信息的链接:浏览器使用HTML POST表单上传至S3