【问题标题】:google cloud platform blob.download_to_file where the file is downloadedgoogle cloud platform blob.download_to_file 文件下载位置
【发布时间】:2019-03-01 14:46:17
【问题描述】:
gcloud beta functions deploy gcp-data-transfer --project probable-scout-216702 --runtime python37 --trigger-bucket connector-test-data-securonix --entry-point detect_file

以上是我正在使用的谷歌云功能。这是我的谷歌云存储桶上的触发器。我的 gcloud 功能正在运行,但我不知道文件应该下载到哪里。我能够将它存储在 /tmp/ 目录中,但它仍然不在我自己的系统上,我不知道哪个 /tmp 它正在下载。我正在使用以下代码:

def detect_file(file, context):
    destination_file_name = "/Securonix/file1.txt"
    f = open("/Securonix/file1.txt",'wb')
    bucket = validate_message(file, 'bucket')
    name = validate_message(file, 'name')
    print("here")
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket)
    blob = bucket.blob(name)
    blob.download_to_file(f)
    print('Blob {} downloaded to {}.'.format(
        name,
        destination_file_name))


def validate_message(message, param):
    var = message.get(param)
    if not var:
        raise ValueError('{} is not provided. Make sure you have \
                          property {} in the request'.format(param, param))
    return var

我收到错误:

FileNotFoundError: [Errno 2] No such file or directory: '/Securonix/file1.txt'

【问题讨论】:

    标签: google-cloud-platform blob google-cloud-storage google-cloud-functions


    【解决方案1】:

    使用/tmp/然后你的文件名,这是因为你不能在云功能上创建文件,除了/tmp/文件夹。

    你的字符串是这样的:

    /tmp/Securonix/file1.txt
    

    【讨论】:

      【解决方案2】:

      Cloud Functions 执行环境是totally read-only except for /tmp/,所以你必须下载到那个目录。

      第二个问题(以及您看到 FileNotFoundError 而不是 PermissionError 的原因)是云函数运行时上不存在 /Securonix/ 目录。 open() 不会为您创建新目录,您必须先创建目录才能在其中创建文件。

      另外:不要忘记delete the file when you are done,因为它存储在内存中。

      【讨论】:

      • 我不知道如何在这个 Cloud Functions 执行环境中创建目录,同样不知道如何访问其中的 /tmp/ 文件夹。
      • 您可以使用os.mkdir() 函数创建目录。您可以使用open("/tmp/yourfile", "r") 访问那里的文件。如果您尝试在云功能中下载文件,然后从其他地方访问它,则您可能使用了错误的工具来完成这项工作 - 您应该将文件直接下载到您想要使用它的地方。
      • 谢谢大卫,我想要一个数据管道,以便谷歌云存储桶(其中的对象)中的任何更改都可以存储在我的系统上。我在想 pub-sub 可以为我做这件事。但我不确定 pub sub 是否允许 GC 存储上的发布者与我的 ssh 机器上的订阅者对话。
      • 您可以从任何可以访问互联网的机器上运行拉取订阅者。
      • 云函数不提供任何持久存储。您可以使用云功能在每次更改时将对象复制到“备份存储桶”。或者,如果您想要本地机器上的文件,您可以在本地机器上运行 pubsub pull 订阅者并将文件保存在本地。
      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 2019-12-19
      • 2020-10-18
      • 2016-04-08
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多