使用 rxJava 将多个图像上传到 AWS 服务器 [英] Uploading multiple image to AWS server using rxJava

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

问题描述

我正在尝试使用 rxJava 将多个图像上传到 ASW.我找到了一个 kotlin 示例.

从安卓上传多张图片(近 100 张)到亚马逊S3?.

我尝试使用 Java 实现它.但找不到任何合适的方法.有人可以帮我吗?

public void processImageFiles(){上传的图像信息 = 新的 ArrayList<>();列表<单<布尔>>上传列表 = 新的 ArrayList<>();for(文件文件:selectedImageList){String fileExtension = MimeTypeMap.getFileExtensionFromUrl(file.toString());String fileName = Calendar.getInstance().getTimeInMillis() + sharedPref.getSharedPrefData(SharedPref.USER_ID) + ."+ 文件扩展名;String uploadFileLink = "https://";+ ApiClient.BUCKET_NAME + ."+ ApiClient.AWS_END_POINT + "/";+ 文件名;RecordData recordData = new RecordData();recordData.setUrl(uploadedFileLink);recordData.setFileType(fileExtension);recordData.setFileName(fileName);UploadImageInfo.add(recordData);uploadList.add(uploadToAWS(file, recordData));}}public Single<Boolean>上传到AWS(文件图像文件,记录数据数据){返回 Single.create(发射器 -> {AmazonS3 s3Client = new AmazonS3Client(new BasicAWSCredentials(ApiClient.ACCESS_KEY, ApiClient.SECRET_KEY));String uploadLink = ApiClient.BUCKET_NAME;s3Client.setEndpoint(ApiClient.AWS_END_POINT);s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));TransferUtility transferUtility = new TransferUtility(s3Client, mContext);TransferObserver transferObserver = transferUtility.upload(uploadLink, data.getFileName(), imageFile, CannedAccessControlList.PublicRead);mContext.registerReceiver(TransferNetworkLossHandler.getInstance(mContext), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));transferObserver.setTransferListener(new TransferListener() {@覆盖public void onStateChanged(int id, TransferState state) {Log.e("onStateChanged", state.name() + "");如果 (state.name().equals(TransferState.COMPLETED.name())) {发射器.onSuccess(真);} 别的 {发射器.onError(null);}}@覆盖public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {//实现处理文件上传进度的代码.Log.e(e", bytesCurrent + ");}@覆盖public void onError(int id, Exception 异常) {发射器.onError(异常);}});});}

解决方案

我已经回复了并行运行多个请求的类似问题.您可以在此处

我可以建议你这个实现:

public final class RecordData {私人最终字符串网址;私有最终字符串文件类型;私有最终字符串文件名;公共记录数据(字符串网址,字符串文件类型,字符串文件名){this.url = url;this.fileType = 文件类型;this.fileName = 文件名;}公共字符串 getUrl() {返回网址;}公共字符串 getFileType() {返回文件类型;}公共字符串 getFileName() {返回文件名;}}公共记录数据到记录(文件文件){String fileExtension = MimeTypeMap.getFileExtensionFromUrl(file.toString());String fileName = Calendar.getInstance().getTimeInMillis() + sharedPref.getSharedPrefData(SharedPref.USER_ID) + ."+ 文件扩展名;String uploadFileLink = "https://";+ ApiClient.BUCKET_NAME + ."+ ApiClient.AWS_END_POINT + "/";+ 文件名;返回新的 RecordData(uploadedFileLink, fileExtension, fileName);}public void processImageFiles(List selectedImageList) {Observable.fromIterable(selectedImageList).flatMapSingle(file -> uploadToAWS(file, toRecord(file)).subscribeOn(Schedulers.io())).订阅();}

I am trying to upload multiple image to ASW using rxJava. I have found a kotlin example.

Upload multiple images(nearly 100) from Android to Amazon S3?.

I tried to implement it using Java. but couldn't find any proper method. can anyone help me with that?

public void processImageFiles(){
        uploadedImageInfo = new ArrayList<>();

        List<Single<Boolean>> uploadList = new ArrayList<>();

         for(File file : selectedImageList){
            String fileExtension = MimeTypeMap.getFileExtensionFromUrl(file.toString());
            String fileName = Calendar.getInstance().getTimeInMillis() + sharedPref.getSharedPrefData(SharedPref.USER_ID) + "." + fileExtension;
            String uploadedFileLink = "https://" + ApiClient.BUCKET_NAME + "." + ApiClient.AWS_END_POINT + "/" + fileName;
            RecordData recordData =  new RecordData();
            recordData.setUrl(uploadedFileLink);
            recordData.setFileType(fileExtension);
            recordData.setFileName(fileName);
            uploadedImageInfo.add(recordData);
            uploadList.add(uploadToAWS(file, recordData));
        }
         

    }


    public Single<Boolean> uploadToAWS(File imageFile, RecordData data) {

        return Single.create(emitter -> {

            AmazonS3 s3Client = new AmazonS3Client(new BasicAWSCredentials(ApiClient.ACCESS_KEY, ApiClient.SECRET_KEY));
            String uploadLink = ApiClient.BUCKET_NAME;
            s3Client.setEndpoint(ApiClient.AWS_END_POINT);
            s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));
            TransferUtility transferUtility = new TransferUtility(s3Client, mContext);
            TransferObserver transferObserver = transferUtility.upload(uploadLink, data.getFileName(), imageFile, CannedAccessControlList.PublicRead);
            mContext.registerReceiver(TransferNetworkLossHandler.getInstance(mContext), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

            transferObserver.setTransferListener(new TransferListener() {

                @Override
                public void onStateChanged(int id, TransferState state) {

                    Log.e("onStateChanged", state.name() + "");
                    if (state.name().equals(TransferState.COMPLETED.name())) {
                        emitter.onSuccess(true);
                    } else {
                        emitter.onError(null);
                    }

                }

                @Override

                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

                    //Implement the code to handle the file uploaded progress.
                    Log.e("e", bytesCurrent + "");

                }

                @Override

                public void onError(int id, Exception exception) {
                    emitter.onError(exception);

                }

            });
        });
    }

解决方案

I've already reply to similar questions for running multiple requests in parallel. you can find them here

I can suggest you this implementation :

public final class RecordData {
    private final String url;
    private final String fileType;
    private final String fileName;

    public RecordData(String url, String fileType, String fileName) {
        this.url = url;
        this.fileType = fileType;
        this.fileName = fileName;
    }

    public String getUrl() {
        return url;
    }

    public String getFileType() {
        return fileType;
    }

    public String getFileName() {
        return fileName;
    }
}

public RecordData toRecord(File file) {
    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(file.toString());
    String fileName = Calendar.getInstance().getTimeInMillis() + sharedPref.getSharedPrefData(SharedPref.USER_ID) + "." + fileExtension;
    String uploadedFileLink = "https://" + ApiClient.BUCKET_NAME + "." + ApiClient.AWS_END_POINT + "/" + fileName;
    return new RecordData(uploadedFileLink, fileExtension, fileName);
}

public void processImageFiles(List<File> selectedImageList) {
    Observable.fromIterable(selectedImageList)
            .flatMapSingle(file -> uploadToAWS(file, toRecord(file)).subscribeOn(Schedulers.io()))
            .subscribe();
}

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

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