ios - AWS S3 iOS 开发工具包 : How to resume upload after connection is interrupted?

标签 ios swift amazon-web-services amazon-s3

这是我完成上传任务的代码:

let image = UIImage(named: "12.jpeg")
let fileManager = FileManager.default
let imageData = UIImageJPEGRepresentation(image!, 0.99)
let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("\(imageData!).jpeg")

fileManager.createFile(atPath: path as String, contents: imageData, attributes: nil)

let fileUrl = NSURL(fileURLWithPath: path)

uploadRequest?.bucket = "testrawdata"
uploadRequest?.key = "test/loodfd.jpeg"
uploadRequest?.contentType = "image/jpeg"
uploadRequest?.body = fileUrl as URL!
uploadRequest?.serverSideEncryption = AWSS3ServerSideEncryption.awsKms
uploadRequest?.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
    DispatchQueue.main.async(execute: {

        print("bytes sent \(bytesSent), total bytes sent \(totalBytesSent), of total \(totalBytesExpectedToSend)")

    })
}


transferManager?.upload(uploadRequest).continue(with: AWSExecutor.mainThread(), withSuccessBlock: { (taskk: AWSTask) -> Any? in
    if taskk.error != nil {
        // Error.
    } else {
        // Do something with your result.
    }
    return nil
})
}

我知道我不需要将其应用于图像,但这只是一个示例,默认情况下我将发送 100mb 之类的文件。

当我在传输过程中将手机设置为飞行模式然后再次打开网络时,它无法完成上传任务。

文档没有明确说明我应该做什么来恢复中断的任务。

这是我尝试过的:

我将请求和管理器的初始化放入 viewDidLoad() 以确保我不会创建另一个请求

class ViewController: UIViewController {

var uploadRequest:AWSS3TransferManagerUploadRequest!
var transferManager: AWSS3TransferManager!


override func viewDidLoad() {
    super.viewDidLoad()
    uploadRequest = AWSS3TransferManagerUploadRequest()
    transferManager = AWSS3TransferManager.default()

}

并尝试打电话

    func resumeTransfer() {

   transferManager?.resumeAll(nil)

}

但是它不起作用。

提前致谢

最佳答案

事实证明,Transfer Utility 是完成此任务的正确工具

func uploadData(data: NSData) {

    let expression = AWSS3TransferUtilityUploadExpression()
    expression.progressBlock = progressBlock

    let transferUtility = AWSS3TransferUtility.default()

    transferUtility.uploadData(
        data as Data,
        bucket: "test",
        key: "test/test.jpeg",
        contentType: "image/jpeg",
        expression: expression,
        completionHander: completionHandler).continue(successBlock: { (task) -> AnyObject! in
            if let error = task.error {
                NSLog("Error: %@",error.localizedDescription);

            }
            if let exception = task.exception {
                NSLog("Exception: %@",exception.description);

            }
            if let _ = task.result {

                NSLog("Upload Starting!")
                // Do something with uploadTask.
            }

            return nil;
        })
}

这样所有上传的事情都在后台发生,我不必担心应用程序被iOS杀死,网络问题等。

甚至可以指定

configuration?.allowsCellularAccess = false

AWSServiceConfiguration

仅当 wifi 可用时才恢复任务。

关于ios - AWS S3 iOS 开发工具包 : How to resume upload after connection is interrupted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41718670/

相关文章:

ios - 自定义 UISlider 无法正确设置最小轨道颜色

ios - iOS 中的 Android 服务是否有任何等效项?

ios - NSNotFound 究竟是什么?

ios - 尝试使用 Swift 3 从 Firebase 检索数据时出现问题

amazon-web-services - AWS Lambda 和 API 网关阶段

amazon-web-services - 无需验证电子邮件的 Cognito

amazon-web-services - 从 CloudFormation 模板生成代码(java、python 等)

objective-c - Objective C 的动态 getter 和 setter

iOS 11 测试版 : Mail View Controller called from app will not send mail

ios - 使用 Alamofire 请求获取图像数据