Exceljs: Can I save file to AWS S3

Created on 16 Sep 2016  ·  3Comments  ·  Source: exceljs/exceljs

Basically is there a way to keep created file in variable so I can send it to API's such as google drive, etc. Similar to when I browse and select a file in browser, I can then keep the file in a variable, can something similar be done?

Most helpful comment

Hey,
I think the case here was trying to avoid storing the file to disk and just directly streaming the created Exceljs workbook to some cloud storage API. For what it's worth, here's how you do it in Node with s3:

import * as exceljs from 'exceljs';
import * as aws from 'aws-sdk';
import * as Stream from 'stream';

const s3 = new aws.S3(/* put your s3 configuration here */);
const stream = new Stream.PassThrough();
const workbook = new exceljs.Workbook();
// Add images of cats to workbook

workbook.xlsx.write(stream)
    .then(() => {
        return s3.upload({
            Key: PATH_IN_S3,
            Bucket: BUCKET_NAME,
            Body: stream,
            ContentType: CONTENT_TYPE_EXCEL
        }).promise();
    })
    .then(/* do whatever */)
    .catch(/* handle error */);

All 3 comments

Hey @piyushmahensaria,

I'm making the assumption you are using node.js.

Here is some logic that should help you achieve what you're looking for:

  • Create the excel file using workbook.xlsx.writeFile(filename);
  • Keep track of filename - we'll want to read the file contents somehow.
  • fs.readFile(filename, function(err, data) { ... }); will get you your file contents.
  • Since we have a filename and the file contents, we can make the proper API call to whatever cloud hosting service you want to use.

If you have more questions please ask.

Hey,
I think the case here was trying to avoid storing the file to disk and just directly streaming the created Exceljs workbook to some cloud storage API. For what it's worth, here's how you do it in Node with s3:

import * as exceljs from 'exceljs';
import * as aws from 'aws-sdk';
import * as Stream from 'stream';

const s3 = new aws.S3(/* put your s3 configuration here */);
const stream = new Stream.PassThrough();
const workbook = new exceljs.Workbook();
// Add images of cats to workbook

workbook.xlsx.write(stream)
    .then(() => {
        return s3.upload({
            Key: PATH_IN_S3,
            Bucket: BUCKET_NAME,
            Body: stream,
            ContentType: CONTENT_TYPE_EXCEL
        }).promise();
    })
    .then(/* do whatever */)
    .catch(/* handle error */);

@emraa your comment was super helpful to me. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PRR24 picture PRR24  ·  3Comments

TranBaVinhSon picture TranBaVinhSon  ·  4Comments

thearabbit picture thearabbit  ·  3Comments

CountryGuide picture CountryGuide  ·  3Comments

dbsxdbsx picture dbsxdbsx  ·  3Comments