How to use curl to send file to Google Cloud Function?

╄→гoц情女王★ 提交于 2020-01-25 09:34:11

问题


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!"

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://Link-to-cloudfunction --verbose

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

Can anyone help me out?


回答1:


You should be able to do something similar to:

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

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

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



来源:https://stackoverflow.com/questions/55429665/how-to-use-curl-to-send-file-to-google-cloud-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!