Search code examples
pythonlinuxgoogle-cloud-storagesoftware-design

how to download file from Google cloud storage using python


I am working with Google Cloud Storage via Python, so wanted to know how to download the file via Python code. I know, a similar example is given in documentation but wanted to know the best way to authenticate via credentials.


Solution

  • Step 1: Install:

    pip3 install google-cloud-storage
    pip install --upgrade google-cloud-storage
    

    Step 2: Followed by authenticating yourself using credentials(like project id, private key, private key_id, client email, and client id).

    Step 3: From official documentation on Downloading objects:

    def download_blob(bucket_name, source_blob_name, destination_file_name):
        """Downloads a blob."""
    
        storage_client = storage.Client()
    
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.blob(source_blob_name)
        blob.download_to_filename(destination_file_name)
    
        print(
            "Blob {} downloaded to file path {}. successfully ".format(
                source_blob_name, destination_file_name
            )
        )