1.1.0 • Published 4 years ago

multer-google-drive-teams-storage v1.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

Google Drive Teams Storage

A simple storage to upload files into Google Drive Teams Folders by Multer.

What's 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).

Translations

This README is also available in others languages.

Installation

npm install --save multer-google-drive-teams-storage

Usage

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.');
});

API

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.

KeyDecriptionTypeRequired
driveA drive object already authenticated provided by googleapis.objectYES
parentThe id of the folder what you want to put your file.stringYES
filenameA function with req, file and callback params to modifies the strategy of the filename. Default: original filenamefunctionNO

F.A.Q.

Can I use this storage to upload file outside a Team Drive?

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).

What kind of authentication can I use?

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.

Licence

MIT