javascript - 如何使用强大的 npm 将文件上传到谷歌云

标签 javascript node.js google-cloud-platform formidable

我尝试使用 formidable 将文件上传到我的 Google Cloud 存储桶(使用 https://www.npmjs.com/package/formidable-serverless?activeTab=readme 中的示例),但仍然无法将其上传到我的存储桶。

这是我的 .env 文件

GCS_BUCKET = <bucket-name>
GCLOUD_PROJECT = <project-name>
GCS_KEYFILE = <key file location>

这是我尝试过的

const formidable = require('formidable-serverless');
const http = require('http');
const util = require('util');

http.createServer(function(req, res) {
  if (req.url == 'gs://<bucket-name>/' && req.method.toLowerCase() == 'post') {
    // parse a file upload
    const form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload:\n\n');
      res.end(util.inspect({fields: fields, files: files}));
    });

    return;
  }

  // show a file upload form
  res.writeHead(200, {'content-type': 'text/html'});
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);

我也试过 Mutler ( https://www.npmjs.com/package/multer-google-storage ) ,谁能帮忙举个例子

最佳答案

我知道这是一个迟到的帖子。

只是为了让问题无法回答。

const express = require('express');
const formidable = require('formidable');
const { Storage } = require('@google-cloud/storage');
const bucket = 'yourBucketName';

async function uploadFile(bucket, filename) {
const storage = new Storage();
const params = { metadata: { cacheControl: 'public, max-age=31536000' } 
};
    await storage.bucket(bucket).upload(filename, params);
    console.log(`${filename} uploaded to ${bucket}.`);
}

const app = express();

app.get('/', (req, res) => {
    res.send(`
    <h2>With <code>"express"</code> npm package</h2>
    <form action="/api/upload" enctype="multipart/form-data" method="post">
      <div>Text field title: <input type="text" name="title" /></div>
      <div>File: <input type="file" name="someExpressFiles" multiple="multiple" /></div>
      <input type="submit" value="Upload" />
    </form>
  `);
});

app.post('/api/upload', (req, res, next) => {
    const form = formidable();

    form.parse(req, (err, fields, files) => {
        if (err) {
            next(err);
            return;
        }
        let imgPath = files.someExpressFiles.path;
        uploadFile(bucket, imgPath).catch(console.error);
        res.json({ fields, files });
    });
});

app.listen(8080, () => {
    console.log('Server listening on http://localhost:8080 ...');
});

关于javascript - 如何使用强大的 npm 将文件上传到谷歌云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60370354/

相关文章:

node.js - Node 应用程序因空字符串作为密码而崩溃

google-cloud-platform - 使用 Terraform 创建 GCP VPC 时出错 - 无法获取 token : unexpected EOF

javascript - 在 React 中继承常见的 props 和方法的最佳实践是什么?

javascript - 使用 AJAX 和 Jquery 从 MySQL 填充表单字段

Javascript setInterval 性能因多个动画而下降

google-cloud-platform - 为 Google Cloud Function 使用自定义域

linux - 运行 hadoop 集群时在 Google Cloud Platform 上获取 'sudo: unknown user: hadoop' 和 'sudo: unable to initialize policy plugin error'

javascript - 使用 Firebase-util 连接表时,Firebase 事件未正确触发

node.js - 从 Mongoose 的子文档 ID 中查找父文档

javascript - Node.js Express 和 Mongoose,在 save() 之后渲染