POST request to google cloud storage using Curl

2019-08-12 03:25发布

问题:

In the following Curl command, content type and content length and access bearer are attached to my bucket URI to upload a file to google cloud storage.

C:\softwares\curl>curl -X POST -H "Content-Type:application/json" \
    -H "Content-Length:100" \
    -H "Authorization: Bearer <MY_OAUTH2_TOKEN>" \ 
    "https://www.googleapis.com/upload/storage/v1/b/kids-74096.appspot.com/o?uploadType=media&name=newcurl" \
    -d '{"text":"something"}'

But I am getting this error:

curl: (92) HTTP/2 stream 1 was not closed cleanly: PROTOCOL_ERROR (err 1)

回答1:

Make sure to include a Content-Type header, and ensure that your Content-Length matches the length of the data you're sending. Here's an example:

curl -k -v -X POST \
-H "Authorization: Bearer <your_oauth2_token>" -H "Content-Length: 8" \
-H "Content-Type: text/plain" \
'https://www.googleapis.com/upload/storage/v1/b/your-bucket/o?uploadType=media&name=yourobjectname' \
-d 'yourdata'

As suggested in the comments, you might find it easier to use gsutil or the Cloud Storage client libraries to accomplish this.