This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/main by this push:
new fa9e5d1b chore(bindings/nodejs): Add s3 example (#2082)
fa9e5d1b is described below
commit fa9e5d1b07475e100503200f33fc6bc2250ddff2
Author: imp2002 <[email protected]>
AuthorDate: Sun Apr 23 18:00:37 2023 +0800
chore(bindings/nodejs): Add s3 example (#2082)
* chore(bindings/nodejs): Add s3 example
* chore: add a nodejs examples README
* fix: fmt with prettier
---
bindings/nodejs/examples/README.md | 42 ++++++++++++++++++++++++++++++++++
bindings/nodejs/examples/example_s3.js | 39 +++++++++++++++++++++++++++++++
2 files changed, 81 insertions(+)
diff --git a/bindings/nodejs/examples/README.md
b/bindings/nodejs/examples/README.md
new file mode 100644
index 00000000..bd4b3dd1
--- /dev/null
+++ b/bindings/nodejs/examples/README.md
@@ -0,0 +1,42 @@
+# OpenDAL Node.js examples
+
+## s3 example
+
+```javascript
+import { Operator } from "opendal";
+
+async function main() {
+ // you can try to config Operator in the following two ways
+ // 1. config Operator plainly
+ const op = new Operator("s3", {
+ root: "/test_opendal",
+ bucket: "your bucket name",
+ region: "your bucket region",
+ endpoint: "your endpoint",
+ access_key_id: "your access key id",
+ secret_access_key: "your secret access key",
+ });
+
+ // 2. Or config Operator from .env, opendal will load from the environment
by default
+ const endpoint = process.env.AWS_S3_ENDPOINT;
+ const region = process.env.AWS_S3_REGION;
+ const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
+ const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
+ const bucket = process.env.AWS_BUCKET;
+
+ const opendal = new Operator("s3", {
+ root: "/",
+ bucket,
+ endpoint,
+ });
+
+ // Use operator to do some operations
+ await op.write("test", "Hello, World!");
+ const bs = await op.read("test");
+ console.log(new TextDecoder().decode(bs));
+ const meta = await op.stat("test");
+ console.log(`contentLength: ${meta.contentLength}`);
+}
+
+main();
+```
diff --git a/bindings/nodejs/examples/example_s3.js
b/bindings/nodejs/examples/example_s3.js
new file mode 100644
index 00000000..2a112f43
--- /dev/null
+++ b/bindings/nodejs/examples/example_s3.js
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { Operator } from 'opendal'
+
+async function main() {
+ const op = new Operator('s3', {
+ root: '/test_opendal',
+ bucket: 'your bucket name',
+ region: 'your bucket region',
+ endpoint: 'your endpoint',
+ access_key_id: 'your access key id',
+ secret_access_key: 'your secret access key',
+ })
+
+ await op.write('test', 'Hello, World!')
+ const bs = await op.read('test')
+ console.log(new TextDecoder().decode(bs))
+ const meta = await op.stat('test')
+ console.log(`contentLength: ${meta.contentLength}`)
+}
+
+main()