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