ios - iOS 中的 Amazon S3 POST 上传

标签 ios post amazon-s3

我的服务器会生成 AWSAccessKeyID、acl、策略、签名等参数,以便使用 POST 将文件上传到 S3,如下所示:http://doc.s3.amazonaws.com/proposals/post.html . 现在有了这些参数,我需要以某种方式在亚马逊服务器上运行这个请求。似乎我不能使用 native AWS iOS SDK,因为它的 S3 客户端只能使用 AWS key 和 secret 进行初始化,它们存储在服务器上而不是设备上。

在 S3 上使用所有参数调用此 POST 请求以上传文件的最佳方法是什么?或者也许有一些方法可以为此使用 AWS SDK。

最佳答案

好的,如果它对某人有帮助,我设法这样做了:

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 格式返回响应。

如果任何参数丢失或包含无效数据 - 请求将不被接受。

这里是关于此的背景信息的链接:Browser Uploads to S3 using HTML POST Forms

关于ios - iOS 中的 Amazon S3 POST 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14838120/

相关文章:

ios - Spritekit 碰撞

ios - 如何实现从 UICollectionViewController 到 UIViewController 的自定义 UIViewController 转换?

ios - 如何在 HERE Maps iOS 路由中添加超过 20 个禁止区域?

amazon-web-services - 如何为 Cloudfront 上的静态托管网站的子目录设置默认根对象?

ios - 自定义 UIView 作为 navigationItem 标题

post - Laravel更新语法-使用数组更新记录

php - 本地主机(在自定义端口)在 POST 请求后返回空数据

java - DWR安全异常: HttpSessionId is blank in POST request

java - Java 相当于 $curl --upload-file $uploadFileName $upload_url

amazon-web-services - 创建新存储桶或使用 OpenBD 检查存储桶是否存在时,某些功能似乎不存在