使用python编写Google Cloud SDK Shell命令 [英] Using python to write Google Cloud SDK Shell commands

查看:320
本文介绍了使用python编写Google Cloud SDK Shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个在Google云端VM上运行的机器人.我一切正常,为了访问我的机器人正在写的数据,我必须打开Goog​​le Cloud SDK Shell并输入:

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

在第3方服务器上制作机器人的过程中,我遇到的全部问题就是要尽可能地自动化流程.因此,我想知道是否有什么办法可以编写一个Python脚本来告诉GC SDK Shell运行上述命令.甚至更好:打开某个Excel文件时运行上面的命令?也许后者实在太好了以至于不能成立.让我知道您的想法,谢谢!

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代码:

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

推荐答案

我注意到对您的代码的注意:

A few things that I've noticed looking into your code:

  1. 我看不到您实际上在调用 download_blob()函数.
  2. 请记住,您的函数具有3个参数: bucket_name source_blob_name destination_file_name .调用函数时需要指定这些.
  3. bucket_name 应该具有存储桶的 name ,而不是gsutil的链接.
  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)

这篇关于使用python编写Google Cloud SDK Shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆