Amazon 网络服务
Amazon 网络服务(AWS)是一个云服务平台,包括传统虚拟机支持、"无服务器功能" 和云存储。
¥Amazon Web Services (AWS) is a cloud services platform which includes traditional virtual machine support, "Serverless Functions" and cloud storage.
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
该演示探讨了两个关键的 AWS 产品:
¥This demo explores two key AWS offerings:
-
"Lambda 函数" ("Lambda") 探索无服务器计算产品。该演示创建了一个 JavaScript 函数,可以处理用户提交的文件并生成电子表格。
¥"Lambda Functions" ("Lambda") explores the serverless computing offering. The demo creates a JavaScript function that can process user-submitted files and generate spreadsheets.
-
"S3 存储" 探索云存储 ("S3") 产品。该演示使用 NodeJS 连接库从 S3 读取电子表格并将电子表格写入 S3 存储桶。
¥"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.
AWS 迭代速度很快,无法保证所引用的服务将来可用。
¥AWS iterates quickly and there is no guarantee that the referenced services will be available in the future.
此演示最后一次于 2024 年 6 月 13 日进行测试。
¥This demo was last tested on 2024 June 13.
Lambda 函数
¥Lambda Functions
AWS 提供 NodeJS 运行时来运行 JavaScript 无服务器函数。[^1]
¥AWS offers NodeJS runtimes for running JavaScript serverless functions.[^1]
Lambda 函数中可能需要 SheetJS NodeJS 模块。部署时,可以将整个 node_modules
文件夹添加到 ZIP 包中。
¥The SheetJS NodeJS module can be
required in Lambda functions. When deploying, the entire node_modules
folder
can be added to the ZIP package.
在本演示中,使用了 "函数地址"(自动 API 网关管理)功能。较旧的部署需要特殊的 "二进制媒体类型" 来处理 XLSX 等格式。测试时不需要配置。
¥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.
Node.js 运行时可以使用 x86_64
或 arm64
CPU 架构。SheetJS 库可在 Linux、Windows 和 macOS 操作系统的两个平台上运行。
¥Node.js runtime can use x86_64
or arm64
CPU architectures. SheetJS libraries
work on both platforms in Linux, Windows, and macOS operating systems.
读取数据
¥Reading Data
在 Lambda 处理程序中,event.body
属性是表示 HTTP 请求表单数据的 Base64 编码字符串。必须解析该主体。
¥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
busboy
主体解析器 [^2] 在 NodeJS 部署中经过了实战测试。
¥The busboy
body parser[^2] is battle-tested in NodeJS deployments.
busboy
为表单正文中的每个文件触发 'file'
事件。回调接收应收集到缓冲区中的 NodeJS 流:
¥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:
/* 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
会触发 'finish'
事件。回调可以假设表单主体中的每个文件都已存储在 NodeJS Buffer 对象中。
¥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.
处理 NodeJS 缓冲区
¥Processing NodeJS Buffers
SheetJS read
方法 [^3] 可以读取 Buffer 对象并生成 SheetJS 工作簿对象 [^4],可以使用其他 API 函数进行处理。
¥The SheetJS read
method[^3] can read the Buffer objects and generate SheetJS
workbook objects[^4] which can be processed with other API functions.
例如,处理程序可以使用 sheet_to_csv
[^5] 生成 CSV 文本:
¥For example, a handler can use sheet_to_csv
[^5] to generate CSV text:
/* 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 */
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) });
});
Complete Code Sample (click to show)
This example takes the first uploaded file submitted with the key upload
,
parses the file and returns the CSV content of the first worksheet.
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 });
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]; });
});
/* 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 */
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 */
bb.end(Buffer.from(event.body, "base64"));
};
写入数据
¥Writing Data
为了安全地传输二进制数据,应使用 Base64 字符串。
¥For safely transmitting binary data, Base64 strings should be used.
带有选项 type: "base64"
的 SheetJS write
method[^6] 将生成 Base64 编码的字符串。
¥The SheetJS write
method[^6] with the option type: "base64"
will generate
Base64-encoded strings.
/* 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" });
Lambda 回调响应函数接受选项。将 isBase64Encoded
设置为 true
将确保回调处理程序对数据进行解码。为了确保浏览器尝试下载响应,必须设置 Content-Disposition
标头:
¥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:
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"'
}
});
Complete Code Sample (click to show)
This example creates a sample workbook object and sends the file in the response:
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"});
/* write to XLSX file in Base64 encoding */
var body = XLSX.write(wb, { type: "base64", bookType: "xlsx" });
/* mark as attached file */
var headers = { "Content-Disposition": 'attachment; filename="SheetJSLambda.xlsx"'};
/* Send back data */
callback(null, {
statusCode: 200,
isBase64Encoded: true,
body: body,
headers: headers
});
};
Lambda 演示
¥Lambda Demo
截至撰写本文时,AWS 免费套餐包括每月 100 万个免费请求和 40 万 GB 秒的计算资源。
¥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.
-
如果你没有账户,请创建一个新的 AWS 免费套餐账户 [^7]。
¥If you do not have an account, create a new AWS free tier account[^7].
创建项目 ZIP
¥Create Project ZIP
-
创建一个新的项目文件夹:
¥Create a new project folder:
mkdir -p SheetJSLambda
cd SheetJSLambda
curl -LO https://xlsx.nodejs.cn/aws/index.js
-
安装 SheetJS NodeJS 模块和
busboy
依赖:¥Install the SheetJS NodeJS module and
busboy
dependency:
mkdir -p node_modules
npm i https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz busboy
-
创建该文件夹内容的 .zip 包:
¥Create a .zip package of the contents of the folder:
zip -c ../SheetJSLambda.zip -r .
Lambda 设置
¥Lambda Setup
-
使用 root 用户账户登录 AWS 管理控制台。
¥Sign into the AWS Management Console with a root user account.
-
在顶部搜索框中输入 "Lambda",然后单击 Lambda(在“服务”下)。
¥Type "Lambda" in the top search box and click Lambda (under Services).
-
打开左侧边栏中的 "函数"。
¥Open "Functions" in the left sidebar.
如果左侧边栏未打开,请单击页面左边缘的 ≡
图标。
¥If the left sidebar is not open, click the ≡
icon in the left edge of the page.
-
单击主面板中的 "创建函数" 按钮。
¥Click the "Create function" button in the main panel.
-
选择以下选项:
¥Select the following options:
-
在顶部列表中,选择 "作者从零开始"(默认选择)
¥In the top list, select "Author from scratch" (default choice)
-
输入令人难忘的 "函数名称"(上次测试时为 "SheetJSLambda")
¥Type a memorable "Function Name" ("SheetJSLambda" when last tested)
-
在 "运行" 下拉列表中,查找 "最新支持" 部分并选择 "Node.js"(上次测试时为 "Node.js 20.x")
¥In the "Runtime" dropdown, look for the "Latest supported" section and select "Node.js" ("Node.js 20.x" when last tested)
-
展开 "高级设置" 并选中 "启用功能 URL"。这将显示一些子选项:
¥Expand "Advanced Settings" and check "Enable function URL". This will display a few sub-options:
-
"认证类型" 选择 "NONE"(禁用 IAM 身份验证)
¥"Auth type" select "NONE" (disable IAM authentication)
-
检查 "配置跨源资源共享 (CORS)"
¥Check "Configure cross-origin resource sharing (CORS)"
-
-
单击 "创建函数" 创建函数。
¥Click "Create function" to create the function.
上传代码
¥Upload Code
-
在界面中,向下滚动并选择 "代码" 选项卡。
¥In the Interface, scroll down and select the "Code" tab.
-
单击 "上传自" 下拉列表并选择 ".zip 文件"。
¥Click the "Upload from" dropdown and select ".zip file".
-
单击模式中的 "上传" 按钮。使用文件选择器,选择步骤 3 中创建的
SheetJSLambda.zip
文件。单击 "保存"。¥Click the "Upload" button in the modal. With the file picker, select the
SheetJSLambda.zip
file created in step 3. Click "Save".
上次测试演示时,ZIP 足够小,Lambda 代码编辑器可以加载该包。
¥When the demo was last tested, the ZIP was small enough that the Lambda code editor will load the package.
-
在代码编辑器中,双击
index.js
并确认代码编辑器显示 JavaScript 代码。¥In the code editor, double-click
index.js
and confirm the code editor displays JavaScript code.
外部访问
¥External Access
-
单击选项卡列表中的 "配置"。
¥Click "Configuration" in the tab list.
-
在选项卡列表下方的侧栏中,选择 "函数地址" 并单击 "编辑"。
¥In the sidebar below the tab list, select "Function URL" and click "Edit".
-
将 "认证类型" 设置为 "NONE",然后单击“保存”。该页面将重定向到函数属性。
¥Set the "Auth type" to "NONE" and click Save. The page will redirect to the Function properties.
-
选择 "配置" 选项卡,然后在左侧边栏中选择 "权限"。
¥Select the "Configuration" tab and select "Permissions" in the left sidebar.
-
向下滚动到 "基于资源的政策声明" 并确保
FunctionURLAllowPublicAccess
已列出。¥Scroll down to "Resource-based policy statements" and ensure that
FunctionURLAllowPublicAccess
is listed.
如果未定义策略语句,请选择 "添加权限" 并使用以下选项:
¥If no policy statements are defined, select "Add Permission" with the options:
-
选择顶部的 "函数地址"
¥Select "Function URL" at the top
-
验证类型:NONE
¥Auth type: NONE
-
确保语句 ID 设置为
FunctionURLAllowPublicAccess
¥Ensure that Statement ID is set to
FunctionURLAllowPublicAccess
-
确保主体设置为
*
¥Ensure that Principal is set to
*
-
确保操作设置为
lambda:InvokeFunctionUrl
¥Ensure that Action is set to
lambda:InvokeFunctionUrl
单击 "保存",应创建一个新的策略声明。
¥Click "Save" and a new Policy statement should be created.
Lambda 测试
¥Lambda Testing
-
找到函数 URL(位于 "功能概述" 部分)。
¥Find the Function URL (It is in the "Function Overview" section).
-
尝试在网络浏览器中访问函数 URL。
¥Try to access the function URL in a web browser.
该站点将尝试下载 SheetJSLambda.xlsx
。保存并打开文件以确认其有效。
¥The site will attempt to download SheetJSLambda.xlsx
. Save and open the file
to confirm it is valid.
-
下载 https://xlsx.nodejs.cn/pres.numbers 并向公共函数 URL 发送 POST 请求。
¥Download https://xlsx.nodejs.cn/pres.numbers and make a POST request to the public function URL.
这可以在命令行上进行测试。更改命令中的 FUNCTION_URL
:
¥This can be tested on the command line. Change FUNCTION_URL
in the commands:
curl -LO https://xlsx.nodejs.cn/pres.numbers
curl -X POST -F "upload=@pres.numbers" FUNCTION_URL
终端将显示第一张表的 CSV 输出。
¥The terminal will display CSV output of the first sheet.
S3 存储
¥S3 Storage
S3 和所有 AWS 服务的主要 NodeJS 模块是 aws-sdk
[^8]。
¥The main NodeJS module for S3 and all AWS services is aws-sdk
[^8].
NodeJS 脚本中可能需要 SheetJS NodeJS 模块。
¥The SheetJS NodeJS module can be required in NodeJS scripts.
连接到 S3
¥Connecting to S3
aws-sdk
模块导出执行连接的函数 S3
。该函数需要一个包含 API 版本和凭据的选项对象。必须使用 IAM 用户 [^9] 的访问密钥:
¥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.
Access keys for an IAM user[^9] must be used:
/* credentials */
var accessKeyId = "...", secretAccessKey = "..."";
/* file location */
var Bucket = "...", Key = "pres.numbers";
/* connect to s3 account */
var AWS = require('aws-sdk');
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
credentials: { accessKeyId, secretAccessKey }
});
下载数据
¥Downloading Data
从 S3 获取文件
¥Fetching Files from S3
s3#getObject
方法返回一个带有 createReadStream
方法的对象。createReadStream
返回 NodeJS 流:
¥The s3#getObject
method returns an object with a createReadStream
method.
createReadStream
returns a NodeJS stream:
/* open stream to the file */
var stream = s3.getObject({ Bucket: Bucket, Key: Key }).createReadStream();
连接 NodeJS 流
¥Concatenating NodeJS Streams
缓冲区可以从流连接成一个统一的 Buffer 对象:
¥Buffers can be concatenated from the stream into one unified Buffer object:
/* array of buffers */
var bufs = [];
/* 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 */
});
解析 NodeJS 缓冲区
¥Parsing NodeJS Buffers
SheetJS read
方法 [^10] 可以读取最终对象并生成 SheetJS 工作簿对象 [^11],可以使用其他 API 函数进行处理。
¥The SheetJS read
method[^10] can read the final object and generate SheetJS
workbook objects[^11] which can be processed with other API functions.
例如,回调可以使用 sheet_to_csv
[^12] 生成 CSV 文本:
¥For example, a callback can use sheet_to_csv
[^12] to generate CSV text:
stream.on('end', function() {
/* concatenate */
var buf = Buffer.concat(bufs);
/* parse */
var wb = XLSX.read(Buffer.concat(bufs));
/* 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);
});
上传数据
¥Uploading Data
带有选项 type: "buffer"
的 SheetJS write
method[^13] 将生成 NodeJS 缓冲区。S3#upload
直接接受这些 Buffer 对象。
¥The SheetJS write
method[^13] with the option type: "buffer"
will generate
NodeJS Buffers. S3#upload
directly accepts these Buffer objects.
此示例创建一个示例工作簿对象,在 NodeJS 缓冲区中生成 XLSX 文件数据,并将数据上传到 S3:
¥This example creates a sample workbook object, generates XLSX file data in a NodeJS Buffer, and uploads the data to S3:
/* 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);
});
S3 演示
¥S3 Demo
截至撰写本文时,AWS 免费套餐包括 5GB S3 存储,每月可处理 20,000 个 Get 请求和 2000 个 Put 请求。
¥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.
此示例从 S3 获取缓冲区并解析工作簿。
¥This sample fetches a buffer from S3 and parses the workbook.
-
如果你没有账户,请创建一个新的 AWS 免费套餐账户 [^14]。
¥If you do not have an account, create a new AWS free tier account[^14].
创建 S3 存储桶
¥Create S3 Bucket
-
使用 root 用户账户登录 AWS 管理控制台。
¥Sign into the AWS Management Console with a root user account.
-
在顶部搜索框中输入 "S3",然后单击 S3(在“服务”下)。
¥Type "S3" in the top search box and click S3 (under Services).
-
打开左侧边栏中的 "水桶"。
¥Open "Buckets" in the left sidebar.
如果左侧边栏未打开,请单击页面左边缘的 ≡
图标。
¥If the left sidebar is not open, click the ≡
icon in the left edge of the page.
-
单击主面板中的 "创建桶" 按钮。
¥Click the "Create bucket" button in the main panel.
-
选择以下选项:
¥Select the following options:
-
输入令人难忘的 "桶名称"(上次测试时为 "sheetjsbouquet")
¥Type a memorable "Bucket Name" ("sheetjsbouquet" when last tested)
-
在 "对象所有权" 部分中,选择 "ACL 已禁用"
¥In the "Object Ownership" section, select "ACLs disabled"
-
检查 "阻止所有公共访问"
¥Check "Block all public access"
-
查找 "存储桶版本控制" 部分并选择 "禁用"
¥Look for the "Bucket Versioning" section and select "Disable"
-
单击 "创建桶" 创建桶。
¥Click "Create bucket" to create the bucket.
创建 IAM 用户
¥Create IAM User
-
在顶部搜索框中输入 "IAM",然后单击 IAM(在“服务”下)。
¥Type "IAM" in the top search box and click IAM (under Services).
-
打开左侧边栏中的 "用户"。
¥Open "Users" in the left sidebar.
如果左侧边栏未打开,请单击页面左边缘的 ≡
图标。
¥If the left sidebar is not open, click the ≡
icon in the left edge of the page.
-
单击主面板中的 "创建用户" 按钮。
¥Click the "Create user" button in the main panel.
-
在步骤 1 中,键入令人难忘的 "桶名称"(上次测试时为 "sheetjs-user")。单击 "下一个"。
¥In step 1, type a memorable "Bucket Name" ("sheetjs-user" when last tested). Click "Next".
-
在步骤 2 中,单击 "下一个"
¥In step 2, click "Next"
-
步骤 3 中,点击 "创建用户" 创建用户。
¥In step 3, click "Create user" to create the user.
添加权限
¥Add Permissions
-
单击“用户”表中的新用户名。
¥Click the new user name in the Users table.
-
选择 "权限" 选项卡
¥Select the "Permissions" tab
-
单击 "添加权限" 下拉列表并选择 "添加权限"。
¥Click the "Add permissions" dropdown and select "Add permissions".
-
选择 "直接附加策略"。
¥Select "Attach policies directly".
-
在 "权限策略" 部分中,搜索 "AmazonS3 完全访问"。应该有一个条目。
¥In the "Permissions policies" section, search for "AmazonS3FullAccess". There should be one entry.
-
选中 "AmazonS3 完全访问" 旁边的复选框,然后单击 "下一个" 按钮。
¥Check the checkbox next to "AmazonS3FullAccess" and click the "Next" button.
-
在 "审查" 屏幕中,单击 "添加权限"
¥In the "Review" screen, click "Add permissions"
生成键
¥Generate Keys
-
单击 "安全凭证",然后单击 "创建访问密钥"。
¥Click "Security credentials", then click "Create access key".
-
选择 "本地代码" 选项。勾选 "我了解上述建议并希望继续创建访问密钥。" 并点击 "下一个"
¥Select the "Local code" option. Check "I understand the above recommendation and want to proceed to create an access key." and click "Next"
-
单击 "创建访问密钥",然后在下一个屏幕中单击 "下载 .csv 文件"。
¥Click "Create Access Key" and click "Download .csv file" in the next screen.
在生成的 CSV 中:
¥In the generated CSV:
-
单元格 A2 是 "访问密钥 ID"(AWS API 中的
accessKeyId
)¥Cell A2 is the "Access key ID" (
accessKeyId
in the AWS API) -
单元 B2 是 "秘密访问密钥"(AWS API 中的
secretAccessKey
)¥Cell B2 is the "Secret access key" (
secretAccessKey
in the AWS API)
设置项目
¥Set up Project
-
创建一个新的 NodeJS 项目:
¥Create a new NodeJS project:
mkdir SheetJSS3
cd SheetJSS3
npm init -y
-
安装依赖:
¥Install dependencies:
mkdir -p node_modules
npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz aws-sdk@2.1467.0
编写测试
¥Write Test
此示例创建一个简单的工作簿,生成 NodeJS 缓冲区,并将缓冲区上传到 S3。
¥This sample creates a simple workbook, generates a NodeJS buffer, and uploads the buffer to S3.
| A | B | C | D | E | F | G |
---+---|---|---|---|---|---|---|
1 | S | h | e | e | t | J | S |
2 | 5 | 4 | 3 | 3 | 7 | 9 | 5 |
-
将以下脚本保存到
SheetJSWriteToS3.js
:¥Save the following script to
SheetJSWriteToS3.js
:
var XLSX = require("xlsx");
var AWS = require('aws-sdk');
/* replace these constants */
var accessKeyId = "<REPLACE WITH ACCESS KEY ID>";
var secretAccessKey = "<REPLACE WITH SECRET ACCESS KEY>";
var Bucket = "<REPLACE WITH BUCKET NAME>";
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]]);
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);
});
-
编辑
SheetJSWriteToS3.js
并替换高亮的行:¥Edit
SheetJSWriteToS3.js
and replace the highlighted lines:
-
accessKeyId
:AWS 账户的访问密钥¥
accessKeyId
: access key for the AWS account -
secretAccessKey
:AWS 账户的秘密访问密钥¥
secretAccessKey
: secret access key for the AWS account -
Bucket
:存储桶的名称¥
Bucket
: name of the bucket
密钥可在步骤 22 的 CSV 中找到。Bucket 是第 5 步中的名称。
¥The keys are found in the CSV from step 22. The Bucket is the name from step 5.
-
运行脚本:
¥Run the script:
node SheetJSWriteToS3.js
该文件将以对象名称 test.xlsx
存储。可以从 S3 Web 界面手动下载。
¥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".
-
将以下脚本保存到
SheetJSReadFromS3.js
:¥Save the following script to
SheetJSReadFromS3.js
:
var XLSX = require("xlsx");
var AWS = require('aws-sdk');
/* replace these constants */
var accessKeyId = "<REPLACE WITH ACCESS KEY ID>";
var secretAccessKey = "<REPLACE WITH SECRET ACCESS KEY>";
var Bucket = "<REPLACE WITH BUCKET NAME>";
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]]));
});
-
编辑
SheetJSReadFromS3.js
并替换高亮的行:¥Edit
SheetJSReadFromS3.js
and replace the highlighted lines:
-
accessKeyId
:AWS 账户的访问密钥¥
accessKeyId
: access key for the AWS account -
secretAccessKey
:AWS 账户的秘密访问密钥¥
secretAccessKey
: secret access key for the AWS account -
Bucket
:存储桶的名称¥
Bucket
: name of the bucket
密钥可在步骤 22 的 CSV 中找到。Bucket 是第 5 步中的名称。
¥The keys are found in the CSV from Step 22. The Bucket is the name from Step 5.
-
运行脚本:
¥Run the script:
node SheetJSReadFromS3.js
该程序将以 CSV 格式显示数据。
¥The program will display the data in CSV format.
S,h,e,e,t,J,S
5,4,3,3,7,9,5
[^1]: 请参阅 AWS 文档中的 "使用 Node.js 构建 Lambda 函数"
¥See "Building Lambda functions with Node.js" in the AWS documentation
[^2]: busboy
模块分布式 在公共 NPM 注册表中
¥The busboy
module is distributed on the public NPM registry
[^3]: 见 read
于 "读取文件"
[^4]: 详细信息请参见 "工作簿对象" 于 "SheetJS 数据模型"。
¥See "Workbook Object" in "SheetJS Data Model" for more details.
[^5]: 见 sheet_to_csv
于 "CSV 和文本"
¥See sheet_to_csv
in "CSV and Text"
[^6]: 见 write
于 "写入文件"
[^7]: 注册免费账户 在 AWS 免费套餐上 需要有效的调用号码和有效的信用卡。
¥Registering for a free account on the AWS Free Tier requires a valid phone number and a valid credit card.
[^8]: aws-sdk
模块分布式 在公共 NPM 注册表中
¥The aws-sdk
module is distributed on the public NPM registry
[^9]: 请参阅 AWS 文档中的 "管理 IAM 用户的访问密钥"
¥See "Managing access keys for IAM users" in the AWS documentation
[^10]: 见 read
于 "读取文件"
[^11]: 详细信息请参见 "工作簿对象" 于 "SheetJS 数据模型"。
¥See "Workbook Object" in "SheetJS Data Model" for more details.
[^12]: 见 sheet_to_csv
于 "CSV 和文本"
¥See sheet_to_csv
in "CSV and Text"
[^13]: 见 write
于 "写入文件"
[^14]: 注册免费账户 在 AWS 免费套餐上 需要有效的调用号码和有效的信用卡。
¥Registering for a free account on the AWS Free Tier requires a valid phone number and a valid credit card.