googleapis / googleapis/google-api-nodejs-client
My pdfs are coming out blank after using Drive Api, drive.files.get(fileId)...
- Dominant language
- TypeScript
- Stars
- 12.2k
- Forks
- 2k
- Avg merge
- 1d 9h
- Merged PRs (30d)
- 24
Description
I am building a personal website where I am trying to create the ability for a user to download my resume through the (React) client side of my web app from an AWS Lambda function. I have tried using FileSaver and window.open(req.url) to prompt the user to download my resume which successfully prompts the user. However, when I open up the file, I can see the pages but they are blank.
I have been only testing this locally and fair warning I am still learning AWS Lambda so, any pointers would help me too.
GoogleConn.js (handler function):
`
const {google} = require('googleapis');
const fs = require('fs');
const CLIENT_ID = 'xxxx';
const CLIENT_SECRET=xxx;
const REDIRECT_URI ='https://developers.google.com/oauthplayground';
const REFRESH_TOKEN ='xxx';
const fileID = 'xxx';
const filePath = fs.createWriteStream("/tmp/resume.pdf");
const oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
module.exports.driveDownload = async function (event, context) {
await oauth2Client.setCredentials({refresh_token: REFRESH_TOKEN});
const drive = await google.drive({version: 'v3', auth: oauth2Client});
console.log('successfully passed google drive initialization!');
await drive.files.get({ fileId: fileID, alt: 'media',}, {responseType: 'stream'}).then(res => {
res.data.on('end', () => {console.log('Done downloading')})
.on('error', (err) => {console.log('Error on downloading: ' + err)})
.on('data', d =>{
console.log(`Data length being downloaded... ${d.length}`);
})
.pipe(filePath);
});
return filePath.path;
}
`
App.js (lambda function)
`
const express = require('express')
const bodyParser = require('body-parser')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
// declare a new express app
const app = express();
const googleConn = require('./googleconn');
app.use(bodyParser.json())
app.use(awsServerlessExpressMiddleware.eventContext())
// Enable CORS for all methods
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "*")
next()
});
app.get('/downloadresume', function(req, res) {
googleConn.driveDownload(this, this)
.then(r => {res.sendFile(r, {}, err => {
console.log(err);
})})
.catch(err =>{res.json({error: 'it did NOT work :/', body: err.toString()})});
});
app.listen(5000, function() {
console.log("App started")
});
// Export the app object. When executing the application local this does nothing. However,
// to port it to AWS Lambda we will create a wrapper around that will load the app from
// this file
module.exports = app
`
AboutMeSection.js (client-side)
`
function AboutMeSection(){
async function download_resume() {
await API.get('DownloadResumeAPI','/downloadresume', {
responseType: 'blob'
})
.then(res =>{
FileSaver.saveAs(res, 'resume.pdf');
})
.catch(err => {console.error(err)});
}
return(
}
export default AboutMeSection;
`
Contributor guide
Assessment
This issue has not been assessed yet.