gpt4 book ai didi

ios - AWS S3 iOS SDK : How to resume upload after connection is interrupted?

转载 作者:可可西里 更新时间:2023-11-01 00:57:41 24 4
gpt4 key购买 nike

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

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 SDK : How to resume upload after connection is interrupted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41718670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com