• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:31:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我尝试了很多方法,但仍然没有运气,也无法成功。我真的需要帮助。提前致谢。



Best Answer-推荐答案


我知道为什么会出现这个问题,因为在我向 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap