【发布时间】:2017-04-28 03:41:02
【问题描述】:
我能够成功地将图像上传到 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中添加一些代码更新进度就完成了。 -
我混合和匹配两种不同的上传方法的问题,所以我以前用不同的方式进行上传进度,并使用另一种方式触发上传,所以上传进度永远不会起作用。由于有关此主题的答案的所有变体,我感到困惑。
标签: swift file-upload amazon-s3 swift3 ios10