使用swift将多个图像上传到Bucket AWS S3 [英] Multiple images upload to Bucket AWS S3 using swift

查看:83
本文介绍了使用swift将多个图像上传到Bucket AWS S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将图像上传到存储桶AWS s3.

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
        }
    }

但是现在我需要将多个图像上传到存储桶中的AWS S3,我想到了使用循环使用相同的功能来上传文件,但使用它需要更多的处理时间,而且所有图像上传后我还需要同步数据.

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.

推荐答案

您可以尝试使用 RxSwift 框架.像...

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)

此外,请找到有关 RRAWSRXUpload 的GitHub演示的更多详细信息.

Also, find out more detail on GitHub demo of RRAWSRXUpload.

您还可以使用 Alamofire 库通过RxSwift调用服务器API.

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

完成部分AWS S3上传之后,您将通过 RRAlamofireRxAPI调用服务器Alamofire API请求.

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)

这篇关于使用swift将多个图像上传到Bucket AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆