如何使用curl将文件发送到Google Cloud Function? [英] How to use curl to send file to Google Cloud Function?

查看:112
本文介绍了如何使用curl将文件发送到Google Cloud Function?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following Cloud function which is mainly based from the documentation:

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:

With the following curl command I try to upload a file to this function, the file can either be an image or a pdf:

curl -i -T myFile https://链接到云函数 --verbose

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

执行此操作时,我的函数将打印"PUT"并立即返回完成".因此,文件字典只是空的.

When doing this, my function prints the "PUT" and immediately returns "Done". Hence, the files dictionary is just empty.

有人可以帮我吗?

推荐答案

您应该能够执行以下操作:

You should be able to do something similar to:

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

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

An alternative -- possibly better -- solution would be to post the files directly to Google Cloud Storage and then trigger your Cloud Function to process these files (possibly moving from a source|receive bucket into a destination|processed bucket too):

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

次要:我认为,在这种情况下,POSTPUT更可取.

Minor: I think, for this case, POST is preferable to PUT.

这篇关于如何使用curl将文件发送到Google Cloud Function?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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