我使用AFNetworking和AFAmazonS3Client将文件上传到Amazon S3。我循环遍历一个文件数组,所有文件按预期成功上传到S3服务器,但在上传期间,额外的内容已添加到不应该存在的每个文件中。例如。添加到文件名为kmeans.sh的文本如下所示:
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="file"; filename="kmeans.sh"
Content-Type: application/octet-stream
此类内容发生在每个文件的顶部。我上传的方法如下所示:
NSMutableURLRequest *request = [self
multipartFormRequestWithMethod:method
path:destination
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data
name:@"file"
fileName:[fileURL.path lastPathComponent]
mimeType:self.mimetype];
}];
我在这里做错了什么,为我的文件添加了额外的信息?我真的很难理解这一点,我很感激我可以尝试的任何建议。
更新 最初我是按照Matt的建议做的,但是我使用的是PUT方法而不是POST。今天我下载了最新版本的AFAmazonS3Client,尝试使用以下方法上传PUT和POST文件:
postObjectWithFile:(NSString *)path destinationPath:(NSString *)destinationPath parameters....
putObjectWithFile:(NSString *)path destinationPath:(NSString *)destinationPath parameters....
对于文件&#34; Lima1996.pdf&#34;上传到桶objcs3和前缀&#34; Site3 / hei /&#34 ;, stringToSign看起来像: POST
multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY
Thu, 06 Jun 2013 00:37:04 GMT
/objcs3/Site3/hei/Lima1996.pdf
请求如下:
auth: {
"Accept-Language" = "en;q=1, nb;q=0.9";
Authorization = "removed";
"Content-Length" = 915478;
"Content-Type" = "multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY";
Date = "Thu, 06 Jun 2013 00:37:04 GMT";
"User-Agent" = "connectCloudTest/1.0 (Mac OS X Version 10.8.4 (Build 12E55))";
}
Everythng看起来不错,但是当我使用POST方法上传文件时,我不断收到错误消息
Upload failed Expected status code in (200-299), got 405 (-1011)
虽然使用PUT方法导致上一个错误,其中上传成功,但内容使用原始问题中的额外标题信息乱码。建议非常感谢!
感谢您的帮助!干杯,特隆德
答案 0 :(得分:3)
我通过将AFAmazonS3Client中的putObjectWithMethod
替换为:
(void)putObjectWithMethod:(NSString *)method
file:(NSString *)filePath
destinationPath:(NSString *)destinationPath
parameters:(NSDictionary *)parameters
progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
NSMutableURLRequest *filerequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];
[filerequest setCachePolicy:NSURLCacheStorageNotAllowed];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:filerequest returningResponse:&response error:&error];
if (data && response) {
NSMutableURLRequest *request = [super requestWithMethod:@"PUT" path:destinationPath parameters:nil];
[self authorizeRequest:request withPath:destinationPath];
[request setHTTPBody:data];
AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
[requestOperation setUploadProgressBlock:progress];
[self enqueueHTTPRequestOperation:requestOperation];
}
}
我不知道为什么我的POST方法从未起作用,但是PUT一切正常。干杯,特隆德
答案 1 :(得分:0)
AFAmazonS3Client具有类似postObjectWithFile:destinationPath:parameters:progress:success:failure:
的方法,它负责所有多部分表单构建。不一定需要创建自己的请求,这可能是文件中附加标题的原因。