“ path”参数必须为字符串类型。接收类型对象nodejs谷歌云存储对象上传

时间:2019-12-21 12:56:07

标签: node.js google-cloud-storage

我正在尝试使用nodejs将图像上传到Google云存储桶。我遵循了文档,但是当我尝试上传图像时,出现错误““ path”参数必须为字符串类型。收到的类型为对象“。我将文件路径作为字符串传递,但是仍然出现错误。这是我的代码

const multer = require('multer');
const fs = require('fs');
const randomString = require('randomstring');
const path = require('path');
const { Storage } = require('@google-cloud/storage');
const configFile = require('../configs/memesdev.json');

exports.upload = (req, res) => {
  const storageObject = new Storage({ keyFilename: configFile });
  uploadFile(storageObject);
}
async function uploadFile(storageObject) {
  await storageObject.bucket("images_memespool").upload("D:\\sotari\\eight.jpg", {});
  console.log('file uploaded');
}

有人可以帮我吗?谢谢

1 个答案:

答案 0 :(得分:1)

您在身份验证时遇到错误,必须将字符串传递给keyFilename

exports.upload = (req, res) => {
  const storageObject = new Storage({ 
     keyFilename: path.join(__dirname, '..', 'configs', 'memesdev.json')
  });
  uploadFile(storageObject);
}