Search code examples
amazon-s3knox-amazon-s3-client

Change header on s3 file


If I have a file on s3 how can I change metadata of that file?

It looks like I can "copy" it to the same location with new headers which would effectively be the same thing.

I'm using knox as the node client to do this. The file in question already has the Content-Type header set to video/mp4 but I want to change it to application/octet-stream. The reason for this is so that this link will trigger the browser to download the resource instead of displaying it in the browser window.

Link to knox source for this function

var filename = "/example/file.mp4",
    headers = {'Content-Type': "application/octet-stream"};

client.copyFile(filename, filename, headers, function(error, resp) {
  //response is successful
});

The response is successful, but when I reload the resource in s3 I don't see that headers have changed.

I can see that the underlying API call is this:

'PUT /example/file.mp4 HTTP/1.1\r\nContent-Type: application/octet-stream
x-amz-copy-source: /bucket/example/file.mp4
Content-Length: 0\r\nDate: Thu, 28 Jan 2016 21:13:12 GMT
Host: cc-video-archives-dev.s3.amazonaws.com
Authorization: <redacted>=\r\nConnection: close\r\n\r\n',

Solution

  • I was missing this header:

    "x-amz-metadata-directive": "REPLACE"

    var filename = "/example/file.mp4",
      headers = {
        "x-amz-metadata-directive": "REPLACE",
        'Content-Type': "application/octet-stream"
      };
    
    client.copyFile(filename, filename, headers, function(error, resp) {
      //response is successful
    });