Download a file in chunks concurrently
Stay organized with collections
Save and categorize content based on your preferences.
Use Transfer Manager to download a single large file in chunks, 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 a file in chunks concurrently\n\nUse Transfer Manager to download a single large file in chunks, with concurrency.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Sliced object downloads](/storage/docs/sliced-object-downloads)\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 AllowDivideAndConquerDownload {\n\n public static void divideAndConquerDownloadAllowed(\n List\u003cBlobInfo\u003e blobs, String bucketName, Path destinationDirectory) {\n 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()\n .setAllowDivideAndConquerDownload(true)\n .build()\n .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 List\u003cDownloadResult\u003e results =\n transferManager.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManager.html#com_google_cloud_storage_transfermanager_TransferManager_downloadBlobs_java_util_List_com_google_cloud_storage_BlobInfo__com_google_cloud_storage_transfermanager_ParallelDownloadConfig_(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### 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 GCS file to download\n // const fileName = 'your-file-name';\n\n // The path to which the file should be downloaded\n // const destFileName = '/local/path/to/file.txt';\n\n // The size of each chunk to be downloaded\n // const chunkSize = 1024;\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 downloadFileInChunksWithTransferManager() {\n // Downloads the files\n await transferManager.https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/transfermanager.html(fileName, {\n destination: destFileName,\n chunkSizeBytes: chunkSize,\n });\n\n console.log(\n `gs://${bucketName}/${fileName} downloaded to ${destFileName}.`\n );\n }\n\n downloadFileInChunksWithTransferManager().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_chunks_concurrently(\n bucket_name, blob_name, filename, chunk_size=32 * 1024 * 1024, workers=8\n ):\n \"\"\"Download a single file in chunks, concurrently in a process pool.\"\"\"\n\n # The ID of your GCS bucket\n # bucket_name = \"your-bucket-name\"\n\n # The file to be downloaded\n # blob_name = \"target-file\"\n\n # The destination filename or path\n # filename = \"\"\n\n # The size of each chunk. The performance impact of this value depends on\n # the use case. The remote service has a minimum of 5 MiB and a maximum of\n # 5 GiB.\n # chunk_size = 32 * 1024 * 1024 (32 MiB)\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 blob = bucket.blob(blob_name)\n\n 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 blob, filename, chunk_size=chunk_size, max_workers=workers\n )\n\n print(\"Downloaded {} to {}.\".format(blob_name, filename))\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)."]]