执行任务,最后使用 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>>