将多个图像(近100张)从Android上传到Amazon S3? [英] Upload multiple images(nearly 100) from Android to Amazon S3?

查看:79
本文介绍了将多个图像(近100张)从Android上传到Amazon S3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个图像上传到亚马逊s3存储桶.我尝试上传的图像大小各接近300KB.我正在使用循环上传图像.但是,与ios相比,它需要花费更多时间.我正在使用以下代码将图像上传到S3.

I am trying to upload multiple image to amazon s3 bucket. The size of the images which i am trying to upload is nearly 300KB each. I am using loop to upload the images. But it's taking more time compared to ios. I am using the below code to upload the images to S3.

 val uploadObserver = transferUtility!!.upload(bucketname,
                    , "img_$timeStamp.jpg", File(fileUri.path!!),
                    md, CannedAccessControlList.PublicRead)

            uploadObserver.setTransferListener(object : TransferListener {
                override fun onStateChanged(id: Int, state: TransferState) {
                    if (TransferState.COMPLETED == state) {
                     
                    } else if (TransferState.FAILED == state) {
                  
                    }
                }

                override fun onProgressChanged(id: Int, bytesCurrent: Long, bytesTotal: Long) {
                  
                }

                override fun onError(id: Int, ex: Exception) {
                
                }
            })
        }

请帮助我,如何提高上传速度.

Please help me, how to increase the speed of upload.

推荐答案

使用 RxJava RxAndroid ,您可以一次执行多个异步任务. zip 操作将所有任务绑定为一个任务.您的案例的示例代码如下:

Using RxJava and RxAndroid you can do multiple async task at a time. zip operate binds all task into one task. Sample code for your case is as following:

fun uploadTask(bucketname: String, timeStamp: Long, md: Any, uriList: List<Uri>) {
    
    val singles = mutableListOf<Single<String>>()
    uriList.forEach {
        singles.add(upload(bucketname, timeStamp, it, md).subscribeOn(Schedulers.io()))
    }
    val disposable = Single.zip(singles) {
        // If all request emits success response, then it will bind all emitted
        // object (in this example String) into an Array and you will get callback here.
        // You can change your final response here. In this can I am appending all strings
        // to a string.
        var finalString = ""
        it.forEach {  responseString ->
            finalString += responseString
        }
        finalString
    }
            .subscribe({
                // you will get final response here.
            }, {
                // If any one request failed and emits error all task will be stopped and error
                // will be thrown here.
                it.printStackTrace()
            })
    
}

upload()方法可以如下:

fun upload(bucketname: String, timeStamp: Long, fileUri: Uri, md: Any): Single<String> {
    return Single.create<String> { emitter ->  // You can change String to anything you want to emmit

        val uploadObserver = transferUtility!!.upload(bucketname,
                , "img_$timeStamp.jpg", File(fileUri.path!!),
                md, CannedAccessControlList.PublicRead)

        uploadObserver.setTransferListener(object : TransferListener {
            override fun onStateChanged(id: Int, state: TransferState) {
                if (TransferState.COMPLETED == state) {
                    emitter.onSuccess("COMPLETED")  // Emmit your object here
                } else if (TransferState.FAILED == state) {
                    emitter.onSuccess("FAILED")
                }
            }
    
            override fun onProgressChanged(id: Int, bytesCurrent: Long, bytesTotal: Long) {
            
            }
    
            override fun onError(id: Int, ex: Exception) {
                emitter.onError(ex)
            }
        })
    }
}

请记住,如果任何上传请求失败,则所有其他任务都将停止.如果要继续,则必须使用onErrorResumeNext()运算符.

Just keep in mind, if any upload request is failed, all other task will be stopped. If you want to continue you have to use onErrorResumeNext() operator in that case.

这篇关于将多个图像(近100张)从Android上传到Amazon S3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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