Skip to content

Commit df89df0

Browse files
authored
feat: add typescript sdk support (#41)
1 parent 5f1edd3 commit df89df0

File tree

3 files changed

+138
-14
lines changed

3 files changed

+138
-14
lines changed

docs/zh/developer/sdk/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RustFS 是 100% 兼容 S3 协议的分布式对象存储软件。 用户可以
1717
- [JavaScript SDK](./javascript.md)
1818
- [Python SDK](./python.md)
1919
- [Rust SDK](./rust.md)
20+
- [TypeScript SDK](./typescript.md)
2021

2122
## 阅读前名词解释
2223

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: "RustFS TypeScript SDK 使用指南"
3+
description: "通过 TypeScript SDK 来对 RustFS 实例进行操作,包括存储桶、对象的创建和删除."
4+
---
5+
6+
# TypeScript Rust SDK
7+
8+
由于 RustFS 是完全兼容 S3 的对象存储系统,因此可以通过对 S3 的 TypeScript SDK 做一些封装来构建适用于 RustFS 的 TypeScript SDK,通过 SDK 对 RustFS 进行操作,包括存储桶/对象的创建和删除、文件的上传和下载等。
9+
10+
## 前提条件
11+
12+
- 一个可用的 RustFS 实例(可参考[安装指南](../../installation/index.md)进行安装)。
13+
- 访问密钥(可参考[访问密钥管理](../../administration/iam/access-token.md)进行创建)。
14+
15+
## RustFS TypeScript SDK 构造
16+
17+
借助于 TypeScript 的 S3Client,使用 `region``access_key_id``secret_access_key` 以及 `endpoint_url` 构造一个 RustFS 客户端:
18+
19+
```
20+
const rustfs_client = new S3Client({
21+
region: "cn-east-1",
22+
credentials: {
23+
accessKeyId: process.env.RUSTFS_ACCESS_KEY_ID!,
24+
secretAccessKey: process.env.RUSTFS_SECRET_ACCESS_KEY!,
25+
},
26+
endpoint: process.env.RUSTFS_ENDPOINT_URL!,
27+
});
28+
```
29+
30+
接着使用构造好的 `rustfs_client` 进行响应的操作。
31+
32+
## 创建存储桶
33+
34+
```
35+
async function createBucket() {
36+
try {
37+
const response = await rustfs_client.send(new CreateBucketCommand({
38+
Bucket: "my-bucket",
39+
}));
40+
console.log(response);
41+
} catch (error) {
42+
console.log(error);
43+
}
44+
}
45+
```
46+
47+
## 删除存储桶
48+
49+
```
50+
async function deleteBucket() {
51+
try {
52+
const response = await rustfs_client.send(new DeleteBucketCommand({
53+
Bucket: "my-bucket",
54+
}));
55+
console.log(response);
56+
} catch (error) {
57+
console.log(error);
58+
}
59+
}
60+
```
61+
62+
## 列出存储桶
63+
64+
```
65+
async function listBuckets() {
66+
try {
67+
const response = await rustfs_client.send(new ListBucketsCommand({}));
68+
console.log(response);
69+
} catch (error) {
70+
console.log(error);
71+
}
72+
}
73+
```
74+
75+
## 列出对象
76+
77+
```
78+
async function listObjects() {
79+
try {
80+
const response = await rustfs_client.send(new ListObjectsV2Command({
81+
Bucket: "rust-sdk-demo",
82+
}));
83+
console.log(response);
84+
} catch (error) {
85+
console.log(error);
86+
}
87+
}
88+
```
89+
90+
## 上传文件
91+
92+
```
93+
async function uploadFile() {
94+
try {
95+
const response = await rustfs_client.send(new PutObjectCommand({
96+
Bucket: "my-bucket",
97+
Key: "/test/1.txt",
98+
Body: fs.createReadStream("/Users/jhma/Desktop/1.txt"),
99+
}));
100+
} catch (error) {
101+
console.log(error);
102+
}
103+
}
104+
```
105+
106+
## 下载对象
107+
108+
```
109+
async function getObject() {
110+
try {
111+
const response = await rustfs_client.send(new GetObjectCommand({
112+
Bucket: "rust-sdk-demo",
113+
Key: "1.txt",
114+
}));
115+
116+
// get object content
117+
if (response.Body) {
118+
const chunks: Buffer[] = [];
119+
for await (const chunk of response.Body as any) {
120+
chunks.push(chunk as Buffer);
121+
}
122+
const data = Buffer.concat(chunks).toString("utf-8");
123+
console.log("Object content:", data);
124+
}
125+
} catch (error) {
126+
console.log(error);
127+
}
128+
}
129+
```
130+
131+
其他的使用,大家可以自行探索,如果借助 Vibe Coding,就更简单了!

docs/zh/sidebar.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const zhSidebar = [
66
items: [
77
{
88
text: 'Linux安装',
9-
link: '/zh/installation/linux/index'
9+
link: '/zh/installation/linux/index',
1010
items: [
1111
{
1212
text: 'Linux快速安装',
@@ -467,23 +467,15 @@ export const zhSidebar = [
467467
text: 'JavaScript',
468468
link: '/zh/developer/sdk/javascript'
469469
},
470-
// {
471-
// text: '.Net',
472-
// link: '/zh/developer/sdk/dotnet'
473-
// },
474-
// {
475-
// text: 'PHP',
476-
// link: '/zh/developer/sdk/php'
477-
// },
478-
// {
479-
// text: 'Go',
480-
// link: '/zh/developer/sdk/go'
481-
// },
470+
{
471+
text: 'TypeScript',
472+
link: '/zh/developer/sdk/typescript'
473+
},
482474
{
483475
text: '其他 SDK',
484476
link: '/zh/developer/sdk/other'
485477
}
486-
]
478+
],
487479
},
488480
{
489481
text: 'S3 兼容性 API',

0 commit comments

Comments
 (0)