docs.sheetjs.com/docz/docs/03-demos/30-cloud/11-aws.md

708 lines
20 KiB
Markdown
Raw Normal View History

2022-08-22 00:39:07 +00:00
---
title: Amazon Web Services
2023-02-28 11:40:44 +00:00
pagination_prev: demos/local/index
pagination_next: demos/extensions/index
2022-08-22 00:39:07 +00:00
---
2023-04-27 09:12:19 +00:00
import current from '/version.js';
2023-05-07 13:58:36 +00:00
import CodeBlock from '@theme/CodeBlock';
2023-04-27 09:12:19 +00:00
2023-11-30 07:10:37 +00:00
[Amazon Web Services](https://aws.amazon.com/) (AWS) is a cloud services
2023-10-02 04:57:11 +00:00
platform which includes traditional virtual machine support, "Serverless
Functions" and cloud storage.
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
data from spreadsheets.
This demo explores two key AWS offerings:
- ["Lambda Functions"](#lambda-functions) ("Lambda") explores the serverless
computing offering. The demo creates a JavaScript function that can process
user-submitted files and generate spreadsheets.
- ["S3 Storage"](#s3-storage) explores the cloud storage ("S3") offering. The
demo uses the NodeJS connection library to read spreadsheets from S3 and write
spreadsheets to a S3 bucket.
2022-08-22 00:39:07 +00:00
2023-09-24 03:59:48 +00:00
:::caution pass
2022-08-22 00:39:07 +00:00
AWS iterates quickly and there is no guarantee that the referenced services
will be available in the future.
:::
2023-11-30 07:10:37 +00:00
:::note Tested Deployments
2022-08-22 00:39:07 +00:00
2024-06-14 02:37:39 +00:00
This demo was last tested on 2024 June 13.
2022-08-22 00:39:07 +00:00
:::
2023-10-02 04:57:11 +00:00
## Lambda Functions
2023-11-30 07:10:37 +00:00
AWS offers NodeJS runtimes for running JavaScript serverless functions.[^1]
2023-10-02 04:57:11 +00:00
The [SheetJS NodeJS module](/docs/getting-started/installation/nodejs) can be
required in Lambda functions. When deploying, the entire `node_modules` folder
can be added to the ZIP package.
:::note pass
2022-08-22 00:39:07 +00:00
In this demo, the "Function URL" (automatic API Gateway management) features
are used. Older deployments required special "Binary Media Types" to handle
formats like XLSX. At the time of testing, the configuration was not required.
2023-10-02 04:57:11 +00:00
:::
:::info pass
Node.js runtime can use `x86_64` or `arm64` CPU architectures. SheetJS libraries
work on both platforms in Linux, Windows, and macOS operating systems.
:::
2022-08-22 00:39:07 +00:00
### Reading Data
2023-10-02 04:57:11 +00:00
In the Lambda handler, the `event.body` attribute is a Base64-encoded string
representing the HTTP request form data. This body must be parsed.
#### Processing Form Bodies
The `busboy` body parser[^2] is battle-tested in NodeJS deployments.
`busboy` fires a `'file'` event for every file in the form body. The callback
receives a NodeJS stream that should be collected into a Buffer:
```js
/* accumulate the files manually */
var files = {};
bb.on('file', function(fieldname, file, filename) {
/* concatenate the individual data buffers */
var buffers = [];
file.on('data', function(data) { buffers.push(data); });
file.on('end', function() { files[fieldname] = Buffer.concat(buffers); });
});
```
`busboy` fires a `'finish'` event when the body parsing is finished. Callbacks
can assume every file in the form body has been stored in NodeJS Buffer objects.
#### Processing NodeJS Buffers
The SheetJS `read` method[^3] can read the Buffer objects and generate SheetJS
workbook objects[^4] which can be processed with other API functions.
For example, a handler can use `sheet_to_csv`[^5] to generate CSV text:
```js
/* on the finish event, all of the fields and files are ready */
bb.on('finish', function() {
/* grab the first file */
var f = files["upload"];
if(!f) callback(new Error("Must submit a file for processing!"));
/* f[0] is a buffer */
// highlight-next-line
var wb = XLSX.read(f[0]);
/* grab first worksheet and convert to CSV */
var ws = wb.Sheets[wb.SheetNames[0]];
callback(null, { statusCode: 200, body: XLSX.utils.sheet_to_csv(ws) });
});
```
2022-08-22 00:39:07 +00:00
<details>
<summary><b>Complete Code Sample</b> (click to show)</summary>
2023-04-25 04:44:16 +00:00
This example takes the first uploaded file submitted with the key `upload`,
parses the file and returns the CSV content of the first worksheet.
2022-08-22 00:39:07 +00:00
```js
const XLSX = require('xlsx');
var Busboy = require('busboy');
exports.handler = function(event, context, callback) {
/* set up busboy */
var ctype = event.headers['Content-Type']||event.headers['content-type'];
var bb = Busboy({headers:{'content-type':ctype}});
/* busboy is evented; accumulate the fields and files manually */
var fields = {}, files = {};
bb.on('error', function(err) { callback(null, { body: err.message }); });
bb.on('field', function(fieldname, val) {fields[fieldname] = val });
// highlight-start
bb.on('file', function(fieldname, file, filename) {
/* concatenate the individual data buffers */
var buffers = [];
file.on('data', function(data) { buffers.push(data); });
file.on('end', function() { files[fieldname] = [Buffer.concat(buffers), filename]; });
});
// highlight-end
/* on the finish event, all of the fields and files are ready */
bb.on('finish', function() {
/* grab the first file */
var f = files["upload"];
if(!f) callback(new Error("Must submit a file for processing!"));
/* f[0] is a buffer */
// highlight-next-line
var wb = XLSX.read(f[0]);
/* grab first worksheet and convert to CSV */
var ws = wb.Sheets[wb.SheetNames[0]];
callback(null, { statusCode: 200, body: XLSX.utils.sheet_to_csv(ws) });
});
/* start the processing */
// highlight-next-line
bb.end(Buffer.from(event.body, "base64"));
};
```
</details>
### Writing Data
2023-10-02 04:57:11 +00:00
For safely transmitting binary data, Base64 strings should be used.
The SheetJS `write` method[^6] with the option `type: "base64"` will generate
Base64-encoded strings.
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
```js
/* sample SheetJS workbook object */
var wb = XLSX.read("S,h,e,e,t,J,S\n5,4,3,3,7,9,5", {type: "binary"});
/* write to XLSX file in Base64 encoding */
var b64 = XLSX.write(wb, { type: "base64", bookType: "xlsx" });
```
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
The Lambda callback response function accepts options. Setting `isBase64Encoded`
to `true` will ensure the callback handler decodes the data. To ensure browsers
will try to download the response, the `Content-Disposition` header must be set:
```js
callback(null, {
statusCode: 200,
/* Base64-encoded file */
isBase64Encoded: true,
body: b64,
headers: {
/* Browsers will treat the response as the file SheetJSLambda.xlsx */
"Content-Disposition": 'attachment; filename="SheetJSLambda.xlsx"'
}
});
```
<details>
<summary><b>Complete Code Sample</b> (click to show)</summary>
2023-10-02 04:57:11 +00:00
This example creates a sample workbook object and sends the file in the response:
2022-08-22 00:39:07 +00:00
```js
var XLSX = require('xlsx');
exports.handler = function(event, context, callback) {
/* make workbook */
var wb = XLSX.read("S,h,e,e,t,J,S\n5,4,3,3,7,9,5", {type: "binary"});
2022-08-25 08:22:28 +00:00
/* write to XLSX file in Base64 encoding */
2022-08-22 00:39:07 +00:00
// highlight-next-line
2023-04-25 04:44:16 +00:00
var body = XLSX.write(wb, { type: "base64", bookType: "xlsx" });
2022-08-22 00:39:07 +00:00
/* mark as attached file */
var headers = { "Content-Disposition": 'attachment; filename="SheetJSLambda.xlsx"'};
/* Send back data */
callback(null, {
statusCode: 200,
// highlight-next-line
isBase64Encoded: true,
body: body,
headers: headers
});
};
```
</details>
2023-10-02 04:57:11 +00:00
### Lambda Demo
:::note pass
At the time of writing, the AWS Free Tier included an allowance of 1 million
free requests per month and 400 thousand GB-seconds of compute resources.
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
:::
0) If you do not have an account, create a new AWS free tier account[^7].
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
#### Create Project ZIP
2022-08-22 00:39:07 +00:00
2024-06-14 02:37:39 +00:00
1) Create a new project folder:
2022-08-22 00:39:07 +00:00
```bash
2022-10-21 00:10:10 +00:00
mkdir -p SheetJSLambda
2022-08-22 00:39:07 +00:00
cd SheetJSLambda
2024-06-14 02:37:39 +00:00
```
2) Download [`index.js`](pathname:///aws/index.js):
```bash
2022-08-22 00:39:07 +00:00
curl -LO https://docs.sheetjs.com/aws/index.js
```
2024-06-14 02:37:39 +00:00
3) Install the SheetJS NodeJS module and `busboy` dependency:
2022-08-22 00:39:07 +00:00
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-10-21 00:10:10 +00:00
mkdir -p node_modules
2023-05-07 13:58:36 +00:00
npm i https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz busboy`}
</CodeBlock>
2022-08-22 00:39:07 +00:00
2024-06-14 02:37:39 +00:00
4) Create a .zip package of the contents of the folder:
2022-08-22 00:39:07 +00:00
```bash
2024-06-14 02:37:39 +00:00
zip -c ../SheetJSLambda.zip -r .
2022-08-22 00:39:07 +00:00
```
2023-10-02 04:57:11 +00:00
#### Lambda Setup
2024-06-14 02:37:39 +00:00
5) Sign into the [AWS Management Console](https://aws.amazon.com/console/) with
2023-10-02 04:57:11 +00:00
a root user account.
2024-06-14 02:37:39 +00:00
6) Type "Lambda" in the top search box and click Lambda (under Services).
2023-10-02 04:57:11 +00:00
2024-06-14 02:37:39 +00:00
![AWS search for "Lambda"](pathname:///aws/lambda.png)
7) Open "Functions" in the left sidebar.
2023-10-02 04:57:11 +00:00
If the left sidebar is not open, click the `≡` icon in the left edge of the page.
2024-06-14 02:37:39 +00:00
8) Click the "Create function" button in the main panel.
2023-10-02 04:57:11 +00:00
2024-06-14 02:37:39 +00:00
9) Select the following options:
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
- In the top list, select "Author from scratch" (default choice)
- Type a memorable "Function Name" ("SheetJSLambda" when last tested)
- In the "Runtime" dropdown, look for the "Latest supported" section and select
2024-06-14 02:37:39 +00:00
"Node.js" ("Node.js 20.x" when last tested)
2023-10-02 04:57:11 +00:00
- Expand "Advanced Settings" and check "Enable function URL". This will display
a few sub-options:
+ "Auth type" select "NONE" (disable IAM authentication)
2023-04-25 04:44:16 +00:00
+ Check "Configure cross-origin resource sharing (CORS)"
2022-08-22 00:39:07 +00:00
2024-06-14 02:37:39 +00:00
10) Click "Create function" to create the function.
2023-10-02 04:57:11 +00:00
#### Upload Code
2024-06-14 02:37:39 +00:00
11) In the Interface, scroll down and select the "Code" tab.
2023-10-02 04:57:11 +00:00
2024-06-14 02:37:39 +00:00
12) Click the "Upload from" dropdown and select ".zip file".
2023-10-02 04:57:11 +00:00
2024-06-14 02:37:39 +00:00
13) Click the "Upload" button in the modal. With the file picker, select the
2023-10-02 04:57:11 +00:00
`SheetJSLambda.zip` file created in step 3. Click "Save".
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
:::note pass
2023-04-25 04:44:16 +00:00
When the demo was last tested, the ZIP was small enough that the Lambda code
editor will load the package.
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
:::
2024-06-14 02:37:39 +00:00
14) In the code editor, double-click `index.js` and confirm the code editor
2023-10-02 04:57:11 +00:00
displays JavaScript code.
#### External Access
2024-06-14 02:37:39 +00:00
15) Click "Configuration" in the tab list.
2022-08-22 00:39:07 +00:00
2024-06-14 02:37:39 +00:00
16) In the sidebar below the tab list, select "Function URL" and click "Edit".
2023-10-02 04:57:11 +00:00
2024-06-14 02:37:39 +00:00
17) Set the "Auth type" to "NONE" and click Save. The page will redirect to the
2023-10-02 04:57:11 +00:00
Function properties.
2024-06-14 02:37:39 +00:00
18) Select the "Configuration" tab and select "Permissions" in the left sidebar.
2023-10-02 04:57:11 +00:00
2024-06-14 02:37:39 +00:00
19) Scroll down to "Resource-based policy statements" and ensure that
2023-10-02 04:57:11 +00:00
`FunctionURLAllowPublicAccess` is listed.
2022-08-22 00:39:07 +00:00
If no policy statements are defined, select "Add Permission" with the options:
- Select "Function URL" at the top
- Auth type: NONE
- Ensure that Statement ID is set to `FunctionURLAllowPublicAccess`
- Ensure that Principal is set to `*`
- Ensure that Action is set to `lambda:InvokeFunctionUrl`
Click "Save" and a new Policy statement should be created.
2023-10-02 04:57:11 +00:00
#### Lambda Testing
19) Find the Function URL (It is in the "Function Overview" section).
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
20) Try to access the function URL in a web browser.
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
The site will attempt to download `SheetJSLambda.xlsx`. Save and open the file
to confirm it is valid.
2024-04-26 04:16:13 +00:00
21) Download https://docs.sheetjs.com/pres.numbers and make a POST request to
the public function URL.
2023-10-02 04:57:11 +00:00
This can be tested on the command line. Change `FUNCTION_URL` in the commands:
2022-08-22 00:39:07 +00:00
```bash
2024-04-26 04:16:13 +00:00
curl -LO https://docs.sheetjs.com/pres.numbers
2022-08-22 00:39:07 +00:00
curl -X POST -F "upload=@pres.numbers" FUNCTION_URL
```
2023-10-02 04:57:11 +00:00
The terminal will display CSV output of the first sheet.
2022-08-22 00:39:07 +00:00
## S3 Storage
2023-10-02 04:57:11 +00:00
The main NodeJS module for S3 and all AWS services is `aws-sdk`[^8].
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
The [SheetJS NodeJS module](/docs/getting-started/installation/nodejs) can be
required in NodeJS scripts.
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
### Connecting to S3
2023-04-25 04:44:16 +00:00
2023-10-16 09:12:56 +00:00
The `aws-sdk` module exports a function `S3` that performs the connection. The
function expects an options object that includes an API version and credentials.
2023-10-03 22:58:43 +00:00
Access keys for an IAM user[^9] must be used:
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
```js
/* credentials */
var accessKeyId = "...", secretAccessKey = "..."";
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
/* file location */
var Bucket = "...", Key = "pres.numbers";
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
/* connect to s3 account */
2022-08-22 00:39:07 +00:00
var AWS = require('aws-sdk');
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
2023-10-02 04:57:11 +00:00
credentials: { accessKeyId, secretAccessKey }
2022-08-22 00:39:07 +00:00
});
2023-10-02 04:57:11 +00:00
```
2023-10-06 20:47:17 +00:00
### Downloading Data
2023-10-02 04:57:11 +00:00
#### Fetching Files from S3
The `s3#getObject` method returns an object with a `createReadStream` method.
`createReadStream` returns a NodeJS stream:
```js
/* open stream to the file */
var stream = s3.getObject({ Bucket: Bucket, Key: Key }).createReadStream();
```
#### Concatenating NodeJS Streams
Buffers can be concatenated from the stream into one unified Buffer object:
```js
/* array of buffers */
2022-08-22 00:39:07 +00:00
var bufs = [];
2023-10-02 04:57:11 +00:00
/* add each data chunk to the array */
stream.on('data', function(data) { bufs.push(data); });
/* the callback will be called after all of the data is collected */
stream.on('end', function() {
/* concatenate */
var buf = Buffer.concat(bufs);
/* AT THIS POINT, `buf` is a NodeJS Buffer */
});
```
#### Parsing NodeJS Buffers
The SheetJS `read` method[^10] can read the final object and generate SheetJS
workbook objects[^11] which can be processed with other API functions.
For example, a callback can use `sheet_to_csv`[^12] to generate CSV text:
```js
stream.on('end', function() {
/* concatenate */
var buf = Buffer.concat(bufs);
/* parse */
2022-08-22 00:39:07 +00:00
var wb = XLSX.read(Buffer.concat(bufs));
2023-10-02 04:57:11 +00:00
/* generate CSV from first worksheet */
var first_ws = wb.Sheets[wb.SheetNames[0]];
var csv = XLSX.utils.sheet_to_csv(first_ws);
console.log(csv);
});
```
2023-10-06 20:47:17 +00:00
### Uploading Data
2023-10-02 04:57:11 +00:00
The SheetJS `write` method[^13] with the option `type: "buffer"` will generate
NodeJS Buffers. `S3#upload` directly accepts these Buffer objects.
This example creates a sample workbook object, generates XLSX file data in a
NodeJS Buffer, and uploads the data to S3:
```js
/* generate sample workbook */
var wb = XLSX.read("S,h,e,e,t,J,S\n5,4,3,3,7,9,5", {type: "binary"});
/* write to XLSX file in a NodeJS Buffer */
var Body = XLSX.write(wb, {type: "buffer", bookType: "xlsx"});
/* upload buffer */
s3.upload({ Bucket, Key, Body }, function(err, data) {
if(err) throw err;
console.log("Uploaded to " + data.Location);
2022-08-22 00:39:07 +00:00
});
```
2023-10-02 04:57:11 +00:00
### S3 Demo
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
:::note pass
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
At the time of writing, the AWS Free Tier included 5GB of S3 storage with 20,000
Get requests and 2000 Put requests per month.
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
:::
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
This sample fetches a buffer from S3 and parses the workbook.
0) If you do not have an account, create a new AWS free tier account[^14].
#### Create S3 Bucket
1) Sign into the [AWS Management Console](https://aws.amazon.com/console/) with
a root user account.
2) Type "S3" in the top search box and click S3 (under Services).
2024-06-14 02:37:39 +00:00
![AWS search for "S3"](pathname:///aws/S3.png)
2023-10-02 04:57:11 +00:00
3) Open "Buckets" in the left sidebar.
If the left sidebar is not open, click the `≡` icon in the left edge of the page.
4) Click the "Create bucket" button in the main panel.
5) Select the following options:
- Type a memorable "Bucket Name" ("sheetjsbouquet" when last tested)
- In the "Object Ownership" section, select "ACLs disabled"
- Check "Block *all* public access"
- Look for the "Bucket Versioning" section and select "Disable"
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
6) Click "Create bucket" to create the bucket.
#### Create IAM User
7) Type "IAM" in the top search box and click IAM (under Services).
2024-06-14 02:37:39 +00:00
![AWS search for "iam"](pathname:///aws/iam.png)
2023-10-02 04:57:11 +00:00
8) Open "Users" in the left sidebar.
If the left sidebar is not open, click the `≡` icon in the left edge of the page.
9) Click the "Create user" button in the main panel.
10) In step 1, type a memorable "Bucket Name" ("sheetjs-user" when last tested).
Click "Next".
11) In step 2, click "Next"
12) In step 3, click "Create user" to create the user.
#### Add Permissions
13) Click the new user name in the Users table.
14) Select the "Permissions" tab
15) Click the "Add permissions" dropdown and select "Add permissions".
16) Select "Attach policies directly".
17) In the "Permissions policies" section, search for "AmazonS3FullAccess".
There should be one entry.
2024-06-14 02:37:39 +00:00
![AWS search for "AmazonS3FullAccess"](pathname:///aws/perm.png)
2023-10-02 04:57:11 +00:00
18) Check the checkbox next to "AmazonS3FullAccess" and click the "Next" button.
19) In the "Review" screen, click "Add permissions"
#### Generate Keys
20) Click "Security credentials", then click "Create access key".
21) Select the "Local code" option. Check "I understand the above recommendation
and want to proceed to create an access key." and click "Next"
22) Click "Create Access Key" and click "Download .csv file" in the next screen.
In the generated CSV:
- Cell A2 is the "Access key ID" (`accessKeyId` in the AWS API)
- Cell B2 is the "Secret access key" (`secretAccessKey` in the AWS API)
#### Set up Project
23) Create a new NodeJS project:
2023-04-25 04:44:16 +00:00
```bash
2023-10-02 04:57:11 +00:00
mkdir SheetJSS3
cd SheetJSS3
npm init -y
2023-04-25 04:44:16 +00:00
```
2023-10-02 04:57:11 +00:00
24) Install dependencies:
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
<CodeBlock language="bash">{`\
mkdir -p node_modules
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz aws-sdk@2.1467.0`}
</CodeBlock>
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
#### Write Test
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
:::note pass
2023-04-25 04:44:16 +00:00
This sample creates a simple workbook, generates a NodeJS buffer, and uploads
the buffer to S3.
2022-08-22 00:39:07 +00:00
2023-10-02 04:57:11 +00:00
```
| A | B | C | D | E | F | G |
---+---|---|---|---|---|---|---|
1 | S | h | e | e | t | J | S |
2 | 5 | 4 | 3 | 3 | 7 | 9 | 5 |
```
:::
25) Save the following script to `SheetJSWriteToS3.js`:
2022-08-22 00:39:07 +00:00
```js title="SheetJSWriteToS3.js"
var XLSX = require("xlsx");
var AWS = require('aws-sdk');
/* replace these constants */
2023-10-02 04:57:11 +00:00
// highlight-start
2022-08-22 00:39:07 +00:00
var accessKeyId = "<REPLACE WITH ACCESS KEY ID>";
var secretAccessKey = "<REPLACE WITH SECRET ACCESS KEY>";
var Bucket = "<REPLACE WITH BUCKET NAME>";
2023-10-02 04:57:11 +00:00
// highlight-end
2023-04-25 04:44:16 +00:00
var Key = "test.xlsx";
2022-08-22 00:39:07 +00:00
/* Create a simple workbook and write XLSX to buffer */
var ws = XLSX.utils.aoa_to_sheet(["SheetJS".split(""), [5,4,3,3,7,9,5]]);
var wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
var Body = XLSX.write(wb, {type: "buffer", bookType: "xlsx"});
/* upload buffer */
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
credentials: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
}
});
s3.upload({ Bucket: Bucket, Key: Key, Body: Body }, function(err, data) {
if(err) throw err;
console.log("Uploaded to " + data.Location);
});
```
2023-10-02 04:57:11 +00:00
26) Edit `SheetJSWriteToS3.js` and replace the highlighted lines:
2023-04-25 04:44:16 +00:00
- `accessKeyId`: access key for the AWS account
- `secretAccessKey`: secret access key for the AWS account
- `Bucket`: name of the bucket
2023-10-02 04:57:11 +00:00
The keys are found in the CSV from step 22. The Bucket is the name from step 5.
27) Run the script:
2023-04-25 04:44:16 +00:00
```bash
node SheetJSWriteToS3.js
```
2023-10-02 04:57:11 +00:00
This file will be stored with the object name `test.xlsx`. It can be manually
downloaded from the S3 web interface.
#### Read Test
This sample will download and process the test file from "Write Test".
28) Save the following script to `SheetJSReadFromS3.js`:
```js title="SheetJSReadFromS3.js"
var XLSX = require("xlsx");
var AWS = require('aws-sdk');
/* replace these constants */
// highlight-start
var accessKeyId = "<REPLACE WITH ACCESS KEY ID>";
var secretAccessKey = "<REPLACE WITH SECRET ACCESS KEY>";
var Bucket = "<REPLACE WITH BUCKET NAME>";
// highlight-end
var Key = "test.xlsx";
/* Get stream */
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
credentials: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
}
});
var f = s3.getObject({ Bucket: Bucket, Key: Key }).createReadStream();
/* collect data */
var bufs = [];
f.on('data', function(data) { bufs.push(data); });
f.on('end', function() {
/* concatenate and parse */
var wb = XLSX.read(Buffer.concat(bufs));
console.log(XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]));
});
```
29) Edit `SheetJSReadFromS3.js` and replace the highlighted lines:
- `accessKeyId`: access key for the AWS account
- `secretAccessKey`: secret access key for the AWS account
- `Bucket`: name of the bucket
The keys are found in the CSV from Step 22. The Bucket is the name from Step 5.
30) Run the script:
```bash
node SheetJSReadFromS3.js
```
The program will display the data in CSV format.
```
S,h,e,e,t,J,S
5,4,3,3,7,9,5
```
2023-04-25 04:44:16 +00:00
2023-10-02 04:57:11 +00:00
[^1]: See ["Building Lambda functions with Node.js"](https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html) in the AWS documentation
[^2]: The `busboy` module is distributed [on the public NPM registry](https://npm.im/busboy)
[^3]: See [`read` in "Reading Files"](/docs/api/parse-options)
[^4]: See ["Workbook Object" in "SheetJS Data Model"](/docs/csf/book) for more details.
[^5]: See [`sheet_to_csv` in "CSV and Text"](/docs/api/utilities/csv#delimiter-separated-output)
[^6]: See [`write` in "Writing Files"](/docs/api/write-options)
[^7]: Registering for a free account [on the AWS Free Tier](https://aws.amazon.com/free/) requires a valid phone number and a valid credit card.
[^8]: The `aws-sdk` module is distributed [on the public NPM registry](https://npm.im/aws-sdk)
[^9]: See ["Managing access keys for IAM users"](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html) in the AWS documentation
[^10]: See [`read` in "Reading Files"](/docs/api/parse-options)
[^11]: See ["Workbook Object" in "SheetJS Data Model"](/docs/csf/book) for more details.
[^12]: See [`sheet_to_csv` in "CSV and Text"](/docs/api/utilities/csv#delimiter-separated-output)
[^13]: See [`write` in "Writing Files"](/docs/api/write-options)
[^14]: Registering for a free account [on the AWS Free Tier](https://aws.amazon.com/free/) requires a valid phone number and a valid credit card.