Google Storage nodejs上传MIME TYPE

时间:2016-12-19 19:45:25

标签: node.js google-cloud-storage

如何为我上传的文件指定mime类型。我正在关注此nodejs示例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();
  });
}

我总是默认

  

应用/八位字节流

1 个答案:

答案 0 :(得分:4)

自己找到答案。您需要将此类元数据放入选项中。无法在任何文档中找到它。

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();
  });
}