[英]Multiple images upload to Bucket AWS S3 using swift
I am tryin to upload an image to bucket AWS s3 using below code.我正在尝试使用以下代码将图像上传到存储桶 AWS s3。
let ext = "jpeg"
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.acl = .publicRead
uploadRequest?.body = URL(fileURLWithPath: path)
uploadRequest?.key = s3BucketKey
uploadRequest?.bucket = S3BucketName
uploadRequest?.contentType = "image/" + ext
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
let transferManager = AWSS3TransferManager.default()
transferManager.upload(uploadRequest!).continueWith { (task) -> AnyObject! in
if let error = task.error {
print("Upload failed ❌ (\(error))")
}
if task.result != nil {
let s3URL = "http://s3.amazonaws.com/\(S3BucketName)/\(String(describing: uploadRequest!.key!))"
print("Uploaded to:\n\(s3URL)")
completion(s3URL)
}
else {
print("Unexpected empty result.")
completion(nil)
}
return nil
}
}
But now I need to upload multiple images to bucket AWS S3, I thought of using same same function in loop to upload files using but its taking more processing time, also I need to sync my data once all images get uploaded.但是现在我需要将多个图像上传到存储桶 AWS S3,我想在循环中使用相同的函数来上传文件,但它需要更多的处理时间,而且我还需要在所有图像上传后同步我的数据。
Please suggest workaround which will take less processing time to upload multiple images and I should get notified once all images get uploaded.请提出解决方法,这将花费更少的处理时间来上传多个图像,并且一旦所有图像都上传,我应该会收到通知。 Thanks in advance.
提前致谢。
You can try with RxSwift framework.您可以尝试使用RxSwift框架。 Something like...
有点像...
// Multiple image uploading
RRAWSRxUpload.upload(dataList: [AWSImageData(type: .image1, image: #imageLiteral(resourceName: "image1")), AWSImageData(type: .image2, image: #imageLiteral(resourceName: "image2"))])
.subscribeOn(ConcurrentDispatchQueueScheduler.init(qos: .background))
.observeOn(MainScheduler.instance)
.subscribe(onNext: { response in
print(response)
//Get here all file uploaded key names after that you will call your server API call to update those files.
}, onError: { error in
print(error.localizedDescription)
}).disposed(by: rxbag)
Also, find out more detail on GitHub demo of RRAWSRXUpload .另外,在RRAWSRXUpload 的GitHub 演示中找到更多详细信息。
And you can also call your server API by RxSwift with Alamofire library.你也可以通过 RxSwift 和Alamofire库调用你的服务器 API。
Once you finished AWS S3 upload part after that you will call your server Alamofire APIs request by RRAlamofireRxAPI .完成 AWS S3 上传部分后,您将通过RRAlamofireRxAPI调用您的服务器 Alamofire API 请求。
RRAWSRxUpload.upload(dataList: [AWSImageData(type: .image1, image: #imageLiteral(resourceName: "image1")), AWSImageData(type: .image2, image: #imageLiteral(resourceName: "image2"))])
.flatMap { (keys) -> Observable<DataModelObject> in
print(keys)// pass your files array/model to your server as parameters
return RRAPIRxManager.rxCall(apiUrl: APIEndPoint.Name.uploadAWSImage, httpMethod: .post, param: parameters)
}
.subscribeOn(RXScheduler.concurrentBackground)
.observeOn(RXScheduler.main)
.subscribe(onNext: {(response) in
print(response)
}, onError: { (error) in
print(error)
}).disposed(by: rxbag)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.