Issue
I am trying to delete many files from google cloud storage at once.
I am using the following code:
public List<Boolean> deleteObjects(List<String> fileParams) {
List<BlobId> blobs =
fileParams.stream()
.map(
file -> {
logger.info("deleteObject: {}", file);
return BlobId.of(bucketName, file);
})
.collect(Collectors.toList());
return storage.delete(blobs);
}
This call takes a very long time - I tried to delete 150k files and it took almost 1 hour.
I would like to run it as "fire and forget".
I saw in the JS example that the api is async by nature:
await storage.bucket(bucketName).file(fileName).delete();
I didn't find such example for Java, either with or without a batch.
I guess I can start a new thread and run it, but I wanted to know if the API supports something like that natively.
Is it possible to run an async command natively by the api?
Solution
The delete object API call is synchronous (it doesn't return a jobId that you have to poll to know if the operation is done or not). Therefore, the standard library can't implement async call because it's sync.
NodeJS best practice is to create async function when you perform API call. It's a language design not an API behavior. You can do the same in Java, Python and Go, but it's not out of the box, you need to create yourselves the concurrency.
Answered By - guillaume blaquiere
Answer Checked By - Katrina (JavaFixing Volunteer)