Kishintai
Kishintai

Reputation: 135

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

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?

Upvotes: 0

Views: 1689

Answers (1)

DazWilkin
DazWilkin

Reputation: 40136

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.

Upvotes: 1

Related Questions