ios - 从 S3 存储桶下载特定文件

标签 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.")
        })
}

}

最佳答案

我找到了罪魁祸首。我需要使用默认用户文件管理器分配管理器变量,例如“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.")
        })
}

}

关于ios - 从 S3 存储桶下载特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41106689/

相关文章:

ios - 在 iPhone 中解码 h264 视频

objective-c - 在 Objective C 类文件中调用 swift 完成 block 。错误: parameter name omitted

ios - 使用 Swift 4 和 UIView 的奇怪动画行为

ios - 如果创建 BLE 应用程序,我的应用程序应该支持哪个最低 iOS 版本?

node.js - 进程在请求完成之前退出

python - Spectrum S3 访问被拒绝

ios - 实时跟踪多个 iPhone GPS 位置

ios - 仅使用提供的 IPA 文件更改应用程序版本(无 xcode)

amazon-web-services - 读取 S3 视频文件,用 ffmpeg 处理并上传到 S3

iOS:有没有办法按下一个按钮,这样它也可以按下另一个按钮?