我正在使用 Alamofire 4.0 创建将文件直接上传到 S3 Amazon 的请求。
我正在使用 GetCloudApp 的服务.在我从 api "https://my.cl.ly/v3/items
请求 Router.shared.prepareForUploadItem
之后,我像这样检索 json
{
"slug": "1h132K0z2n3G",
"name": "Image.png",
"url": "http://f.cl.ly",
"uploads_remaining": 1,
"max_upload_size": 26214400,
"s3": {
"AWSAccessKeyId": "AKIAJP2C6U543KJIE2GA",
"key": "items/353u2B053p0H0D1O3w1b/${filename}",
"policy": "eyJleHBpcmF0aW9uIjoiMjAxNS0xMS0xMlQxMjo0MTozOFoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJjbG91ZGFwcC5jb3BwZXIuaW8ifSxbInN0YXJ0cy13aXRoIiwiJGtleSIsIml0ZW1zLzM1M3UyQjA1M3AwSDBEMU8zdzFiLyJdLHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiJodHRwOi8vbXkuY2wuZGV2L3YzL2l0ZW1zLzFoMTMySzB6Mm4zRy9zMyJ9LHsiYWNsIjoicHVibGljLXJlYWQifSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwwLDI2MjE0NDAwXV19",
"signature": "wqSVl9+fvkvtIzGfakNF+drqN0s=",
"success_action_redirect": "http://api.cl.ly/v3/items/1h132K0z2n3G/s3",
"acl": "public-read"
}
}
我将这些 key
和 value
作为我的参数:
"AWSAccessKeyId": "AKIAJP2C6U543KJIE2GA",
"key": "items/353u2B053p0H0D1O3w1b/${filename}",
"policy": "eyJleHBpcmF0aW9uIjoiMjAxNS0xMS0xMlQxMjo0MTozOFoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJjbG91ZGFwcC5jb3BwZXIuaW8ifSxbInN0YXJ0cy13aXRoIiwiJGtleSIsIml0ZW1zLzM1M3UyQjA1M3AwSDBEMU8zdzFiLyJdLHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiJodHRwOi8vbXkuY2wuZGV2L3YzL2l0ZW1zLzFoMTMySzB6Mm4zRy9zMyJ9LHsiYWNsIjoicHVibGljLXJlYWQifSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwwLDI2MjE0NDAwXV19",
"signature": "wqSVl9+fvkvtIzGfakNF+drqN0s=",
"success_action_redirect": "http://api.cl.ly/v3/items/1h132K0z2n3G/s3",
"acl": "public-read"
然后我创建使用 s3
字典上传文件的请求,如下所示:
Alamofire.upload(multipartFormData: { (multipartForm) in
for (key, value) in parameter {
let valueData = value.data(using: .utf8, allowLossyConversion: false)
guard let newData = valueData else{
return
}
multipartForm.append(newData, withName: key)
print("\(key) - \(value)")
}
multipartForm.append(data, withName: "file")
}, to: s3.url, method : .post) { (encodingResult) in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
最后我像这样从服务器检索响应,我对身份验证了如指掌,但我真的不知道这样做的正确方法是什么。
status code: 401, headers {
"Cache-Control" = "no-cache";
Connection = "keep-alive";
"Content-Length" = 28;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Sun, 08 Jan 2017 17:37:17 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
Status = "401 Unauthorized";
"Www-Authenticate" = "Digest realm=\"Application\", qop=\"auth\", algorithm=MD5, nonce=\"MTQ4Mzg5NzAzNzpiYmEwN2MzZDljM2RjNDMyMGE1NzI2ODQ5MjhjMWVkNQ==\", opaque=\"9eb56ccb2e8b017ae42bdb4739690863\"";
"X-Request-Id" = 7bd77581bf677aee1b1abba21b3ad097;
"X-Runtime" = "0.004697";
"X-UA-Compatible" = "IE=Edge,chrome=1";
我尝试了很多方法,但仍然没有运气,也无法成功。我真的需要帮助。提前致谢。
我知道为什么会出现这个问题,因为在我向 CloudApp
请求上传新文件后,我需要使用 CloudApp
的 Digest 验证我的上传请求到 S3 .
我将在下面发布我完成的代码:
func uploadFileToS3(_ s3: S3, data : Data, fileName : String, mimeType : String) {
let params = s3.s3Parameter
Alamofire.upload(multipartFormData: { (multipartForm) in
for (key, value) in params{
multipartForm.append(value.data(using: .utf8)!, withName: key)
}
multipartForm.append(data, withName: "file", fileName: fileName, mimeType: mimeType)
}, to: s3.url, method : .post, headers:["Accept":"application/json"]) { (encodingResult) in
guard let userInfor = UserDefaults.standard.dictionary(forKey: "UserInformation") else {
return
}
switch encodingResult {
case .success(let upload, _, _):
upload.authenticate(user: userInfor["email"]! as! String, password: userInfor["password"]! as! String)
self.progressbar.isHidden = false
upload.uploadProgress(closure: { (progress) in
self.progressbar.progress = Float(progress.fractionCompleted)
})
upload.responseJSON{ response in
self.progressbar.isHidden = true
let result = JSON(response.result.value!)
let file = CloudFile(fileInformation: result)
self.files.append(file)
let indexPath = IndexPath(row: 0, section: 0)
DispatchQueue.main.async {
self.filesTableView.insertRows(at: [indexPath], with: .automatic)
}
}
case .failure(let encodingError):
print(encodingError)
}
}
}
在 encodingResult
中注意到 upload.authenticate(user: userInfor["email"]!as!String, password: userInfor["password"]!as!String)
结果完成句柄,这是您需要验证上传请求的地方。 Alamofire 将完成剩下的工作。
希望这会有所帮助。谢谢
关于ios - 使用 Alamofire 将文件上传到 amazon s3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535722/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |