简体   繁体   English

在 node.js 中将文件上传到 Google Cloud Storage 时出错

[英]Error when uploading file to Google Cloud Storage in node.js

I have this form element:我有这个表单元素:

  <form action="/upload" method="POST">
      <input type="file" id="img" name="image" accept="image/*" class="btn"/>
      <button type="submit" class="btn">Upload</button>
  </form>

When I submit the form, the post request is getting the file name of the image through body-parser.当我提交表单时, post请求通过 body-parser 获取图像的文件名。

app.post("/upload", (req, res) => {
  console.log(req.body.image);
  const filename = req.body.image;
  async function uploadFile() {
    await storage.bucket(bucketName).upload(filename)
  }
  uploadFile().catch(console.error);
  res.redirect("/upload");
});

The console.log returns just the file name image.jpg without the path. console.log只返回没有路径的文件名image.jpg When the uploadFile() method is called it returns an error saying that the file path does not exist, because it is saying the file path is: C:\\\\Users\\Owner\\Desktop\\Serverfolder\\image.jpg which is completely wrong.uploadFile()方法被调用时,它返回一个错误,说文件路径不存在,因为它说文件路径是: C:\\\\Users\\Owner\\Desktop\\Serverfolder\\image.jpg这是完全错误的。

So how do I prepend the right file path that google cloud will accept?那么我如何预先设置谷歌云将接受的正确文件路径? I know that the browser will protect the actual file path for file uploads, but it is not filling in the temporary or fakepath so that the image can be uploaded.我知道浏览器会保护文件上传的实际文件路径,但它没有填写临时或假路径以便上传图像。

Please help, because the google cloud documentation is annoyingly vague on the matter...请帮忙,因为谷歌云文档在这个问题上非常含糊......

The file path should be referenced within the scope of your server.应该在您的服务器范围内引用文件路径。 What you can do is store the file in a folder called uploads with multer.您可以做的是将文件存储在一个名为 uploads with multer 的文件夹中。 Then reference the relative path into uploadFile() and upload it.然后将相对路径引用到 uploadFile() 中并上传。 It should get the file then.然后它应该得到文件。 Once you've uploaded you can then use the fs node package to delete the file.上传后,您可以使用 fs 节点包删除文件。

Here is the multer configuration i used这是我使用的multer配置

// Multer config
const multer = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, 'blog-images/')
    },
    filename: function (req, file, cb) {
        let tempImageName = file.mimetype.toString().split('/')[1];
        req.imageName = tempImageName
      cb(null, tempImageName)
    }
  })
  var upload = multer({ storage: storage })

And this is the code that uploads it这是上传它的代码

app.post('/upload', upload.single('image'), (req, res)=>{
 //blog-images below is the custom folder i created. I can easily reference it
//since it is a part of the server
  let imagePath = `blog-images/${req.imageName}`
  // The code is split here but essentially, it solves the file path problem
  uploadBlog(imagePath, req.body)
  .then( uploadRef => {
    // The fs module deletes it from the folder so you don't have to worry about
    // uncessary files
    fs.unlink(imagePath, ()=>{
      res.send('upload success')
     })
   }).catch(err => console.log(err.message))
})

you have to put all the header in the hidden text tag你必须把所有的标题放在隐藏的文本标签中

 <form action="https://storage.googleapis.com/travel-maps" method="post" enctype="multipart/form-data"> <input type="text" name="key" value="test-object"> <input type="hidden" name="Content-Type" value="image/jpeg"> <input type="hidden" name="success_action_redirect" value="https://www.example.com/success_notification.html"> <input type="hidden" name="policy" value=""> <input type="hidden" name="x-goog-algorithm" value="GOOG4-RSA-SHA256"> <input type="hidden" name="x-goog-credential" value="example_account@example_project.iam.gserviceaccount.com/20191102/auto/storage/goog4_request"> <input type="hidden" name="x-goog-date" value="20191102T043530Z"> <input type="hidden" name="x-goog-signature" value="58bc39b8f604ee1f18171fee4828ef8967f3d2721676570e115d68c2f133820cbb833976f18955516b2b7d0c3d9660fea613a2ad90c240bd02c1eefa4a55e9038ce74dcfdd34e278ea0436e261131a36fa4e922f0a077ca1c9842f654928aac3ca7f9341075f9db275d8286b5ef13e7f91b4837e77b2a6dbea83f86b90f848331053d8a6b1fbc26787992e7fb819a2005bae9b3026b9c7d1158e88e4a2018f13757083c7873241d2dfe6ea46a17cd6f3d090f3e0da44ccfbd6bc425124de1bea744a32f3ab175672a991ef274cd83550eca57ea591b85fa9799098a38ec552dc3ec679c431491444820624f5c4ba0c8cf87d60af89899afce2a90325c6966dcf"> <input name="file" type="file"> <input type="submit" value="Upload"> </form>

you can refer to this documentation for further information您可以参考此文档以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Node.js上传到Google云存储 - Uploading to google cloud storage with Node.js 在node.js中将文件上传到谷歌云存储时如何合并目录名称 - How to incorporate directory names when uploading a file to google cloud storage in node.js FetchError,原因:读取ECONNRESET; 将图片从 node.js 上传到谷歌云存储时 - FetchError, reason: read ECONNRESET; When uploading images to google cloud storage from node.js 使用 node.js 和 Glob 将多个文件从 Google Cloud VM 上传到 Google Cloud Storage - Uploading multiple files from a Google Cloud VM to Google Cloud Storage using node.js and Glob 使用 Node.js 将 pdf 文件上传到 Google Cloud Storage - Upload a pdf file to Google Cloud Storage with Node.js 谷歌云存储删除特定路径中的文件 Node.js - Google Cloud Storage Delete file in a specific path Node.js Google Cloud Storage API和Node.js - Google Cloud Storage API and Node.js 使用 Multer 将文件上传到 Google Cloud Storage 时出现奇怪的错误 - Weird error when uploading file to Google Cloud Storage with Multer 使用 node.js 在谷歌云存储中上传超过 1 GB 的大数据 - Uploading large data more then 1 GB in google cloud storage with node.js 将文件上传到Google驱动器中的Node.JS错误 - Node.JS Error in uploading file to google drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM