A simple storage to upload files into Google Drive Teams Folders by Multer.
Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files. It is written on top of busboy for maximum efficiency.
NOTE: Multer will not process any form which is not multipart (multipart/form-data).
This README is also available in others languages.
- Português (Português Brasil)
npm install --save multer-google-drive-teams-storage
const express = require('express');
const multer = require('multer');
const { uuid } = require('uuidv4');
const { google } = require('googleapis');
const bodyparser = require('body-parser');
const GoogleStorage = require('google-drive-storage');
const auth = new google.auth.GoogleAuth({
keyFile: './your-google-service-account-key-file.json',
scopes: [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.photos.readonly'
],
});
const drive = google.drive({version: 'v3', auth });
const app = express();
app.use(bodyparser.urlencoded({ limit: '50mb', extended: false }))
const upload = multer({
storage: GoogleStorage({
drive: drive,
driveId: '1iaudFygUYG_GKgkgKGJHGhjghjghjgtirtrduu',
filename: function (req, file, callback) {
const fileName = `${uuid()}-${file.originalname}`;
callback(null, fileName);
},
})
});
app.post('/upload', upload.single('fileField'), (req, res) => {
const {
originalname,
fileId
} = req.file;
res.send('File ' + originalname + ' with the id ' + fileId + ' are sent.');
});
GoogleDriveTeamsStorage accepts an options object. You need to specify a two required parameters in this object.
The following are the options that should be passaed to GoogleDriveTeamsStorage.
Key | Decription | Type | Required |
---|---|---|---|
drive | A drive object already authenticated provided by googleapis. | object | YES |
parent | The id of the folder what you want to put your file. | string | YES |
filename | A function with req, file and callback params to modifies the strategy of the filename. Default: original filename | function | NO |
Yes! But, it's depend of your authentication type. When you use the OAuth authentication you can upload files to you root Google Drive (My Drive).
You can authenticate to google through OAuth or through Service Account. In the example in the 'Use' section, I authenticated by Service Account. When you authenticate with Service Account, you need to create the Shared Drive (Team Drive) and grant permission for the Service Account to access it.