我非常接近能够使用Alamofire从iOS设备使用签名网址将图像PUT到我的S3存储桶,但我收到请求超时。
我有一个Node后端,使用以下代码生成预签名的URL:
var aws = require('aws-sdk');
var config = require('../config');
const uuid = require('uuid');
aws.config.update({accessKeyId: config.accessKeyId, secretAccessKey: config.secretAccessKey});
exports.getSignedImageUploadURL = function(filetype, callback) {
var s3 = new aws.S3();
var filename = uuid.v4();
var params = {
Bucket: 'xxx',
Key: filename,
Expires: 4000,
ContentType: "image/jpeg"
};
s3.getSignedUrl('putObject', params, function(err, data) {
if (err) {
console.log(err);
callback(err, null);
} else {
callback(null, data);
}
});
}
我可以使用以下选项成功地使用Postman PUT图像:
但似乎我正在努力用Alamofire请求复制这些条件。我目前正在使用以下代码:
Alamofire.upload(.PUT, postURL, headers: ["Content-Type": "image/jpeg"], multipartFormData: { multipartFormData in
if let image = image, imageData = UIImageJPEGRepresentation(image, 1.0) {
multipartFormData.appendBodyPart(data: imageData, name: "file", mimeType: "image/jpeg")
}
}, encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
print("RESPONSE JSON", response)
}
case .Failure(let encodingError):
print("ENCODING ERROR", encodingError)
}
}
)
要获得以下错误:
RESPONSE JSON FAILURE: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
UserInfo={NSUnderlyingError=0x7fee750cfc90 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}},
NSErrorFailingURLStringKey=https://xxx.s3.amazonaws.com/f01ab7e0-ecdc-4f19-93f0-4f1d89b2903c?AWSAccessKeyId=xxx&Content-Type=image%2Fjpeg&Expires=1468384920&Signature=xxx,
NSErrorFailingURLKey=https://xxx.s3.amazonaws.com/f01ab7e0-ecdc-4f19-93f0-4f1d89b2903c?AWSAccessKeyId=xxx&Content-Type=image%2Fjpeg&Expires=1468384920&Signature=xxx,
_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
有什么想法吗?
编辑:
我也尝试过这段代码同样的错误:
if let image = image, imageData = UIImageJPEGRepresentation(image, 1.0) {
Alamofire.upload(.PUT, postURL, headers: ["Content-Type": "image/jpeg"], data: imageData)
.response { (req, res, json, error) in
print(req, res, json, error)
completionHandler(success: false)
return
}
}
答案 0 :(得分:-1)
这很令人尴尬:由于澳大利亚的上传速度,该请求实际上已经超时了。