Using Python To Write Google Cloud Sdk Shell Commands
I made a bot that's running on a google cloud VM. I got everything working and in order to access the data that my bot is writing out, I have to go open up the Google Cloud SDK She
Solution 1:
Sample code using Python to download an object file.xlsx
into your local directory C:\\Users\\User\\Documents\\file.xlsx
from a Cloud Storage bucket gs://bucket_name
:
from google.cloud import storage
defdownload_blob(bucket_name, source_blob_name, destination_file_name):
"""Downloads a blob from the bucket."""
bucket_name = "gs://bucket_name"
source_blob_name = "file.xlsx"
destination_file_name = "C:\\Users\\User\\Documents\\file.xlsx"
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 {}.".format(
source_blob_name, destination_file_name
)
)
More info about Cloud Storage for Python can be found in the documentation.
Regarding your Excel question, you can go ahead and use the openpyxl library and adjust your code accordingly.
Solution 2:
A few things that I've noticed looking into your code:
- I don't see you actually calling the download_blob() function.
- Remember that your function takes 3 parameters: bucket_name, source_blob_name, destination_file_name. These need to be specified when calling the function.
- bucket_name should have the name of your bucket, not its link for gsutil.
I have modified that example accordingly and tested it. Here is what should work:
from google.cloud import storage
bucket_name = "<BUCKET-NAME>"
source_blob_name = "<FILE-NAME>"
destination_file_name = "<PATH-TO-THE-DESTINATION-FILE>"defdownload_blob(bucket_name, source_blob_name, destination_file_name):
"""Downloads a blob from the bucket."""
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 {}.".format(
source_blob_name, destination_file_name
)
)
download_blob(bucket_name, source_blob_name, destination_file_name)
Post a Comment for "Using Python To Write Google Cloud Sdk Shell Commands"