algoflows/zig-s3
A simple and efficient Zig native S3 client library, supporting AWS S3 and S3-compatible services like MINIO.
refs
This project is currently under construction and subject to changes.
A simple and efficient S3 client library for Zig, supporting AWS S3 and S3-compatible services.
Add the package to your build.zig.zon
:
.{
.name = "your-project",
.version = "0.1.0",
.dependencies = .{
.s3 = .{
.url = "https://github.com/algoflows/zig-s3/archive/v0.2.0.tar.gz",
// Don't forget to update hash after publishing
.hash = "...",
},
},
}
Then in your build.zig
:
const s3_dep = b.dependency("s3", .{
.target = target,
.optimize = optimize,
});
exe.addModule("s3", s3_dep.module("s3"));
const std = @import("std");
const s3 = @import("s3");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Initialize client
var client = try s3.S3Client.init(allocator, .{
.access_key_id = "your-key",
.secret_access_key = "your-secret",
.region = "us-east-1",
// Optional: Use with MinIO or other S3-compatible services
// .endpoint = "http://localhost:9000",
});
defer client.deinit();
// Create bucket
try client.createBucket("my-bucket");
// Upload string content
var uploader = client.uploader();
try uploader.uploadString("my-bucket", "hello.txt", "Hello, S3!");
}
The main client interface for S3 operations.
const client = try s3.S3Client.init(allocator, .{
.access_key_id = "your-key",
.secret_access_key = "your-secret",
.region = "us-east-1",
.endpoint = "http://localhost:9000", // Optional, for S3-compatible services
});
createBucket(bucket_name: []const u8) !void
deleteBucket(bucket_name: []const u8) !void
listBuckets() ![]BucketInfo
putObject(bucket_name: []const u8, key: []const u8, data: []const u8) !void
getObject(bucket_name: []const u8, key: []const u8) ![]const u8
deleteObject(bucket_name: []const u8, key: []const u8) !void
listObjects(bucket_name: []const u8, options: ListObjectsOptions) ![]ObjectInfo
A helper for uploading different types of content:
var uploader = client.uploader();
// Upload string content
try uploader.uploadString("my-bucket", "hello.txt", "Hello, World!");
// Upload JSON data
const data = .{ .name = "example", .value = 42 };
try uploader.uploadJson("my-bucket", "data.json", data);
// Upload file from filesystem
try uploader.uploadFile("my-bucket", "image.jpg", "path/to/local/image.jpg");
The library uses Zig's error union type for robust error handling:
Error Type | Description |
---|---|
S3Error.InvalidCredentials |
Invalid AWS credentials |
S3Error.BucketNotFound |
Requested bucket doesn't exist |
S3Error.ObjectNotFound |
Requested object doesn't exist |
S3Error.ConnectionFailed |
Network or connection issues |
S3Error.InvalidResponse |
Unexpected response from S3 service |
Run the unit test suite:
zig build test
Integration tests require a running MinIO instance:
docker run -p 9000:9000 minio/minio server /data
zig build integration-test
See tests/integration/README.md
for detailed information about the integration
tests.
git checkout -b feature/amazing-feature
)MIT License - see LICENSE file for details.