3
votes

How do I specify mime type for the file that I am uploading. I am following this nodejs example https://cloud.google.com/storage/docs/object-basics#storage-upload-object-nodejs

```

function uploadFile (bucketName, fileName, callback) {
  // Instantiates a client
  const storageClient = Storage();

  // References an existing bucket, e.g. "my-bucket"
  const bucket = storageClient.bucket(bucketName);

  // Uploads a local file to the bucket, e.g. "./local/path/to/file.txt"
  bucket.upload(fileName, (err, file) => {
    if (err) {
      callback(err);
      return;
    }

    console.log(`File ${file.name} uploaded.`);
    callback();
  });
}

I always get default

application/octet-stream

1

1 Answers

6
votes

Found the answer myself. You need to put this kind of metadata into options. Couldn't find it in any documentation.

function uploadFile (bucketName, fileName, callback) {
  // Instantiates a client
  const storageClient = Storage();

  // References an existing bucket, e.g. "my-bucket"
  const bucket = storageClient.bucket(bucketName);

  // STARTING FROM HERE
  const options = {
    metadata: {
      contentType: 'image/jpeg',
    },
  }
  // TO HERE

  // Uploads a local file to the bucket, e.g. "./local/path/to/file.txt"
  bucket.upload(fileName, options, (err, file) => {
    if (err) {
      callback(err);
      return;
    }

    console.log(`File ${file.name} uploaded.`);
    callback();
  });
}