Amazon S3に画像を正常にアップロードできましたが、その進捗状況を示す方法がわかりません。アップロードを表示Amazon S3への画像アップロードの進捗状況Swift 3とAmazon SDK
私は現在そのようにアップロードしています。
私は、次のコードを持っている私のアップロードボタンで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)。彼らには2つの異なる方法があります。私はどちらかの方法で作業することができませんでした。
私が間違っていることはわかりません。
問題はどこですか?アップロードの進行状況を示す '.uploadProgress'クロージャがあり、コード内で使用することさえできます。 UIに進捗状況を更新するコードをいくつか追加してください。 – user28434
問題は、私は2つの異なるアップロード方法を混在させてマッチングしていたので、以前は別の方法でアップロードを進めていました。私はこのトピックに関する答えのすべての変形のために混乱しました。 –