Search code examples
javagoogle-cloud-storage

Is it possible to query multiple files with one request in Google Cloud Storage?


I have a function that retrieves a download link for an object stored in the Google Cloud Storage. The implementation in Java looks something like this:

public String getDownloadLink(String bucketName,
                              String blobName) {
    return storage.signUrl(
            BlobInfo.newBuilder(bucketName, blobName).build(),
            60,
            TimeUnit.MINUTES,
            SignUrlOption.httpMethod(HttpMethod.GET),
            SignUrlOption.withV4Signature()
    ).toString();
}

The above method takes the bucketName and blobName as arguments and returns a downloadlink for that object, valid for 60 minutes. Is it possible to get a list of downloadlink for multiple files or objects in the same bucket, using Java client api for GCS, without having to call the above function in a for-loop? For example the above method could look something like :

public List<String> getDownloadLink(String bucketName,
                              List<String> blobName) {
    // Get the downloadlinks for all the blobName in the list
}

I've been looking around for a solution, but couldn't find anything substantial, and been resorting to using a for-loop to get downloadlinks for multiple filenames. Using for-loop to query multiple files could get pretty resource consuming and tedious. Could really use some help.


Solution

  • Based on my understanding of the question, you want to return a list of signed URLs (valid for 60 min) for multiple objects within a bucket, not directly downloading the files.

    It is evident that if you were to download these objects using any of the Cloud Storage client libraries, you could use BlobListOption.prefix() to filter the query on certain folders or objects, as shown in this example code.

    However, after checking the Java Cloud Storage client library documentation, I'm not aware of any prefix or method based Signed URL to access a list of objects in Cloud Storage API at the moment.

    Since every file or object needs to get signed individually, you will most likely need to iterate through the list of blobs.

    Although it is not the Java Cloud Storage client library, I found the following example that may help you.