Reputation: 15
I tried to download objects from google cloud storage using python instead of using google cloud SDK. Here is my code:
#Imports the Google Cloud client library
from google.cloud import storage
from google.cloud.storage import Blob
# Downloads a blob from the bucket
def download_blob(bucket_name, source_blob_name, destination_file_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket('sora_mue')
blob = bucket.blob('01N3P*.ubx')
blob.download_to_filename('C:\\Users\\USER\\Desktop\\Cloud')
print ('Blob {} downloaded to {}.'.format(source_blob_name,
destination_file_name))
the problem is after I run it, there is nothing happened and no results. Did I do something wrong here? Really appreciate any help!
Upvotes: 1
Views: 967
Reputation: 49473
TL;DR - You have defined a function in python but have not invoked it. Calling the function should actually execute the code to pull the blob from your Google Cloud Storage bucket and copy it to your local destination directory.
Also, you're taking in arguments in your function but are not using them and instead using hard-coded values for blob name, GCS bucket name, destination path. Although this will work, it does defeat the purpose of defining a function in the first place.
Here is a working example which uses the arguments in the function to make the call to GCS.
from google.cloud import storage
# Define a function to download the blob from GCS to local destination
def download_blob(bucket_name, source_blob_name, destination_file_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print ('Blob {} downloaded to {}.'.format(source_blob_name, destination_file_name))
# Call the function to download blob '01N3P*.ubx' from GCS bucket
# 'sora_mue' to local destination path 'C:\\Users\\USER\\Desktop\\Cloud'
download_blob('sora_mue', '01N3P*.ubx', 'C:\\Users\\USER\\Desktop\\Cloud')
# Will print
# Blob 01N3P*.ubx downloaded to C:\Users\USER\Desktop\Cloud.
Upvotes: 2