简体   繁体   中英

Uploading multiple files to Google Cloud using multer and Node.js

I am trying to upload multiple files to Google Cloud bucket using Node.js and multer. It works with multer.single function but I don't know how to upload multiple images at once.

const bucket = gc.bucket('still-cover');
// Multer is required to process file uploads and make them available via
// req.files.
const multer = Multer({
  storage: Multer.memoryStorage(),
  limits: {
    fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
  },
});

Router.post('/test/upload',multer.array('files',5),async(req,res)=>{
  if (!req.files) {
    res.status(400).send('No file uploaded.');
    return;
  }
      
  // Create a new blob in the bucket and upload the file data.

  const blob = bucket.file(req.files.originalname);
  const blobStream = blob.createWriteStream();
  blobStream.on('finish', res => {});

  blobStream.on('finish', () => {
    // The public URL can be used to directly access the file via HTTP.
    const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`
   
    res.status(200).send(publicUrl);
  });

  blobStream.end(req.files.buffer);
});

You can use multer.array('files', numberoffiles) or multer.any() to upload files to your Google Cloud Storage Bucket. You can use the following code to upload multiple files using Multer:

const express = require('express');
const path = require('path');
const cors = require('cors');
const Multer = require('multer');
const bodyParser = require('body-parser');

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

// Creates a client
const storage = new Storage();

const bucket = storage.bucket('YOUR_BUCKET_NAME')

const PATH = './public/';

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

const multer = Multer({
    storage: Multer.memoryStorage(),
    limits: {
      fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
    },
  });


app.get('/', function(req, res){
    res.json({
        json: "json"
    });
})

// You can also use multer.array('data', numberofFiles)

app.post('/', multer.any(), function(req, res) {
    console.log(req.files);
    var counter = 0;
    if (!req.files) {
        res.status(400).send('No file uploaded.');
        return;
      }
      
    //  Create a new blob in the bucket and upload the file data.
    req.files.forEach((fil) => {
        const blob = bucket.file(fil.originalname);
        const blobStream = blob.createWriteStream();


        blobStream.on('finish', () => {
            counter+=1
            // The public URL can be used to directly access the file via HTTP.
            const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`

            if(counter>=2){
                res.status(200).send(publicUrl);
            }
        
        });
        
        blobStream.end(req.files.buffer);
    });
    
    
});

app.listen(3000, function () {
    console.log("Working on port 3000");
});


On the line blobStream.end(req.files.buffer); replace files.buffer with fil.buffer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM