ekeren
ekeren

Reputation: 3458

Google Storage API custom header on node.js

I am using googleapis.auth.JWT to authenticate and request multipart upload to upload JSON files into google storage, it is working as expected.

Here is the code:

  var data = JSON.stringify(json);
  var metadata = {
      name: "name"
      contentLanguage: "en",
      acl: [...]
    };

  authClient.authorize(function(err, tokens) {
      if (err) {...}
      request.post({
        'url': 'https://....',
        'qs': {
          'uploadType': 'multipart'
        },  
        'headers' : { 
          'Authorization': 'Bearer ' + tokens.access_token
        },  
        'multipart':  [{  
          'Content-Type': 'application/json; charset=UTF-8',
          'body': JSON.stringify(metadata)
        },{ 
          'Content-Type': 'application/json',
          'body': data
        }]
      }, done);      
    });
  });
}

According to google here if I want to include custom headers I need to add it in the form of "x-goog-meta-mycustomheader"

When I change my above metadata object to this:

 var metadata = {
      name: "name"
      contentLanguage: "en",
      "x-goog-meta-something": "completely different",
      acl: [...]
  };

It doesn't have any affect.

How do I add custom headers when I upload an object to Google Storage?

EDIT:

Please notice that this is a multipart upload that uses the first part body as the metadata of the second part (which is the actual part) see details here

Especially:

If you have metadata that you want to send along with the data to upload, you can make a single multipart/related request. As with simple, media-only requests, this is a good choice if the data you are sending is small enough to upload again in its entirety if the connection fails.

Metadata part: Must come first, and Content-Type must match one of the accepted metadata formats.

Media part: Must come second, and Content-Type must match one the method's accepted media MIME types.

This is why I use the metadata as the header section, I also tried all other combinations like putting the "x-goog-meta-something" in all other places

Upvotes: 3

Views: 723

Answers (1)

jterrace
jterrace

Reputation: 67073

Take a look at the JSON request builder here: https://developers.google.com/storage/docs/json_api/v1/objects/insert

You'll notice that metadata is a separate key in the body. So you'll want something like:

var metadata = {
      name: "name"
      contentLanguage: "en",
      metadata: {
        "something": "completely different",
      },
      acl: [...]
  };

Upvotes: 2

Related Questions