Multiple images upload to Bucket AWS S3 using swift

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-01 08:46:21

问题


I am tryin to upload an image to bucket AWS s3 using below code.

    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.

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.


回答1:


You can try with RxSwift framework. 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.

And you can also call your server API by RxSwift with Alamofire library.

Once you finished AWS S3 upload part after that you will call your server Alamofire APIs request by RRAlamofireRxAPI.

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)


来源:https://stackoverflow.com/questions/48322629/multiple-images-upload-to-bucket-aws-s3-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!