Download many objects
Stay organized with collections
Save and categorize content based on your preferences.
Use Transfer Manager to download many objects with concurrency.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Download many objects\n\nUse Transfer Manager to download many objects with concurrency.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Download objects](/storage/docs/downloading-objects)\n\nCode sample\n-----------\n\n### Java\n\n\nFor more information, see the\n[Cloud Storage Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BlobInfo.html;\n import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.DownloadResult.html;\n import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.ParallelDownloadConfig.html;\n import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManager.html;\n import com.google.cloud.storage.transfermanager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html;\n import java.nio.file.Path;\n import java.util.List;\n\n class DownloadMany {\n\n public static void downloadManyBlobs(\n String bucketName, List\u003cBlobInfo\u003e blobs, Path destinationDirectory) throws Exception {\n\n try (https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManager.html transferManager =\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html.newBuilder().build().getService()) {\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.ParallelDownloadConfig.html parallelDownloadConfig =\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.ParallelDownloadConfig.html.newBuilder()\n .setBucketName(bucketName)\n .setDownloadDirectory(destinationDirectory)\n .build();\n\n List\u003cDownloadResult\u003e results =\n transferManager.downloadBlobs(blobs, parallelDownloadConfig).getDownloadResults();\n\n for (https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.DownloadResult.html result : results) {\n System.out.println(\n \"Download of \"\n + https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.spi.v1.StorageRpc.RewriteResponse.html#com_google_cloud_storage_spi_v1_StorageRpc_RewriteResponse_result.getInput().getName()\n + \" completed with status \"\n + https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.spi.v1.StorageRpc.RewriteResponse.html#com_google_cloud_storage_spi_v1_StorageRpc_RewriteResponse_result.getStatus());\n }\n }\n }\n }\n\n### Node.js\n\n\nFor more information, see the\n[Cloud Storage Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // The ID of your GCS bucket\n // const bucketName = 'your-unique-bucket-name';\n\n // The ID of the first GCS file to download\n // const firstFileName = 'your-first-file-name';\n\n // The ID of the second GCS file to download\n // const secondFileName = 'your-second-file-name;\n\n // Imports the Google Cloud client library\n const {Storage, TransferManager} = require('https://cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');\n\n // Creates a client\n const storage = new Storage();\n\n // Creates a transfer manager client\n const transferManager = new https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/transfermanager.html(storage.bucket(bucketName));\n\n async function downloadManyFilesWithTransferManager() {\n // Downloads the files\n await transferManager.https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/transfermanager.html([firstFileName, secondFileName]);\n\n for (const fileName of [firstFileName, secondFileName]) {\n console.log(`gs://${bucketName}/${fileName} downloaded to ${fileName}.`);\n }\n }\n\n downloadManyFilesWithTransferManager().catch(console.error);\n\n### Python\n\n\nFor more information, see the\n[Cloud Storage Python API\nreference documentation](https://cloud.google.com/python/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n def download_many_blobs_with_transfer_manager(\n bucket_name, blob_names, destination_directory=\"\", workers=8\n ):\n \"\"\"Download blobs in a list by name, concurrently in a process pool.\n\n The filename of each blob once downloaded is derived from the blob name and\n the `destination_directory `parameter. For complete control of the filename\n of each blob, use transfer_manager.download_many() instead.\n\n Directories will be created automatically as needed to accommodate blob\n names that include slashes.\n \"\"\"\n\n # The ID of your GCS bucket\n # bucket_name = \"your-bucket-name\"\n\n # The list of blob names to download. The names of each blobs will also\n # be the name of each destination file (use transfer_manager.download_many()\n # instead to control each destination file name). If there is a \"/\" in the\n # blob name, then corresponding directories will be created on download.\n # blob_names = [\"myblob\", \"myblob2\"]\n\n # The directory on your computer to which to download all of the files. This\n # string is prepended (with os.path.join()) to the name of each blob to form\n # the full path. Relative paths and absolute paths are both accepted. An\n # empty string means \"the current working directory\". Note that this\n # parameter allows accepts directory traversal (\"../\" etc.) and is not\n # intended for unsanitized end user input.\n # destination_directory = \"\"\n\n # The maximum number of processes to use for the operation. The performance\n # impact of this value depends on the use case, but smaller files usually\n # benefit from a higher number of processes. Each additional process occupies\n # some CPU and memory resources until finished. Threads can be used instead\n # of processes by passing `worker_type=transfer_manager.THREAD`.\n # workers=8\n\n from google.cloud.storage import https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html, https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.transfer_manager.html\n\n storage_client = Client()\n bucket = storage_client.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_bucket(bucket_name)\n\n results = https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.transfer_manager.html.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.transfer_manager.html(\n bucket, blob_names, destination_directory=destination_directory, max_workers=workers\n )\n\n for name, result in zip(blob_names, results):\n # The results list is either `None` or an exception for each blob in\n # the input list, in order.\n\n if isinstance(result, Exception):\n print(\"Failed to download {} due to exception: {}\".format(name, result))\n else:\n print(\"Downloaded {} to {}.\".format(name, destination_directory + name))\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=storage)."]]