Elad Benda
Elad Benda

Reputation: 36672

how to download a folder from google storage

I have this code to download a file from google storage to local storage:

@Override
public void downloadFile(URI originFileLocation, URI destinationFileLocation) throws IOException {
    StorageFileId gcsFileId = fileIdCreator.createFromUri(originFileLocation);
    Get getRequest = gsClient.objects().get(gcsFileId.getBucket(), gcsFileId.getObjectId());
    File localFile = new File(destinationFileLocation);
    if (!localFile.exists())
    {
        localFile.createNewFile();
    }
    try (FileOutputStream fileOutputStream = new FileOutputStream(localFile))
    {
        getRequest.getMediaHttpDownloader().setDirectDownloadEnabled(false);
        getRequest.executeMediaAndDownloadTo(fileOutputStream);
    }       
}

Is there an API to download a directory and all its subDirectories to local storage?

Upvotes: 1

Views: 1421

Answers (1)

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38399

No. Google Cloud Storage has no built-in concept of a directory. Instead, there are only object names that contain the "/" character. The user interface and command-line utilities pretend that such concepts exist for convenience, but the API has no real concept of them. Instead, you'll need to use the object listing method to iterate over the objects you're interested in and individually make a call to download each one.

Upvotes: 5

Related Questions