简体   繁体   中英

Multiple images upload to Bucket AWS S3 using swift

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM