Amazon S3的POST上传iOS中上传、Amazon、iOS、POST

2023-09-11 08:49:47 作者:别过来你丑到我了

我有服务器生成像AWSAccessKeyID,ACL,策略的签名参数使用POST喜欢这里上传文件到S3:的 http://doc.s3.amazonaws.com/proposals/post.html 。 现在有这些参数,我需要以某种方式运行亚马逊的服务器上此请求。看来,我不能使用原生的iOS AWS SDK,因为它是S3的客户端只能通过AWS密钥和密码,它们存储在服务器上,而不是在设备进行初始化。

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.

什么是调用与S3的所有参数,这个POST请求上传文件的最好方法是什么?或者,也许有很多方法可以使用​​AWS SDK为。

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];

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

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.

下面是一个链接,背景资料的是:浏览器上传使用HTML表单POST到S3

Here is a link to background info on this: Browser Uploads to S3 using HTML POST Forms