简体   繁体   English

使用 swift 将多个图像上传到 Bucket AWS S3

[英]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.

相关问题 快速上传图像 AWS S3 存储桶 - Upload image AWS S3 bucket in swift 应用程序在AWS S3存储桶中的上载映像时崩溃,导致Swift 3 - Application getting crashed at upload image in AWS S3 bucket for swift 3 快速上传到s3存储桶并没有结束 - Swift upload to s3 bucket does not end 使用 AWS Swift SDK 上传图片到 S3 - Using AWS Swift SDK to upload image to S3 AWS S3将图像上传到Bucket iOS应用程序 - AWS S3 Upload image to Bucket iOS app 在Amazon S3存储桶中上传背景图片(背景模式),而无需在iOS Swift中使用AWS开发工具包 - Background image (Background Mode) uploading in Amazon S3 bucket without using AWS SDK in iOS Swift 如何将我的ios应用程序中的图像上传到存储在aws s3上的存储桶中,并在Swift中完成所有这些操作? - How can I upload an image from my ios app to my bucket stored on aws s3 and do all of that in Swift? 如何使用 AWS amplify 从只读 AWS S3 Bucket 安全地将图像下载到 iOS 应用程序? - How to securely download images from read-only AWS S3 Bucket to an iOS app using AWS amplify? 快速将多个文件并行上传到AWS S3中并在表格视图单元中显示进度视图状态 - Swift upload multiple files parallel into AWS S3 and show progress view status in tableview cell 使用适用于iOS的AWS S3 SDK v2上传图像:跟踪进度 - Upload images using AWS S3 SDK v2 for iOS: Track progress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM