forked from sheetjs/docs.sheetjs.com
		
	aws
This commit is contained in:
		
							parent
							
								
									31b4925ad5
								
							
						
					
					
						commit
						2462218120
					
				| @ -17,9 +17,12 @@ will be available in the future. | ||||
| This demo focuses on two key offerings: cloud storage ("S3") and the | ||||
| "Serverless Function" platform ("Lambda"). | ||||
| 
 | ||||
| The [NodeJS Module](/docs/getting-started/installation/nodejs) can be shipped in | ||||
| a bundled Lambda function. | ||||
| 
 | ||||
| :::note | ||||
| 
 | ||||
| This was tested on 2022 August 21. | ||||
| This was tested on 2023 April 24. | ||||
| 
 | ||||
| ::: | ||||
| 
 | ||||
| @ -34,7 +37,10 @@ formats like XLSX.  At the time of testing, the configuration was not required. | ||||
| In the Lambda handler method, the `event.body` attribute is a Base64-encoded | ||||
| string.  The `busboy` body parser can accept a decoded body. | ||||
| 
 | ||||
| <details><summary><b>Code Sample</b> (click to show)</summary> | ||||
| <details open><summary><b>Code Sample</b> (click to hide)</summary> | ||||
| 
 | ||||
| This example takes the first uploaded file submitted with the key `upload`, | ||||
| parses the file and returns the CSV content of the first worksheet. | ||||
| 
 | ||||
| ```js | ||||
| const XLSX = require('xlsx'); | ||||
| @ -84,9 +90,11 @@ exports.handler = function(event, context, callback) { | ||||
| ### Writing Data | ||||
| 
 | ||||
| For safely transmitting binary data, the `base64` type should be used.  Lambda | ||||
| callback response `isBase64Encoded` property forces a binary download: | ||||
| callback response `isBase64Encoded` property forces a binary download. | ||||
| 
 | ||||
| <details><summary><b>Code Sample</b> (click to show)</summary> | ||||
| <details open><summary><b>Code Sample</b> (click to hide)</summary> | ||||
| 
 | ||||
| This example generates a sample workbook and writes to a XLSX workbook. | ||||
| 
 | ||||
| ```js | ||||
| var XLSX = require('xlsx'); | ||||
| @ -96,7 +104,7 @@ exports.handler = function(event, context, callback) { | ||||
|   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 */ | ||||
|   // highlight-next-line | ||||
|   var body = XLSX.write(wb, {type:"base64", bookType: "xlsx"}); | ||||
|   var body = XLSX.write(wb, { type: "base64", bookType: "xlsx" }); | ||||
|   /* mark as attached file */ | ||||
|   var headers = { "Content-Disposition": 'attachment; filename="SheetJSLambda.xlsx"'}; | ||||
|   /* Send back data */ | ||||
| @ -114,7 +122,7 @@ exports.handler = function(event, context, callback) { | ||||
| 
 | ||||
| ### Demo | ||||
| 
 | ||||
| <details><summary><b>Complete Example</b> (click to show)</summary> | ||||
| <details open><summary><b>Complete Example</b> (click to hide)</summary> | ||||
| 
 | ||||
| 0) Review the quick start for JavaScript on AWS | ||||
| 
 | ||||
| @ -145,20 +153,20 @@ yes | zip -c ../SheetJSLambda.zip -r . | ||||
| - "Function Name": SheetJSLambda | ||||
| - "Runtime": "Node.js" (select the version in the "Latest supported" block) | ||||
| - Advanced Settings: | ||||
|  + check "Enable function URL" | ||||
|  + Auth type: NONE | ||||
|  + Check "Configure CORS" | ||||
|   + check "Enable function URL" | ||||
|   + Auth type: NONE | ||||
|   + Check "Configure cross-origin resource sharing (CORS)" | ||||
| 
 | ||||
| 5) In the Interface, click "Upload from" and select ".zip file".  Click the | ||||
| "Upload" button in the modal, select SheetJSLambda.zip, and click "Save". | ||||
| 
 | ||||
| At the time of writing, the ZIP is small enough that the Lambda code editor | ||||
| will load the package. | ||||
| When the demo was last tested, the ZIP was small enough that the Lambda code | ||||
| editor will load the package. | ||||
| 
 | ||||
| 6) Enable external access to the function. | ||||
| 
 | ||||
| Under Configuration > Function URL, click "Edit" and ensure that Auth type is | ||||
| set to NONE.  If it is not, select NONE and hit Save. | ||||
| set to NONE.  If it is not, select NONE and click Save. | ||||
| 
 | ||||
| Under Configuration > Permissions, scroll down to "Resource-based policy". | ||||
| If no policy statements are defined, select "Add Permission" with the options: | ||||
| @ -176,7 +184,8 @@ Click "Save" and a new Policy statement should be created. | ||||
| Try to access that URL in a web browser and the site will try to download | ||||
| `SheetJSLambda.xlsx`.  Save and open the file to confirm it is valid. | ||||
| 
 | ||||
| To test parsing, download <https://sheetjs.com/pres.numbers> and run | ||||
| To test parsing, download <https://sheetjs.com/pres.numbers> and make a POST | ||||
| request to the public function URL (change `FUNCTION_URL` in the command): | ||||
| 
 | ||||
| ```bash | ||||
| curl -X POST -F "upload=@pres.numbers" FUNCTION_URL | ||||
| @ -193,11 +202,15 @@ The main module for S3 and all AWS services is `aws-sdk`. | ||||
| ### Reading Data | ||||
| 
 | ||||
| The `s3#getObject` method returns an object with a `createReadStream` method. | ||||
| Buffers can be concatenated and passed to `XLSX.read`: | ||||
| Buffers can be concatenated and passed to `XLSX.read`. | ||||
| 
 | ||||
| <details><summary><b>Code Sample</b> (click to show)</summary> | ||||
| <details open><summary><b>Demo</b> (click to hide)</summary> | ||||
| 
 | ||||
| ```js title="SheetJSReadFromS3.mjs" | ||||
| This sample fetches a buffer from S3 and parses the workbook. | ||||
| 
 | ||||
| 1) Save the following script to `SheetJSReadFromS3.js`: | ||||
| 
 | ||||
| ```js title="SheetJSReadFromS3.js" | ||||
| var XLSX = require("xlsx"); | ||||
| var AWS = require('aws-sdk'); | ||||
| 
 | ||||
| @ -205,7 +218,8 @@ var AWS = require('aws-sdk'); | ||||
| var accessKeyId = "<REPLACE WITH ACCESS KEY ID>"; | ||||
| var secretAccessKey = "<REPLACE WITH SECRET ACCESS KEY>"; | ||||
| var Bucket = "<REPLACE WITH BUCKET NAME>"; | ||||
| var Key = "<REPLACE WITH KEY>"; | ||||
| 
 | ||||
| var Key = "pres.numbers"; | ||||
| 
 | ||||
| /* Get stream */ | ||||
| var s3 = new AWS.S3({ | ||||
| @ -227,13 +241,39 @@ f.on('end', function() { | ||||
| }); | ||||
| ``` | ||||
| 
 | ||||
| 2) Create a new bucket (or get the name of an existing bucket). | ||||
| 
 | ||||
| 3) Download <https://sheetjs.com/pres.numbers> | ||||
| 
 | ||||
| In the S3 site, open the bucket and click "Upload". In the Upload page, click | ||||
| and drag the `pres.numbers` file into the browser window and click "Upload". | ||||
| 
 | ||||
| 4) Edit `SheetJSReadFromS3.js` and replace the marked constants: | ||||
| 
 | ||||
| - `accessKeyId`: access key for the AWS account | ||||
| - `secretAccessKey`: secret access key for the AWS account | ||||
| - `Bucket`: name of the bucket | ||||
| 
 | ||||
| 5) Run the script: | ||||
| 
 | ||||
| ```bash | ||||
| node SheetJSReadFromS3.js | ||||
| ``` | ||||
| 
 | ||||
| The program will display the data in CSV format. | ||||
| 
 | ||||
| </details> | ||||
| 
 | ||||
| ### Writing Data | ||||
| 
 | ||||
| `S3#upload` directly accepts a Buffer: | ||||
| `S3#upload` directly accepts a Buffer. | ||||
| 
 | ||||
| <details><summary><b>Code Sample</b> (click to show)</summary> | ||||
| <details open><summary><b>Demo</b> (click to hide)</summary> | ||||
| 
 | ||||
| This sample creates a simple workbook, generates a NodeJS buffer, and uploads | ||||
| the buffer to S3. | ||||
| 
 | ||||
| 1) Save the following script to `SheetJSWriteToS3.js`: | ||||
| 
 | ||||
| ```js title="SheetJSWriteToS3.js" | ||||
| var XLSX = require("xlsx"); | ||||
| @ -243,7 +283,8 @@ var AWS = require('aws-sdk'); | ||||
| var accessKeyId = "<REPLACE WITH ACCESS KEY ID>"; | ||||
| var secretAccessKey = "<REPLACE WITH SECRET ACCESS KEY>"; | ||||
| var Bucket = "<REPLACE WITH BUCKET NAME>"; | ||||
| var Key = "<REPLACE WITH KEY>"; | ||||
| 
 | ||||
| var Key = "test.xlsx"; | ||||
| 
 | ||||
| /* 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]]); | ||||
| @ -264,4 +305,21 @@ s3.upload({ Bucket: Bucket, Key: Key, Body: Body }, function(err, data) { | ||||
| }); | ||||
| ``` | ||||
| 
 | ||||
| 2) Create a new bucket (or get the name of an existing bucket). | ||||
| 
 | ||||
| 3) Edit `SheetJSWriteToS3.js` and replace the marked constants: | ||||
| 
 | ||||
| - `accessKeyId`: access key for the AWS account | ||||
| - `secretAccessKey`: secret access key for the AWS account | ||||
| - `Bucket`: name of the bucket | ||||
| 
 | ||||
| 4) Run the script: | ||||
| 
 | ||||
| ```bash | ||||
| node SheetJSWriteToS3.js | ||||
| ``` | ||||
| 
 | ||||
| 5) In the S3 site, select the bucket and download the object named `test.xlsx`. | ||||
| Open the file in a spreadsheet editor. | ||||
| 
 | ||||
| </details> | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user