XU XUv0.16.0

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

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

counts: 7 functions, 21 methods, 3 types, 0 constants, 0 method sets

S3 & MinIO Compatible Object Storage Driver for Xu Features Standard AWS Signature Version 4 (SigV4) calculation with pure Xu crypto primitives. Works with AWS S3, MinIO, Ceph, Cloudflare R2, and other S3-compatible object storages. Full bucket lifecycle: make_bucket, bucket_exists, remove_bucket. Full object lifecycle: put_object, get_object, remove_object, object_exists.

github.com/xulang-org/packages · 1.17.2 · Install and use

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

27_standalone_s3/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@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 declarations

open

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

Purpose

Open resource or establish connection.

Parameters

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

Returns

Client — a value of this type

See usage example

open_pool

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

Purpose

Open pool.

Parameters

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)

Returns

Pool — a value of this type

See usage example

($&Client) make_bucket

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

Purpose

Operation on $&Client: make bucket. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
bucket string
bucket (string)

Returns

int — a value of this type

($&Client) bucket_exists

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

Purpose

Operation on $&Client: bucket exists.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
bucket string
bucket (string)

Returns

bool — true or false

($&Client) remove_bucket

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

Purpose

Operation on $&Client: remove bucket. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
bucket string
bucket (string)

Returns

int — a value of this type

struct Response

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

Purpose

S3 Server response payload containing status, headers, and body.

Public fields

status_code int
field (int)
body string
body

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
}

Purpose

S3 Client session maintaining connection and operations.

Public fields

host string
host
port int
port
secure bool
field (bool)
access_key string
field (string)
secret_key string
field (string)
region string
field (string)

new_client

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

Purpose

Construct client.

Parameters

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

Returns

Client — a value of this type

($&Client) request

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

Purpose

Execute request on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
method string
method (string)
path string
filesystem path (string)
body string
body (string)
content_type string
content_type (string)

Returns

Response — a value of this type

($&Client) put_object

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

Purpose

Operation on $&Client: put object. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
bucket string
bucket (string)
key string
key (string)
data string
data (string)
content_type string
content_type (string)

Returns

int — a value of this type

($&Client) get_object

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

Purpose

Operation on $&Client: read object. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
bucket string
bucket (string)
key string
key (string)

Returns

string — result string

($&Client) remove_object

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

Purpose

Operation on $&Client: remove object. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
bucket string
bucket (string)
key string
key (string)

Returns

int — a value of this type

($&Client) object_exists

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

Purpose

Operation on $&Client: object exists.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
bucket string
bucket (string)
key string
key (string)

Returns

bool — true or false

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
}

Purpose

Connection Pool for S3 & MinIO Compatible Object Storage. Manages reusable TCP/HTTP socket connections.

Public fields

host string
host
port int
port
secure bool
field (bool)
endpoint string
field (string)
access_key string
field (string)
secret_key string
field (string)
region string
field (string)
state chan pool.State
field (channel; type chan pool.State)
max_idle int
field (int)

new_pool

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

Purpose

Construct pool.

Parameters

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)

Returns

Pool — a value of this type

(&Pool) get

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

Purpose

Execute get on &Pool.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

Client — a value of this type

(&Pool) dup

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

Purpose

Shares sockets and close state; clones configuration strings.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

Pool — a value of this type

(&Pool) get_conn

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

Purpose

Operation on &Pool: read conn.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

int — a value of this type

(&Pool) put_conn

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

Purpose

Operation on &Pool: put connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) discard_conn

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

Purpose

Operation on &Pool: discard connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) make_bucket

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

Purpose

Operation on &Pool: make bucket. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
bucket string
bucket (string)

Returns

int — a value of this type

(&Pool) bucket_exists

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

Purpose

Operation on &Pool: bucket exists.

Parameters

p &Pool
The Pool instance used by this call (borrow).
bucket string
bucket (string)

Returns

bool — true or false

(&Pool) remove_bucket

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

Purpose

Operation on &Pool: remove bucket. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
bucket string
bucket (string)

Returns

int — a value of this type

(&Pool) put_object

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

Purpose

Operation on &Pool: put object. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
bucket string
bucket (string)
key string
key (string)
data string
data (string)
content_type string
content_type (string)

Returns

int — a value of this type

(&Pool) get_object

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

Purpose

Operation on &Pool: read object. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
bucket string
bucket (string)
key string
key (string)

Returns

string — result string

(&Pool) remove_object

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

Purpose

Operation on &Pool: remove object. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
bucket string
bucket (string)
key string
key (string)

Returns

int — a value of this type

(&Pool) object_exists

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

Purpose

Operation on &Pool: object exists.

Parameters

p &Pool
The Pool instance used by this call (borrow).
bucket string
bucket (string)
key string
key (string)

Returns

bool — true or false

(&Pool) close

XuAPI
pub fn (p &Pool) close()

Purpose

Operation on &Pool: close the resource.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

This call does not return a value.

sha256_hex

XuAPI
pub fn sha256_hex(data string) string

Purpose

Sha256 hex.

Parameters

data string
data (string)

Returns

string — result string

See usage example

hmac_hex

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

Purpose

Hmac hex.

Parameters

key string
key (string)
data string
data (string)

Returns

string — result 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

Purpose

Compute auth header.

Parameters

method string
method (string)
path string
filesystem path (string)
host string
host (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)

Returns

string — result string

See usage example