首页 > 解决方案 > 在 Google Cloud Function 中为 Google Cloud Storage 使用 compose 时出现 404 错误

问题描述

我有几个文件要上传到 Google Cloud Storage,然后使用compose. 我能够上传文件,但无法让撰写工作。我最初用 Nodejs 尝试它并得到奇怪的错误,现在尝试在用 python 编写的云函数中使用 compose。这是我正在使用的代码:

from google.cloud import storage

def compose_file(event, context):
    bucket_name = 'bucket-name-appspot.com'
    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)

    blob1 = bucket.blob('composettest/compose1.txt')
    blob2 = bucket.blob('composettest/compose2.txt')

    sources = [blob1, blob2]
    destination_blob_name = 'compose3.txt'
    print(blob1)
    print(blob2)

    destination = bucket.blob(destination_blob_name)
    print(destination)
    destination.content_type = "text/plain"
    destination.compose(sources)
    return destination

这是我不断遇到的错误。我知道该文件存在,因为我可以将其打印为 blob。有任何想法吗?是否可能存在某种权限错误?我怀疑是不是因为我可以毫无问题地创建和下载文件。我只是无法让 compose 与 python 或 nodejs 客户端库一起工作。

  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 449, in run_background_function
    _function_handler.invoke_user_function(event_object)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 268, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 265, in call_user_function
    event_context.Context(**request_or_event.context))
  File "/user_code/main.py", line 26, in compose_file
    destination.compose(sources)
  File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 3070, in compose
    retry=retry,
  File "/env/local/lib/python3.7/site-packages/google/cloud/storage/_http.py", line 63, in api_request
    return call()
  File "/env/local/lib/python3.7/site-packages/google/cloud/_http.py", line 438, in api_request
    raise exceptions.from_http_response(response)
google.api_core.exceptions.NotFound: 404 POST https://storage.googleapis.com/storage/v1/b/bucket-name-appspot.com/o/compose3.txt/compose?prettyPrint=false: Not Found

标签: pythongoogle-cloud-functionsgoogle-cloud-storage

解决方案


您无法在新文件中撰写。你拿一个文件,然后用另一个文件组成它。在您的情况下,您首先在 GCS 中创建一个空的 compose3.txt,然后您可以创建您的命令。或者,您使用 compose1 作为目标,仅使用 compose2 作为源,并且两者都组合在一起。


推荐阅读