gpt4 book ai didi

ios - 通过 AFNetworking 使用 S3 预签名帖子

转载 作者:可可西里 更新时间:2023-11-01 05:40:52 24 4
gpt4 key购买 nike

我正在尝试使用在我的服务器上生成的预签名表单将文件上传到 S3。 api 端点从 bucket.presigned_post 返回 url 和字段。我觉得这应该与示例使用的 html 表单相同,但它只是超时而没有响应。

NSLog(@"responseObject: %@", responseObject);
NSURL *uploadURL = [NSURL URLWithString:responseObject[@"upload_url"]];
NSLog(@"uploadURL: %@", uploadURL);
NSDictionary *uploadFields = responseObject[@"upload_fields"];
NSLog(@"uploadFields: %@", uploadFields);

AFHTTPSessionManager *client = [[AFHTTPSessionManager alloc] init];
[client POST:uploadURL.absoluteString parameters:uploadFields constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"iPhone.png" fileName:@"file" mimeType:@"image/png"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"upload responseObject: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"upload error: %@ %@", error, [[NSString alloc] initWithData:error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding]);
}];

该代码的输出(已编辑私有(private)信息):

uploadURL: https://**********.s3.amazonaws.com/
uploadFields: {
AWSAccessKeyId = **********;
"Cache-Control" = "max-age=300, private";
Secure = true;
acl = "public-read";
key = "file_attachments/980191050/iPhone.png";
policy = "**********";
signature = "**********";
"success_action_status" = 201;
}
upload error: Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x7f92694caf10 {NSErrorFailingURLStringKey=https://thecity-dev.s3.amazonaws.com/, NSUnderlyingError=0x7f9269a80220 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)", NSErrorFailingURLKey=https://thecity-dev.s3.amazonaws.com/}

最佳答案

我仍然不确定为什么 AFNetworking 不工作,但我能够使用普通的 NSURLConnection 多部分形式让它工作。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:uploadURL];
request.HTTPMethod = @"POST";
NSString *boundary = [NSString stringWithFormat:@"Boundary+%08X%08X", arc4random(), arc4random()];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];

[uploadFields enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@\r\n", obj] dataUsingEncoding:NSUTF8StringEncoding]];
}];

[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:fileData];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError != nil || ((NSHTTPURLResponse *)response).statusCode != 201) {
if (failure != nil) {
failure(task, connectionError);
}
} else {
if (success != nil) {
success(task, responseObject, [responseObject[@"id"] unsignedIntegerValue]);
}
}
}];

关于ios - 通过 AFNetworking 使用 S3 预签名帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26877548/

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