Search code examples
pythonapigoogle-cloud-platformclientgoogle-cloud-storage

Download GCS object to your Vertex AI notebook in GCP


If you have a gs:// blob, how do you download that file from GCS to Vertex AI notebook in GCP using python client library?


Solution

  • To download GCS file to Vertex AI notebook refer following python code:

    from google.cloud import storage
    
    
    def download_blob(bucket_name, source_blob_name, destination_file_name):
        """Downloads a blob from the bucket."""
        
        # bucket_name = "your-bucket-name"
        # The ID of your GCS object
        # source_blob_name = "storage-object-name"      
        # destination_file_name = "/path/to/file"
    
        storage_client = storage.Client()
    
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.blob(source_blob_name)
        blob.download_to_filename(destination_file_name)
    
        print(
            "Downloaded storage object {} from bucket {} to file {}.".format(
                source_blob_name, bucket_name, destination_file_name
            )
        )
    

    Alternatively, if you want to download GCS file to jupyter directory you can use the command:

    !gsutil cp gs://BUCKET_NAME/OBJECT_NAME JUPYTER_LOCATION