Using python to write Google Cloud SDK Shell commands

落花浮王杯 提交于 2020-04-18 05:45:41

问题


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:

  1. I don't see you actually calling the download_blob() function.
  2. Remember that your function takes 3 parameters: bucket_name, source_blob_name, destination_file_name. These need to be specified when calling the function.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!