从S3存储桶

时间:2016-12-12 17:56:40

标签: ios swift amazon-s3

我有一个用swift 2.3编写的ios项目,它是按照AWS移动集线器集成页面的说明与AWS集成的。在解释从s3存储桶下载文件的页面上,它提供了一个函数,该函数采用AWSContent类型参数。我想使用该功能从S3存储桶下载特定文件。我尝试使用AWSUserFileManager的contentWithKey成员和我的文件名和路径,但在解开可选值时发现“nil”错误。有人能指出我正确的方向吗?感谢。

private var manager: AWSUserFileManager!
class S3Access: NSObject {


  func setupS3Acess() {
    let x = manager.contentWithKey("public/GMG.csv")
    downloadContent(x, pinOnCompletion: false)
  }

  // This code is from Amazon MobileHub integration page
  private func downloadContent(content: AWSContent, pinOnCompletion: Bool) {
    content.downloadWithDownloadType(
        .IfNewerExists,
        pinOnCompletion: pinOnCompletion,
        progressBlock: {[weak self](content: AWSContent?, progress: NSProgress?) -> Void in
            guard self != nil else { return }
            /* Show progress in UI. */
        },
        completionHandler: {[weak self](content: AWSContent?, data: NSData?, error: NSError?) -> Void in
            guard self != nil else { return }
            if let error = error {
                print("Failed to download a content from a server. \(error)")
                return
            }
            print("Object download complete.")
        })
}

}

1 个答案:

答案 0 :(得分:0)

我找到了罪魁祸首。我需要使用默认的用户文件管理器分配manager变量,例如" manager = AWSUserFileManager.defaultUserFileManager()"。我只是像我一样发布了这个已经与AWS库集成到项目中的开发人员,并且不需要额外的身份验证设置。

private var manager: AWSUserFileManager!
class S3Access: NSObject {


  func setupS3Acess() {
     manager = AWSUserFileManager.defaultUserFileManager()
    let x = manager.contentWithKey("public/GMG.csv")
    downloadContent(x, pinOnCompletion: false)
  }

  // This code is from Amazon MobileHub integration page
  private func downloadContent(content: AWSContent, pinOnCompletion: Bool) {
    content.downloadWithDownloadType(
        .IfNewerExists,
        pinOnCompletion: pinOnCompletion,
        progressBlock: {[weak self](content: AWSContent?, progress: NSProgress?) -> Void in
            guard self != nil else { return }
            /* Show progress in UI. */
        },
        completionHandler: {[weak self](content: AWSContent?, data: NSData?, error: NSError?) -> Void in
            guard self != nil else { return }
            if let error = error {
                print("Failed to download a content from a server. \(error)")
                return
            }
            print("Object download complete.")
        })
}

}