gpt4 book ai didi

ios - 使用 Alamofire 将文件上传到 amazon s3

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:54:37 25 4
gpt4 key购买 nike

我正在使用 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"
}
}

我将这些 keyvalue 作为我的参数:

"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 authenticate 来验证我对 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/

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