Google Drive Backup upload #3
@ -3,7 +3,7 @@ const readline = require('readline');
|
||||
const {google} = require('googleapis');
|
||||
|
||||
// If modifying these scopes, delete token.json.
|
||||
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
|
||||
const SCOPES = ['https://www.googleapis.com/auth/drive'];
|
||||
// The file token.json stores the user's access and refresh tokens, and is
|
||||
// created automatically when the authorization flow completes for the first
|
||||
// time.
|
||||
@ -72,19 +72,23 @@ function getAccessToken(oAuth2Client, callback) {
|
||||
*/
|
||||
function listFiles(auth) {
|
||||
const drive = google.drive({version: 'v3', auth});
|
||||
drive.files.list({
|
||||
pageSize: 10,
|
||||
fields: 'nextPageToken, files(id, name)',
|
||||
}, (err, res) => {
|
||||
if (err) return console.log('The API returned an error: ' + err);
|
||||
const files = res.data.files;
|
||||
if (files.length) {
|
||||
console.log('Files:');
|
||||
files.map((file) => {
|
||||
console.log(`${file.name} (${file.id})`);
|
||||
});
|
||||
var fileMetadata = {
|
||||
'name': 'teste.zip'
|
||||
};
|
||||
var media = {
|
||||
mimeType: 'application/octet-stream',
|
||||
body: fs.createReadStream('teste.zip')
|
||||
};
|
||||
drive.files.create({
|
||||
resource: fileMetadata,
|
||||
media: media,
|
||||
fields: 'id'
|
||||
}, function (err, file) {
|
||||
if (err) {
|
||||
// Handle error
|
||||
console.error(err);
|
||||
} else {
|
||||
console.log('No files found.');
|
||||
console.log('File: ', file.data.id);
|
||||
}
|
||||
});
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
var {google} = require('googleapis');
|
||||
const { GoogleAuth } = require('google-auth-library');
|
||||
const stream = require('stream');
|
||||
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');
|
||||
// });
|
Binary file not shown.
Reference in New Issue
Block a user