googleapis / googleapis/google-api-nodejs-client
My pdfs are coming out blank after using Drive Api, drive.files.get(fileId)...
- Lingua principale
- TypeScript
- Stelle
- 12.3k
- Fork
- 2k
- Merge medio
- 1g 9h
- PR unite (30g)
- 24
Descrizione
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;
`
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Traccia il percorso del download attraverso GoogleConn.js, App.js e AboutMeSection.js, iniziando con una riproduzione locale e con il file e la risposta generati. Conferma dove viene perso il contenuto PDF prima di raggiungere FileSaver; il lavoro è completo quando viene identificato il confine che fallisce e viene documentata una modifica concreta all'applicazione o alla libreria.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- aws, javascript, node.js, react
- Ambito
- api, backend, frontend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100