繁体   English   中英

如何从 iOS 中的 AWS S3 存储桶下载文件?

[英]How to download a file from a AWS S3 bucket in iOS?

在我的 iOS 应用程序中,我尝试从 AWS S3 存储桶下载文件。 这是我尝试过的:

  1. 我初始化 AWSMobileClient:
import AWSMobileClient
import AWSS3

let configuration = AWSServiceConfiguration(
    region: AWSRegionType.EUCentral1,
    credentialsProvider: AWSMobileClient.default())
AWSServiceManager.default().defaultServiceConfiguration = configuration

AWSMobileClient.default().initialize { (userState: UserState?, error: Error?) in

    if (userState != nil)
    {
        print("Initialize OK : \(userState.debugDescription)")
    }
    if (error != nil)
    {
        print("Initialize error: \(String(describing: error))")
    }
}

我有:

"initialize OK : Optional(AWSMobileClient.UserState.guest)"
  1. 现在我尝试下载一个文件:
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
        print("Progress : \(progress)")
    })
}
                
let completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock = {
    (task: AWSS3TransferUtilityDownloadTask, url: URL?, data: Data?, error: Error?) -> Void in
        DispatchQueue.main.async(execute: {
            print("End download 1")
        })
}
                
let fileManager = FileManager.default
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("100.ogg")
                
let transferUtility: AWSS3TransferUtility = AWSS3TransferUtility.default()
transferUtility.download(
    to: fileURL,
    bucket: "my-s3-bucket",
    key: "100.ogg",
    expression: expression,
    completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
                        
        if let error = task.error {
            print("Error download : \(error)")
        }
        if let result = task.result {
            print("Result : \(result.debugDescription)")
        }
        print("End download 2 : \(fileManager.fileExists(atPath: fileURL.absoluteString))")
        return nil
    }

我有:

"Result : <AWSS3TransferUtilityDownloadTask: 0x6000020bd4d0>
"End download 2 : false"

我没有任何进展,我也没有得到"End download 1"

所以基本上,我没有收到任何错误,但看起来确实没有下载任何内容。 另外,附带说明一下,它适用于我的应用程序的 Android 版本,因此我的代码很可能存在错误。

那么我应该改变什么才能让它工作呢?

谢谢。

我的错,上面的例子实际上是有效的,但我必须:

  • 通过 fileManager.fileExists(atPath: fileURL.path)) 更改fileManager.fileExists(atPath: fileURL.absoluteString)) fileManager.fileExists(atPath: fileURL.path))
  • 检查第一个completionHandler中是否存在文件(我在其中写了print("End download 1")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM