问题
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 Shell and type:
gsutil cp gs://bucket_name/file.xlsx C:\\Users\\User\\Documents\\file.xlsx
The whole point of me going through the trouble of making a bot on a 3rd party server is to automate a process as much as i can. So I was wondering if there is any way for me to write a python script that tells the GC SDK Shell to run the command above. Or even better: run the command above when I open a certain Excel file? maybe the latter is just too good to be true though. Let me know what you think, thanks!
Python code:
import google
from google.cloud import storage
def download_blob(bucket_name, source_blob_name, destination_file_name):
bucket_name = "gs://bot-example"
source_blob_name = "Koersen.xlsx"
destination_file_name = "C:\\Users\\User\\Documents\\Koersen.xlsx"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)```
回答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
def download_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.
回答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>"
def download_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)
来源:https://stackoverflow.com/questions/61103159/using-python-to-write-google-cloud-sdk-shell-commands