【问题标题】:How to use curl to send file to Google Cloud Function?如何使用 curl 将文件发送到 Google Cloud Function?
【发布时间】:2019-03-30 08:41:44
【问题描述】:

我有以下主要基于文档的云功能:

def hello_world(request):
    if request.method == 'GET':
        print('GET')
    elif request.method == 'PUT':
        print("PUT")
    # This code will process each file uploaded
    files = request.files.to_dict()
    print("files: ",files)
    for file_name, file in files.items():
        file.save(get_file_path(file_name))
        print('Processed file: %s' % file_name)

    # Clear temporary directory
    for file_name in files:
        file_path = get_file_path(file_name)
        os.remove(file_path)

    return "Done!"

使用以下 curl 命令我尝试将文件上传到此函数,该文件可以是图像或 pdf:

curl -i -T myFile https://Link-to-cloudfunction --verbose

执行此操作时,我的函数会打印“PUT”并立即返回“Done”。因此,文件字典只是空的。

谁能帮帮我?

【问题讨论】:

    标签: python-3.x curl google-cloud-functions


    【解决方案1】:

    你应该能够做类似的事情:

    curl \
    --form file1=@filename1 \
    --form file2=@filename2 \
    ...\
    https://...
    

    另一种可能更好的解决方案是将文件直接发布到 Google Cloud Storage,然后触发您的 Cloud Function 来处理这些文件(也可能从源|接收存储桶移动到目标|已处理存储桶) :

    https://cloud.google.com/functions/docs/writing/http#uploading_files_via_cloud_storage

    Minor:我认为,在这种情况下,POSTPUT 更可取。

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2019-08-20
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 2021-04-25
      相关资源
      最近更新 更多