Download all objects in a bucket
Stay organized with collections
Save and categorize content based on your preferences.
Use Transfer Manager to download all of the files in a bucket 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 all objects in a bucket\n\nUse Transfer Manager to download all of the files in a bucket 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.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.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 import java.util.stream.Collectors;\n\n class DownloadBucket {\n\n public static void downloadBucketContents(\n String projectId, String bucketName, Path destinationDirectory) {\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().getService();\n List\u003cBlobInfo\u003e blobs =\n storage\n .list(bucketName)\n .streamAll()\n .map(blob -\u003e blob.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Blob.html#com_google_cloud_storage_Blob_asBlobInfo__())\n .collect(Collectors.toList());\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManager.html transferManager = 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.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### 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_bucket_with_transfer_manager(\n bucket_name, destination_directory=\"\", workers=8, max_results=1000\n ):\n \"\"\"Download all of the blobs in a bucket, 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, for instance to\n accommodate blob names that include slashes.\n \"\"\"\n\n # The ID of your GCS bucket\n # bucket_name = \"your-bucket-name\"\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 # The maximum number of results to fetch from bucket.list_blobs(). This\n # sample code fetches all of the blobs up to max_results and queues them all\n # for download at once. Though they will still be executed in batches up to\n # the processes limit, queueing them all at once can be taxing on system\n # memory if buckets are very large. Adjust max_results as needed for your\n # system environment, or set it to None if you are sure the bucket is not\n # too large to hold in memory easily.\n # max_results=1000\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 blob_names = [blob.name for blob in bucket.list_blobs(max_results=max_results)]\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)."]]