0

执行任务,最后使用 Flowable rxjva3 返回值。我有以下代码

public Maybe<List<String>> uploadObject(Publisher<CompletedFileUpload> images) {
        Storage storage = StorageOptions.getDefaultInstance().getService();
        var returnValue = Flowable.fromPublisher(images)
                .collect((List<String> returnImages, CompletedFileUpload image) -> {
                    BlobId blobId = BlobId.of(googleUploadObjectConfiguration.bucketName(), image.getName());
                    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
                    Blob updatedImage = storage.create(blobInfo, image.getBytes());
                    returnImages.add(updatedImage.getName());
                })
                .flatMapMaybe(returnImages -> Maybe.just(returnImages));
    }

基本上,它会迭代并将图像上传到谷歌存储。然后返回的媒体 URL 应该返回到 String 列表。但是尝试了下面的代码,返回类型是Maybe<U>. 执行此操作的正确方法是什么?

更新 1

Flowable.fromPublisher(images).collect(ArrayList::new, (returnImages, image) -> {
            BlobId blobId = BlobId.of(googleUploadObjectConfiguration.bucketName(), image.getName());
            BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
            Blob updatedImage = storage.create(blobInfo, image.getBytes());
            returnImages.add(updatedImage.getName());
            LOG.info(
                    String.format("File %s uploaded to bucket %s as %s", image.getName(),
                            googleUploadObjectConfiguration.bucketName(), image.getName())
            );
        }).flatMapMaybe((returnImages)-> List.of(returnImages));

这也不正确,返回类型应该是Maybe<List<String>>

4

1 回答 1

1

从评论中,使用两个参数collect,然后使用toMaybe. 您可能需要加强集合类型,如下所示:

Flowable.fromPublisher(images)
.<List<String>>collect(ArrayList::new, (returnImages, image) -> {
    BlobId blobId = BlobId.of(googleUploadObjectConfiguration.bucketName(), image.getName());
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    Blob updatedImage = storage.create(blobInfo, image.getBytes());
    returnImages.add(updatedImage.getName());
    LOG.info(
        String.format("File %s uploaded to bucket %s as %s", image.getName(),
                            googleUploadObjectConfiguration.bucketName(), image.getName())
            );
}).toMaybe();
于 2021-03-10T17:23:40.243 回答