Wednesday, August 6, 2014

Multipart Upload with cURL

Occasionally I find that I need to manually do a multipart upload from the command line. It is always a pain to remember how to craft such a curl command. Here is an example of how to craft a multipart upload for Google Cloud Storage using curl.

First, create a file named something like "multipart.txt" that contains your multipart message. The first line must be the boundary (with -- in front), and the last line must be the boundary with a -- in front and a -- at the end:

--myboundary
Content-Type: application/json; charset=UTF-8

{
  "name": "myObject.txt",
  "contentType": "text/plain"
}

--myboundary
Content-Type: text/plain


This is some text data type.

--myboundary--


Okay, now upload that sucker with curl:

$> curl -X POST -T multipart.txt -H 'Content-Type: multipart/related; boundary=myboundary' 'https://www.googleapis.com/upload/storage/v1/b/bucket_name/o?uploadType=multipart'

Note that we used -T (--upload-file) instead of --data. --data treats data like a web browser would, which is not what we want in this case. We could also have used "--data-binary @multipart.txt" for the same effect, but that's so may more characters.

No comments:

Post a Comment