【问题标题】:Using python to write Google Cloud SDK Shell commands使用python编写Google Cloud SDK Shell命令
【发布时间】:2020-07-21 00:15:19
【问题描述】:

我制作了一个在谷歌云虚拟机上运行的机器人。我一切正常,为了访问我的机器人正在写出的数据,我必须打开 Google Cloud SDK Shell 并输入:

gsutil cp gs://bucket_name/file.xlsx C:\\Users\\User\\Documents\\file.xlsx

我在 3rd 方服务器上制作机器人的麻烦的全部意义在于尽可能地自动化流程。所以我想知道是否有任何方法可以让我编写一个告诉 GC SDK Shell 运行上述命令的 python 脚本。甚至更好:当我打开某个 Excel 文件时运行上面的命令?也许后者实在太好了,令人难以置信。让我知道你的想法,谢谢!

Python 代码:

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)```

【问题讨论】:

    标签: python google-cloud-sdk


    【解决方案1】:

    使用 Python 将对象 file.xlsx 从 Cloud Storage 存储桶 gs://bucket_name 下载到本地目录 C:\\Users\\User\\Documents\\file.xlsx 的示例代码:

    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
            )
        )
    

    有关 Cloud Storage for Python 的更多信息,请访问documentation

    关于您的 Excel 问题,您可以继续使用 openpyxl 库并相应地调整您的代码。

    【讨论】:

    • 感谢您的回答!我试图遵循 GC 文档,但它似乎没有做任何事情。不覆盖旧文件,也不写入新文件。此外,键入 storage_client = storage.Client() 后的“客户端”一词在我的 python 脚本中没有着色。也许我还没有安装某个模块?我安装了以下内容: pip install --upgrade google-api-python-client , pip install --upgrade google-cloud , pip install --upgrade google-cloud-storage
    • 要安装和使用 Cloud Storage Python 客户端库,您只需要 pip install --upgrade google-cloud-storage,如 documentation。然后,您可能希望在使用客户端库之前按照其他documentation 进行身份验证。如果这不起作用,您能否提供一些代码或您收到的任何错误消息,以便我们为您提供帮助?
    • 问题是我没有收到错误。代码执行并需要几秒钟。然后我在界面中收到PS C:\Users\User\Documents\Bot> 消息,让我知道执行已完成。我的 Google Cloud 存储桶称为 bot-example,我尝试从那里下载的文件是一个名为“Koersen”的 Excel 文件。我的桌面上已经有一个“Koersen”Excel 文件,我想将下载的文件保存到该现有位置。我将我的代码添加到我的第一个问题中。提前致谢。
    【解决方案2】:

    我在查看您的代码时注意到的几件事:

    1. 我没有看到您实际调用 download_blob() 函数。
    2. 请记住,您的函数需要 3 个参数:bucket_namesource_blob_namedestination_file_name。这些需要在调用函数时指定。
    3. bucket_name 应该是您的存储桶的名称,而不是它的 gsutil 链接。

    我已经相应地修改了该示例并对其进行了测试。以下是应该起作用的:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      相关资源
      最近更新 更多