Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions docs/developer/sdk/csharp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
---
title: "C# SDK"
description: "本文主要讲解 RustFS 中 C# SDK 的使用。"
---

# C# SDK

RustFS 兼容 Amazon S3 协议,因此我们推荐使用官方的 AWS SDK for .NET (`AWSSDK.S3`) 与 RustFS 进行交互。这为您提供了稳定、高性能且维护良好的客户端体验。

## 1. 安装 SDK

在您的 .NET 项目中,通过 NuGet 包管理器安装 `AWSSDK.S3`。

### 使用 .NET CLI

```bash
dotnet add package AWSSDK.S3
```

## 2. 初始化客户端

在使用 SDK 之前,您需要初始化 `AmazonS3Client`。关键配置在于设置 `ServiceURL` 指向您的 RustFS 服务地址,并启用 `ForcePathStyle`。

```csharp
using Amazon.S3;
using Amazon.S3.Model;

// 配置 RustFS 连接信息
var config = new AmazonS3Config
{
// RustFS 服务地址,例如 http://localhost:9000
ServiceURL = "http://127.0.0.1:9000",

// 必须启用 Path Style 访问模式
ForcePathStyle = true,

// 如果不使用 HTTPS,请设置为 true(默认为 false)
UseHttp = true
};

// 使用 Access Key 和 Secret Key 初始化客户端
var accessKey = "admin";
var secretKey = "admin123";
IAmazonS3 s3Client = new AmazonS3Client(accessKey, secretKey, config);
```

> **注意**:`ForcePathStyle = true` 是连接 RustFS 的必要配置。如果不设置,SDK 默认会使用 Virtual Hosted-Style(例如 `http://bucketname.localhost:9000`),这可能导致连接失败。

## 3. 基本操作示例

以下是一些常见操作的代码示例。

### 3.1 创建存储桶 (Create Bucket)

```csharp
public async Task CreateBucketAsync(IAmazonS3 client, string bucketName)
{
var putBucketRequest = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = false
};

await client.PutBucketAsync(putBucketRequest);
Console.WriteLine($"Bucket '{bucketName}' created successfully.");
}
```

### 3.2 列出存储桶 (List Buckets)

```csharp
public async Task ListBucketsAsync(IAmazonS3 client)
{
var response = await client.ListBucketsAsync();
Console.WriteLine("Buckets:");
foreach (var bucket in response.Buckets)
{
Console.WriteLine($" - {bucket.BucketName} (Created: {bucket.CreationDate})");
}
}
```

### 3.3 上传文件 (Upload Object)

```csharp
public async Task UploadFileAsync(IAmazonS3 client, string bucketName, string key, string filePath)
{
var putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = key,
FilePath = filePath,
ContentType = "application/octet-stream"
};

await client.PutObjectAsync(putRequest);
Console.WriteLine($"File uploaded to {bucketName}/{key}");
}
```

### 3.4 下载文件 (Download Object)

```csharp
public async Task DownloadFileAsync(IAmazonS3 client, string bucketName, string key, string downloadPath)
{
var request = new GetObjectRequest
{
BucketName = bucketName,
Key = key
};

using var response = await client.GetObjectAsync(request);
using var responseStream = response.ResponseStream;
using var fileStream = File.Create(downloadPath);

await responseStream.CopyToAsync(fileStream);
Console.WriteLine($"File downloaded to {downloadPath}");
}
```

### 3.5 删除文件 (Delete Object)

```csharp
public async Task DeleteFileAsync(IAmazonS3 client, string bucketName, string key)
{
var deleteRequest = new DeleteObjectRequest
{
BucketName = bucketName,
Key = key
};

await client.DeleteObjectAsync(deleteRequest);
Console.WriteLine($"File {key} deleted from {bucketName}");
}
```

### 3.6 生成预签名 URL (Generate Presigned URL)

预签名 URL 允许您生成一个临时的、带签名的 URL,供用户上传或下载文件,而无需暴露您的 Access Key。

```csharp
public string GeneratePresignedUrl(IAmazonS3 client, string bucketName, string key, double durationMinutes)
{
var request = new GetPreSignedUrlRequest
{
BucketName = bucketName,
Key = key,
Verb = HttpVerb.GET, // 或 HttpVerb.PUT 用于上传
Expires = DateTime.UtcNow.AddMinutes(durationMinutes)
};

string url = client.GetPreSignedURL(request);
Console.WriteLine($"Presigned URL: {url}");
return url;
}
```

## 4. 依赖注入集成 (ASP.NET Core)

如果您正在使用 ASP.NET Core,可以通过依赖注入容器来注册 `IAmazonS3` 服务。

```csharp
// Program.cs

using Amazon.S3;
using Amazon.Extensions.NETCore.Setup;

var builder = WebApplication.CreateBuilder(args);

// 从配置中读取 RustFS 设置
var rustfsOptions = builder.Configuration.GetSection("RustFS");
var serviceUrl = rustfsOptions["ServiceUrl"] ?? "http://localhost:9000";
var accessKey = rustfsOptions["AccessKey"] ?? "admin";
var secretKey = rustfsOptions["SecretKey"] ?? "admin123";

builder.Services.AddSingleton<IAmazonS3>(sp =>
{
var config = new AmazonS3Config
{
ServiceURL = serviceUrl,
ForcePathStyle = true,
UseHttp = true // 根据实际情况配置
};

return new AmazonS3Client(accessKey, secretKey, config);
});

// ... 其他服务注册
```

## 5. 参考项目

完整的集成示例可以参考 [RustFS DotNet Demo](https://github.com/rustfs/rustfs-dotnet-demo) 项目。
47 changes: 27 additions & 20 deletions docs/developer/sdk/index.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
---
title: "RustFS SDK Overview"
description: "Overview of supported S3 SDKs for RustFS."
title: "RustFS SDK 概述"
description: "RustFS 可以使用哪些 S3 的 SDK?在这篇文章中进行了详细的说明。"
---

# SDK Overview
# SDK 概述

RustFS is a distributed object storage system fully compatible with the S3 protocol. Users can:
RustFS 是 100% 兼容 S3 协议的分布式对象存储软件。 用户可以通过:

- Manage RustFS through the Console management interface.
- Manage RustFS through S3 clients.
- Implement object storage operations and management on the business side through SDKs.
1. Console 控制台管理 RustFS;
2. 可以通过 S3 客户端管理 RustFS;
3. 也可以通过 SDK 在业务端实现对于对象存储的操作和管理。

Currently, the SDKs provided by RustFS include:
目前 RustFS 提供的 SDK 包括:

- [C# SDK](./csharp.md)
- [Java SDK](./java.md)
- [JavaScript SDK](./javascript.md)
- [Python SDK](./python.md)
- [Rust SDK](./rust.md)
- [TypeScript SDK](./typescript.md)
- [Golang SDK](./go.md)

## Terminology
## 阅读前名词解释

Amazon S3 (Simple Storage Service) was the first widely adopted object storage service. Its API has become the de facto standard for object storage. In this documentation, "S3" refers to the protocol.
S3 是亚马逊最早开放和推出的对象存储的产品名称。并且,他开放了他的全部协议和规范。后来,几乎所有的对象存储都遵循了 S3 的协议和规范。有时人们把 S3 称为对象存储,有的时候又简称 S3 为对象存储协议。

## 1. SDK Recommendations
## 1. SDK 推荐

We recommend using the official AWS S3 SDKs. These SDKs are mature, well-maintained, and highly optimized.
由于市面上已经有太多的经过多年维护的 SDK 了。如 AWS S3 SDK 经过多年的调试和调优。他的性能、错误几乎为 0。因此,我们推荐您直接使用标准的 AWS 的 S3 SDK 直接控制 RustFS 与 RustFS 进行通信。

If you have a familiar and trusted SDK from a vendor, you can use it.
如果您有熟悉的 SDK 和信任和 SDK 厂商的产品您均可以使用。

Some third-party SDKs may have non-standard implementations. We recommend avoiding SDKs that are not strictly S3-compliant.
由于中国云厂商在很多地方进行了”魔改“。有很多最新的 S3 的技术不支持。因此,在全球很多对象存储的产品都不太推荐中国很多云厂商的 SDK。

## 2. Compatibility with MinIO SDKs

Yes, RustFS is fully compatible with MinIO SDKs.

If you are using MinIO SDKs, you can modify the Endpoint, AK, and SK to be directly compatible with RustFS.
## 2. MinIO 的 SDK 可以与 RustFS 直接通信吗?

## 3. Handling Incompatible SDKs
可以。

我们针对 MinIO 的 SDK 进行了全面的适配和兼容。

如果您正在使用 MinIO 的 SDK,可以修改 Endpoint 和 AK、SK 后直接兼容 RustFS。


## 3. 如果有其他不兼容的 SDK 怎么办?

我们使用某个云厂商的 SDK,他对于最新的 S3、MinIO 和 RustFS 均不支持应该怎么处理呢?
请您尽快更换 SDK,在业务端重新进行匹配和升级。

If you encounter an SDK that does not support standard S3, MinIO, or RustFS:

We recommend switching to a standard AWS S3 SDK.
4 changes: 4 additions & 0 deletions docs/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ export const sidebar = [
text: 'Java',
link: '/developer/sdk/java'
},
{
text: 'C#',
link: '/developer/sdk/csharp'
},
{
text: 'Python',
link: '/developer/sdk/python'
Expand Down