Google Drive Backup upload #3
@ -1,4 +1,3 @@
|
|||||||
//index.js
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const readline = require('readline');
|
const readline = require('readline');
|
||||||
const {google} = require('googleapis');
|
const {google} = require('googleapis');
|
||||||
@ -10,17 +9,12 @@ const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
|
|||||||
// time.
|
// time.
|
||||||
const TOKEN_PATH = 'token.json';
|
const TOKEN_PATH = 'token.json';
|
||||||
|
|
||||||
function callGDriveApi(callback){
|
|
||||||
// Load client secrets from a local file.
|
// Load client secrets from a local file.
|
||||||
fs.readFile('credentials.json', (err, content) => {
|
fs.readFile('credentials.json', (err, content) => {
|
||||||
if (err) return console.log('Error loading client secret file:', err);
|
if (err) return console.log('Error loading client secret file:', err);
|
||||||
// Authorize a client with credentials, then call the Google Drive API.
|
// Authorize a client with credentials, then call the Google Drive API.
|
||||||
if(callback)
|
authorize(JSON.parse(content), listFiles);
|
||||||
authorize(JSON.parse(content), callback);
|
|
||||||
else
|
|
||||||
authorize(JSON.parse(content), listFiles);//default
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an OAuth2 client with the given credentials, and then execute the
|
* Create an OAuth2 client with the given credentials, and then execute the
|
||||||
@ -94,7 +88,3 @@ function listFiles(auth) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
callGDriveApi();
|
|
||||||
|
|
||||||
module.exports = callGDriveApi;
|
|
@ -1,31 +0,0 @@
|
|||||||
const fs = require("fs");
|
|
||||||
const {google} = require('googleapis');
|
|
||||||
|
|
||||||
function imageUpload(fileName, filePath, callback){
|
|
||||||
require("./auth")((auth) => {
|
|
||||||
const fileMetadata = {
|
|
||||||
name: fileName
|
|
||||||
};
|
|
||||||
|
|
||||||
const media = {
|
|
||||||
mimeType: "application/zip",
|
|
||||||
body: fs.createReadStream(filePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
const drive = google.drive({version: 'v3', auth});
|
|
||||||
drive.files.create({
|
|
||||||
resource: fileMetadata,
|
|
||||||
media: media,
|
|
||||||
fields: 'id'
|
|
||||||
}, function (err, file) {
|
|
||||||
if (err) {
|
|
||||||
// Handle error
|
|
||||||
console.error(err);
|
|
||||||
} else {
|
|
||||||
callback(file.data.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { imageUpload };
|
|
@ -1,5 +1,35 @@
|
|||||||
//index.js
|
var {google} = require('googleapis');
|
||||||
const gdrive = require("./drive");
|
const { GoogleAuth } = require('google-auth-library');
|
||||||
gdrive.imageUpload("teste.zip", "./teste.zip", (id) => {
|
const stream = require('stream');
|
||||||
console.log(id);
|
const serviceAccount = require('../config/node-uploader-99-9e3f13fd0cde.json')
|
||||||
});
|
console.log(serviceAccount);
|
||||||
|
let fileObject = req.body.filePDF;
|
||||||
|
console.log(fileObject);
|
||||||
|
let bufferStream = new stream.PassThrough();
|
||||||
|
bufferStream.end(fileObject.buffer);
|
||||||
|
const jWTClient = new google.auth.JWT(
|
||||||
|
serviceAccount.client_email,
|
||||||
|
null,
|
||||||
|
serviceAccount.private_key,
|
||||||
|
['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file']
|
||||||
|
)
|
||||||
|
google.drive({ version: 'v3'})
|
||||||
|
.files.create({
|
||||||
|
auth: GoogleAuth/jWTClient,
|
||||||
|
media: {
|
||||||
|
mimeType: 'application/pdf',
|
||||||
|
body: bufferStream
|
||||||
|
},
|
||||||
|
resource: {
|
||||||
|
name: 'DeniTheFile.pdf',
|
||||||
|
// if you want to store the file in the root, remove this parents
|
||||||
|
parents: ['1KwLSHyu9R1jo3-ahtWgJCohoCsrtrE-I']
|
||||||
|
},
|
||||||
|
fields: 'id',
|
||||||
|
}).then(function (resp) {
|
||||||
|
console.log(resp,'resp');
|
||||||
|
}).catch(function (error) {
|
||||||
|
console.log(error);
|
||||||
|
})
|
||||||
|
res.send('File uploaded');
|
||||||
|
// });
|
Reference in New Issue
Block a user