XU XUv0.16.0

#github.com/xulang-org/packages/s3

导入方式: s3 #github.com/xulang-org/packages/s3

公开 API: 7 函数, 21 方法, 3 类型, 0 常量, 0 方法集

扩展包 `s3 #github.com/xulang-org/packages/s3`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

27_standalone_s3/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@main

s3 #github.com/xulang-org/packages/s3
#errors

fn verify_sigv4_calc() {
    println("--- Testing AWS SigV4 Canonical Calculation ---")
    method := "GET"
    path := "/test-bucket/test-file.txt"
    host := "127.0.0.1:9000"
    ak := "minioadmin"
    sk := "minioadmin"
    region := "us-east-1"
    amz_date := "20260923T120000Z"
    date_stamp := "20260923"
    payload_hash := s3.sha256_hex("")

    auth := s3.compute_auth_header(
        method,
        path,
        host,
        ak,
        sk,
        region,
        amz_date,
        date_stamp,
        payload_hash
    )
    println("Calculated Auth Header: " + auth)
    prefix := "AWS4-HMAC-SHA256 Credential=minioadmin/20260923/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature="
    if auth.len > prefix.len && auth[0:prefix.len] == prefix {
        println("[PASS] SigV4 format matches AWS specification!")
    } else {
        println("[FAIL] SigV4 format mismatch!")
    }
}

fn verify_s3_live() {
    println("\n--- Testing S3 / MinIO Live API ---")
    $client := s3.open("http://127.0.0.1:9000", "minioadmin", "minioadmin", "us-east-1")

    bucket := "xu-s3-test"
    println("1. Checking if bucket exists...")
    exists := client.bucket_exists(bucket + "")
    println("   Bucket exists initially: " + str(exists))

    if !exists {
        println("2. Creating bucket '" + bucket + "'...")
        client.make_bucket(bucket + "") >>> (err) {
            println("   [SKIP/FAIL] make_bucket error: " + err.msg())
            <<
        }
        println("   [PASS] Bucket created.")
    }

    println("3. Putting object 'greeting.txt'...")
    content := "Hello from Xu Language S3 Native Driver! 🚀"
    client.put_object(bucket + "", "greeting.txt", content + "", "text/plain") >>> (err) {
        println("   [FAIL] put_object error: " + err.msg())
        <<
    }
    println("   [PASS] Object put successfully.")

    println("4. Getting object 'greeting.txt'...")
    got := client.get_object(bucket + "", "greeting.txt") >>> (err) {
        println("   [FAIL] get_object error: " + err.msg())
        <<
    }
    println("   Retrieved content: " + got)
    if got == content {
        println("   [PASS] Object content matches exactly!")
    } else {
        println("   [FAIL] Content mismatch!")
    }

    println("5. Removing object 'greeting.txt'...")
    client.remove_object(bucket + "", "greeting.txt") >>> (err) {
        println("   [FAIL] remove_object error: " + err.msg())
        <<
    }
    println("   [PASS] Object removed.")

    println("6. Removing bucket '" + bucket + "'...")
    client.remove_bucket(bucket + "") >>> (err) {
        println("   [FAIL] remove_bucket error: " + err.msg())
        <<
    }
    println("   [PASS] Bucket removed.")
}

fn verify_s3_pool() {
    println("\n--- 3. Testing S3 Connection Pool ---")
    $p := s3.open_pool("http://127.0.0.1:9000", "minioadmin", "minioadmin", "us-east-1", 5)

    // 1. Connection Reuse Test
    fd1 := p.get_conn()
    println("   Borrow 1 fd: " + str(fd1))
    p.put_conn(fd1)

    fd2 := p.get_conn()
    println("   Borrow 2 (after return) fd: " + str(fd2))
    if fd1 == fd2 {
        println("   [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
    } else {
        println("   [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
    }

    // 2. Multi-connection concurrent borrow
    fd3 := p.get_conn()
    println("   Borrow 3 (concurrently) fd: " + str(fd3))
    if fd2 != fd3 {
        println("   [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
    } else {
        println("   [FAIL] Expected distinct fds for concurrent connections!")
    }
    p.put_conn(fd2)
    p.put_conn(fd3)

    // 3. Delegated operations via pool
    println("   Testing delegated operations via pool...")
    pool_bucket := "xu-pool-bucket"
    if !p.bucket_exists(pool_bucket + "") {
        p.make_bucket(pool_bucket + "") >>> (err) {
            println("   [FAIL] Pool make_bucket error: " + err.msg())
            <<
        }
    }
    println("   [PASS] Pool bucket created.")

    pool_content := "Pooled S3 payload data 999"
    p.put_object(pool_bucket + "", "pool_obj.txt", pool_content + "", "text/plain") >>> (err) {
        println("   [FAIL] Pool put_object error: " + err.msg())
        <<
    }
    println("   [PASS] Pool object put.")

    got := p.get_object(pool_bucket + "", "pool_obj.txt") >>> (err) {
        println("   [FAIL] Pool get_object error: " + err.msg())
        <<
    }
    println("   Retrieved via pool: " + got)
    if got == pool_content {
        println("   [PASS] Pool object content matches exactly!")
    } else {
        println("   [FAIL] Content mismatch!")
    }

    p.remove_object(pool_bucket + "", "pool_obj.txt") >>> (err) {
        println("   [FAIL] Pool remove_object error: " + err.msg())
        <<
    }
    p.remove_bucket(pool_bucket + "") >>> (err) {
        println("   [FAIL] Pool remove_bucket error: " + err.msg())
        <<
    }
    println("   [PASS] Pool cleanup completed.")

    p.close()
    println("   [PASS] S3 Pool closed cleanly.")
}

fn main() {
    println("=== Xu S3 / MinIO Driver Verification ===")
    verify_sigv4_calc()
    verify_s3_live()
    verify_s3_pool()
    println("\n[ALL S3 VERIFICATION COMPLETED]")
}

API 声明

open

XuAPI
pub fn open(endpoint string, access_key string, secret_key string, region string) Client

用途

打开资源或建立网络连接。

参数

endpoint string
endpoint(string)
access_key string
access_key(string)
secret_key string
secret_key(string)
region string
region(string)

返回值

Client — 该类型的值

查看用法示例

open_pool

XuAPI
pub fn open_pool(endpoint string, access_key string, secret_key string, region string, max_idle int) Pool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
access_key string
access_key(string)
secret_key string
secret_key(string)
region string
region(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

($&Client) make_bucket

XuAPI
pub fn (c $&Client) make_bucket(bucket string) int

用途

$&Client 上的操作:执行 make bucket 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bucket string
bucket(string)

返回值

int — 该类型的值

($&Client) bucket_exists

XuAPI
pub fn (c $&Client) bucket_exists(bucket string) bool

用途

$&Client 上的操作:执行 bucket exists 操作。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bucket string
bucket(string)

返回值

bool — 是否成立

($&Client) remove_bucket

XuAPI
pub fn (c $&Client) remove_bucket(bucket string) int

用途

$&Client 上的操作:执行 remove bucket 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bucket string
bucket(string)

返回值

int — 该类型的值

struct Response

XuAPI
pub struct Response {
    pub status_code int
    pub body string
}

用途

S3 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

status_code int
字段(int)
body string
正文

struct Client

XuAPI
pub struct Client {
    pub host string
    pub port int
    pub secure bool
    pub access_key string
    pub secret_key string
    pub region string
}

用途

S3 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
access_key string
字段(string)
secret_key string
字段(string)
region string
字段(string)

new_client

XuAPI
pub fn new_client(endpoint string, access_key string, secret_key string, region string) Client

用途

构造 client。

参数

endpoint string
endpoint(string)
access_key string
access_key(string)
secret_key string
secret_key(string)
region string
region(string)

返回值

Client — 该类型的值

($&Client) request

XuAPI
pub fn (c $&Client) request(method string, path string, body string, content_type string) Response

用途

$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
method string
method(string)
path string
文件系统路径(string)
body string
正文(string)
content_type string
content_type(string)

返回值

Response — 该类型的值

($&Client) put_object

XuAPI
pub fn (c $&Client) put_object(bucket string, key string, data string, content_type string) int

用途

$&Client 上的操作:执行 put object 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)
data string
数据(string)
content_type string
content_type(string)

返回值

int — 该类型的值

($&Client) get_object

XuAPI
pub fn (c $&Client) get_object(bucket string, key string) string

用途

$&Client 上的操作:读取 object。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)

返回值

string — 结果字符串

($&Client) remove_object

XuAPI
pub fn (c $&Client) remove_object(bucket string, key string) int

用途

$&Client 上的操作:执行 remove object 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)

返回值

int — 该类型的值

($&Client) object_exists

XuAPI
pub fn (c $&Client) object_exists(bucket string, key string) bool

用途

$&Client 上的操作:执行 object exists 操作。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)

返回值

bool — 是否成立

struct Pool

XuAPI
pub struct Pool {
    pub host string
    pub port int
    pub secure bool
    pub endpoint string
    pub access_key string
    pub secret_key string
    pub region string
    pub state chan pool.State
    pub max_idle int
}

用途

S3 与 MinIO 兼容对象存储连接池管理器。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
endpoint string
字段(string)
access_key string
字段(string)
secret_key string
字段(string)
region string
字段(string)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(endpoint string, access_key string, secret_key string, region string, max_idle int) Pool

用途

构造 pool。

参数

endpoint string
endpoint(string)
access_key string
access_key(string)
secret_key string
secret_key(string)
region string
region(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

用途

在 &Pool 上执行 get 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Client — 该类型的值

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

用途

创建并返回持有独立状态或底层的副本对象。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Pool — 该类型的值

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) discard_conn

XuAPI
pub fn (p &Pool) discard_conn(fd int)

用途

&Pool 上的操作:丢弃连接。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) make_bucket

XuAPI
pub fn (p &Pool) make_bucket(bucket string) int

用途

&Pool 上的操作:执行 make bucket 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
bucket string
bucket(string)

返回值

int — 该类型的值

(&Pool) bucket_exists

XuAPI
pub fn (p &Pool) bucket_exists(bucket string) bool

用途

&Pool 上的操作:执行 bucket exists 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
bucket string
bucket(string)

返回值

bool — 是否成立

(&Pool) remove_bucket

XuAPI
pub fn (p &Pool) remove_bucket(bucket string) int

用途

&Pool 上的操作:执行 remove bucket 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
bucket string
bucket(string)

返回值

int — 该类型的值

(&Pool) put_object

XuAPI
pub fn (p &Pool) put_object(bucket string, key string, data string, content_type string) int

用途

&Pool 上的操作:执行 put object 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)
data string
数据(string)
content_type string
content_type(string)

返回值

int — 该类型的值

(&Pool) get_object

XuAPI
pub fn (p &Pool) get_object(bucket string, key string) string

用途

&Pool 上的操作:读取 object。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)

返回值

string — 结果字符串

(&Pool) remove_object

XuAPI
pub fn (p &Pool) remove_object(bucket string, key string) int

用途

&Pool 上的操作:执行 remove object 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)

返回值

int — 该类型的值

(&Pool) object_exists

XuAPI
pub fn (p &Pool) object_exists(bucket string, key string) bool

用途

&Pool 上的操作:执行 object exists 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
bucket string
bucket(string)
key string
键(string)

返回值

bool — 是否成立

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

sha256_hex

XuAPI
pub fn sha256_hex(data string) string

用途

SHA-256十六进制。

参数

data string
数据(string)

返回值

string — 结果字符串

查看用法示例

hmac_hex

XuAPI
pub fn hmac_hex(key string, data string) string

用途

执行 hmac hex 操作。

参数

key string
键(string)
data string
数据(string)

返回值

string — 结果字符串

compute_auth_header

XuAPI
pub fn compute_auth_header( method string, path string, host string, access_key string, secret_key string, region string, amz_date string, date_stamp string, payload_hash string ) string

用途

执行 compute auth header 操作。

参数

method string
method(string)
path string
文件系统路径(string)
host string
主机(string)
access_key string
access_key(string)
secret_key string
secret_key(string)
region string
region(string)
amz_date string
amz_date(string)
date_stamp string
date_stamp(string)
payload_hash string
payload_hash(string)

返回值

string — 结果字符串

查看用法示例