gpt4 book ai didi

ios - iOS 中的 Amazon S3 POST 上传

转载 作者:可可西里 更新时间:2023-11-01 04:08:55 25 4
gpt4 key购买 nike

我的服务器会生成 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com