Skip to content
Jennifer Programming Language

s3 - S3-compatible object storage

Import with import "s3.j" as s3;. An object-storage client for Amazon S3 and every S3-compatible store - the endpoint is configurable, so one module serves AWS S3, MinIO, Cloudflare R2, and Backblaze B2. Every request is signed with AWS Signature Version 4 (HMAC-SHA256 key-chaining), built on hash.hmac + hash.compute + encoding (hex) + time (the request timestamp) + http. Needs the default jennifer binary (net via http).

jennifer
use io;
use os;
import "s3.j" as s3;
import "http.j" as http;

def c as s3.Client init s3.connect(
    "https://s3.us-east-1.amazonaws.com", "us-east-1", os.getEnv("S3_KEY"), os.getEnv("S3_SECRET"));

def put as http.Response init s3.put($c, "mybucket", "hello.txt", "hi there");
def obj as http.Response init s3.get($c, "mybucket", "hello.txt");
io.printf("%d\n%s\n", $obj.status, $obj.body);

Runnable: examples/modules/s3_demo.j.

Client

s3.connect(endpoint, region, accessKey, secretKey) returns a value-semantic s3.Client. The endpoint is any S3-compatible base URL: http:// or https://, a host (a name, an IPv4 address or a bracketed IPv6 address such as [::1]) and an optional port. One trailing / is dropped; a path, query, fragment, userinfo, or a malformed host or port throws an S3Error (kind "s3"), since the signature covers the path and a base path would sign a different one than the one sent. Addressing is path-style ({endpoint}/{bucket}/{key}), which works uniformly across AWS and self-hosted stores.

Signed requests go over https://, or http:// to a loopback host (localhost, 127.0.0.0/8, [::1], such as a MinIO container on the same machine). Any other http:// endpoint throws an S3Error before the request is sent: SigV4 never sends the secret key, but the access key id and every object travel in the clear, and an on-path attacker can replay a captured signed PUT or DELETE within its 15-minute window or answer a GET with data of their own. For a store on a trusted LAN set $c.allowInsecureHttp = true;. presign sends nothing and is not checked.

Every request carries a timeout so a hung S3 endpoint fails instead of blocking (and exhausting a worker pool). connect defaults Client.timeoutMs to 30000 ms; set it to change the bound, or to 0 to disable it:

jennifer
def c as s3.Client init s3.connect($endpoint, $region, $key, $secret);
$c.timeoutMs = 5000;   # fail a request that stalls for 5 s
Storeendpointregion
AWS S3https://s3.<region>.amazonaws.comyour bucket's region
MinIOhttp://host:9000us-east-1 (or as configured)
Cloudflare R2https://<account>.r2.cloudflarestorage.comauto
Backblaze B2https://s3.<region>.backblazeb2.comyour bucket's region

Operations

Every call returns an http.Response (status / headers / body); reading it needs import "http.j". A non-2xx status is a value to branch on, not an error; s3.errorOf(resp) reads S3's XML error body into an s3.S3Error with the status, S3's code (NoSuchKey, AccessDenied, SlowDown, ...) and the requestId, to inspect or throw:

jennifer
def r as http.Response init s3.get($c, "photos", "cat.jpg");
if ($r.status != 200) {
    def e as s3.S3Error init s3.errorOf($r);
    if ($e.code == "NoSuchKey") {
        io.printf("no such photo\n");
    } else {
        throw $e;
    }
}

Every reply except an object download (get, getBytes) is capped at 16 MiB; a larger one throws Error{kind: "limit"}.

A call throws an S3Error (kind "s3", status 0) for a bucket name outside [A-Za-z0-9._-]{1,255} (or . / ..), since the name goes into the path unencoded, and for an empty key, which would address the bucket itself (DELETE /bucket/ deletes the bucket).

CallMethodNotes
s3.get(client, bucket, key)GETbody is the object contents (string); a missing object is a 404.
s3.getBytes(client, bucket, key)GETByte-safe download -> http.BytesResponse (body as bytes); use for binary objects.
s3.put(client, bucket, key, body)PUTUpload / overwrite from a string body; 200 on success.
s3.putBytes(client, bucket, key, data)PUTUpload from a raw bytes body (byte-for-byte).
s3.putWith(client, bucket, key, body, contentType, metadata)PUTUpload with a signed Content-Type + x-amz-meta-* metadata ({} = none).
s3.putBytesWith(client, bucket, key, data, contentType, metadata)PUTThe bytes-body counterpart to putWith.
s3.head(client, bucket, key)HEADObject metadata (status + headers, empty body); 404 when absent.
s3.copy(client, srcBucket, srcKey, dstBucket, dstKey)PUTServer-side copy via a signed x-amz-copy-source (no download / re-upload); an <Error> body under a 200 throws.
s3.errorOf(resp)-A failed response as an S3Error: status, S3's code and requestId.
s3.delete(client, bucket, key)DELETE204 on success.
s3.listObjects(client, bucket)GET ?list-type=2body is the ListObjectsV2 XML (first page, up to 1000 keys).
s3.listObjectsFrom(client, bucket, token)GET ?list-type=2&continuation-token=...Fetch a further page from a continuation token.
s3.objectKeys(xml)-Pull the <Key> values out of a list body -> list of string.
s3.isTruncated(xml)-true when the listing has more pages.
s3.nextContinuationToken(xml)-The token for the next page, or "" when complete.

(The list op is listObjects, not list, because list is a reserved type keyword.)

jennifer
# Page through every object (S3 caps a listing at 1000 keys per page).
def body as string init s3.listObjects($c, "mybucket").body;
def more as bool init true;
while ($more) {
    for (def k in s3.objectKeys($body)) {
        io.printf("%s\n", $k);
    }
    $more = s3.isTruncated($body);
    if ($more) {
        $body = s3.listObjectsFrom($c, "mybucket", s3.nextContinuationToken($body)).body;
    }
}

Presigned URLs

s3.presign(client, method, bucket, key, expiresSeconds) builds a URL that grants time-limited access to an object without exposing the secret key, using SigV4 query-signing (the X-Amz-* query parameters, UNSIGNED-PAYLOAD). The returned URL works from any HTTP client (a browser, curl) until it expires. This is pure string work - no request is sent. s3 itself imports http, so it does not load on jennifer-tiny; there, import s3presign instead, a subset of s3 (Client, connect, S3Error, presign) with the same names and the same signing code.

jennifer
# A link that lets anyone GET the object for one hour.
def url as string init s3.presign($c, "GET", "mybucket", "report.pdf", 3600);
# A PUT upload link (hand it to a browser to upload directly to S3).
def up as string init s3.presign($c, "PUT", "mybucket", "upload.bin", 900);

The validity is 1 to 604800 seconds (7 days); any other expiresSeconds throws an S3Error (kind "s3").

Multipart upload

For objects beyond the ~5 GB single-PUT limit (or to stream parts as they are produced), upload in parts:

jennifer
def uid as string init s3.createMultipartUpload($c, "mybucket", "big.iso", "application/octet-stream");
def etags as list of string init [];
$etags[] = s3.uploadPart($c, "mybucket", "big.iso", $uid, 1, $part1); # each part >= 5 MiB except the last
$etags[] = s3.uploadPart($c, "mybucket", "big.iso", $uid, 2, $part2);
def r as http.Response init s3.completeMultipartUpload($c, "mybucket", "big.iso", $uid, $etags);
# On any failure, discard the parts so they do not accrue storage cost:
#   s3.abortMultipartUpload($c, "mybucket", "big.iso", $uid);

uploadPart returns each part's ETag; pass them to completeMultipartUpload in part-number order (part 1 first). createMultipartUpload and uploadPart throw an S3Error for a non-2xx reply (and for one without the UploadId / ETag). S3 may answer completeMultipartUpload and copy with 200 and report a failure found afterwards in an <Error> body; both throw that as an S3Error with status 200 and S3's code.

Signing

Requests are signed with SigV4 for service s3: the canonical request covers the method, the URI-encoded path (object keys keep their /), the canonical query (parameters sorted by key), the signed headers, and the SHA-256 of the body; the string-to-sign is HMAC-chained through AWS4<secret> -> date -> region -> s3 -> aws4_request to the signature. The base signed set is host / x-amz-content-sha256 / x-amz-date; putWith / putBytesWith add content-type and each x-amz-meta-* header to the signed set, and copy adds x-amz-copy-source, all sorted in. The payload hash is the SHA-256 of the body (not UNSIGNED-PAYLOAD) for the request-signing path, so the whole request is integrity-covered; presigned URLs use UNSIGNED-PAYLOAD (the standard for query-signing). The signature is pinned in the tests against independent SigV4 implementations - the header-signing over the wire (TestS3Requests, which re-derives the signature from the actual signed-header set) and the presigned URL against a Python reference.

Scope

  • Path-style, SigV4, s3 service. Virtual-hosted addressing and the older SigV2 are out of scope.
  • String and byte bodies. put / get carry text; putBytes / getBytes (and putBytesWith) carry raw bytes for binary objects. A single body is held in memory, so objects beyond ~5 GB use the multipart path.
  • Object ops + multipart + presign. Get / put / delete / head / copy, content-type + metadata, multipart upload, and presigned URLs are covered. Bucket create / delete and ACL / policy management are not.
  • listObjects returns the raw XML (plus objectKeys); pagination is supported through isTruncated / nextContinuationToken / listObjectsFrom, but per-object metadata (size, etag, last-modified) is not parsed.

See also

Academy talks

TalkWhat it shows
Storing data in the cloud: S3, WebDAV, Koofr and Seafileobjects, metadata, presigned URLs and multipart uploads

API reference

Every exported function, struct and constant of s3, with its signature, generated from the module's doc comments: the s3 API reference.