我能夠成功將圖像上傳到Amazon S3,但似乎無法弄清楚如何顯示它的進度。顯示使用Swift 3和Amazon SDK將圖像上傳到Amazon S3的上傳進度
我目前正在上傳像這樣。
import UIKit
import MapKit
import CoreData
import AWSCore
import AWSCognito
import AWSS3
import MobileCoreServices
import AWSMobileAnalytics
在我的上傳按鈕
我有以下代碼...
let myIdentityPoolId = "us-west-2:dca2beb4-9a67-etc....etc..."
let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.usWest2, identityPoolId: myIdentityPoolId)
let configuration = AWSServiceConfiguration(region: AWSRegionType.usWest2, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
self.uploadImage(filename)
這是uploadImage功能...
func uploadImage(filename:String){
print("AWS Upload Image Attempt...")
//defining bucket and upload file name
let S3BucketName: String = "distribution-tech-mobile-live"
let filepath = "\(AppDelegate.appDelegate.applicationDocumentsDirectory())/\(filename)"
let imageURL = URL(fileURLWithPath: filepath)
let S3UploadKeyName = filename //TODO: Change this later
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = S3BucketName
uploadRequest?.key = filename
uploadRequest?.contentType = "image/jpeg"
uploadRequest?.body = imageURL
uploadRequest?.serverSideEncryption = AWSS3ServerSideEncryption.awsKms
uploadRequest?.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
DispatchQueue.main.async(execute: {
self.amountUploaded = totalBytesSent // To show the updating data status in label.
self.fileSize = totalBytesExpectedToSend
print("\(totalBytesSent)/\(totalBytesExpectedToSend)")
})
}
self.uploadCompletionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
if ((error) != nil){
print("Failed with error")
print("Error: \(error!)");
}
else{
print("Sucess")
}
})
}
let transferUtility = AWSS3TransferUtility.default()
let expression = AWSS3TransferUtilityUploadExpression()
transferUtility.uploadFile(imageURL, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continue({ (task) -> AnyObject! in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let exception = task.exception {
print("Exception: \(exception.description)")
}
if let _ = task.result {
print("Upload Starting!")
}
return nil;
})
}
我得到了圖片上傳進度代碼另一個線程,但有解決方案似乎不工作,或者我誤解它。
Upload image AWS S3 bucket in swift和此線程(Swift - AWS S3 Upload Image from Photo Library and download it)。他們都有兩種不同的方式來做到這一點,最後一個環節我也無法工作。
我不知道我在做什麼錯。
問題在哪裏?你有'.uploadProgress'閉包,它爲你提供上傳進度,甚至在你的代碼中使用它。在UI中添加一些代碼更新進度並完成。 – user28434
我混合和匹配上傳的兩種不同方法的問題,所以我正在做一個不同的方式做上傳進度,並使用另一種方式觸發上傳,所以上傳過程從未奏效。因爲關於這個主題的所有答案變種,我感到困惑。 –