简体   繁体   English

在node.js中将文件上传到谷歌云存储时如何合并目录名称

[英]How to incorporate directory names when uploading a file to google cloud storage in node.js

In my express server I have created a page to upload image files from a local machine.在我的快速服务器中,我创建了一个页面来从本地机器上传图像文件。 It creates a temporary directory path in my root directory that is specified by the form data from the upload page.它在我的根目录中创建一个临时目录路径,该路径由上传页面中的表单数据指定。 I am trying to pass in the temporary directory names in the image upload, but no matter what I do it only uploads image.jpg instead of category/subcat/image.jpg , giving no organization to the bucket it is being uploaded to.我试图在图像上传中传递临时目录名称,但无论我做什么,它都只上传image.jpg而不是category/subcat/image.jpg ,没有组织它正在上传到的存储桶。

The form looks like:表格看起来像:

  <form action="/upload" method="POST">
      <input type="text" name="category" autocomplete="off"/>
      <input type="text" name="subcat" autocomplete="off"/>
      <input type="file" id="img" name="image" accept="image/*"/>
      <button type="submit">Upload</button>
  </form>

With my code (using fs and multer ) I create a directory path that looks like:使用我的代码(使用fsmulter ),我创建了一个如下所示的目录路径:

./category/subcat/image.jpg    //the names are dynamically generated by the form data

This is what I want the file to look like when it is added to the bucket.这就是我希望文件添加到存储桶时的样子。 However, when I upload, it is only uploading image.jpg and not category/subcat/image.jpg .但是,当我上传时,它只上传image.jpg而不是category/subcat/image.jpg
Here is the post request in the server file:这是服务器文件中的 post 请求:

app.post("/upload", upload.single("image"), (req, res) => {
  let imagePath = `${req.body.categories}/${req.body.subcat}/${req.imageName}`

  async function uploadFile() {
      await storage.bucket(bucketName).upload(imagePath)
    }
  uploadFile().catch(console.error);

  res.redirect("/upload#upload");
});

Any idea why the uploadFile() is not taking the full image path I am providing?知道为什么uploadFile()没有采用我提供的完整图像路径吗? How can I amend the function to upload a file with it's directories?如何修改上传文件及其目录的功能? No matter what I add to upload() or how I define imagePath it only uploads image.jpg instead of category/subcat/img.jpg .无论我添加什么到upload()或我如何定义imagePath它只上传image.jpg而不是category/subcat/img.jpg

If you want to specify the destination path of the uploaded file, you will need to pass a second argument toupload() , which is a UploadOptions object.如果要指定上传文件的目标路径,则需要将第二个参数传递给upload() ,这是一个UploadOptions对象。 You can see that it has a property called destination :您可以看到它有一个名为destination的属性:

The place to save your file.保存文件的地方。 If given a string, the file will be uploaded to the bucket using the string as a filename.如果给定一个字符串,文件将使用该字符串作为文件名上传到存储桶。 When given a File object, your local file will be uploaded to the File object's bucket and under the File object's name.当给定一个 File 对象时,您的本地文件将被上传到 File 对象的存储桶并在 File 对象的名称下。 Lastly, when this argument is omitted, the file is uploaded to your bucket using the name of the local file.最后,当省略此参数时,文件将使用本地文件的名称上传到您的存储桶。

Since you are not providing a destination, it's taking the basename of the file you provided as the default.由于您没有提供目的地,因此将您提供的文件的基本名称作为默认值。 You should simply provide the path name you want:你应该简单地提供你想要的路径名:

storage.bucket(bucketName).upload(imagePath, { destination: "path/you/choose.jpg" })

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

相关问题 在 node.js 中将文件上传到 Google Cloud Storage 时出错 - Error when uploading file to Google Cloud Storage in node.js 使用Node.js上传到Google云存储 - Uploading to google cloud storage with 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 - Google Cloud Storage Delete file in a specific path Node.js 使用 Node.js 将 pdf 文件上传到 Google Cloud Storage - Upload a pdf file to Google Cloud Storage with Node.js 如何在 Google Cloud Storage (Node.js) 中管理对 object 的权限? - How to manage permissions to an object in Google Cloud Storage (Node.js)? Google Cloud Storage API和Node.js - Google Cloud Storage API and Node.js 如何使用node.js为要上载到Google云端存储的文件设置内容类型 - How to set the Content-Type for a file being uploaded to Google Cloud Storage using node.js Google Cloud Storage-如何在Node.JS中接收文件的特定生成 - Google Cloud Storage - how to receive specific generation of file in Node.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM