s3 API reference
An S3-compatible object-storage client: get / put / delete objects and list a bucket, signing every request with AWS Signature Version 4. The endpoint is configurable, so one module serves AWS S3 and every S3-compatible store (MinIO, Cloudflare R2, Backblaze B2) - a selectable backend, not a module per vendor. Path-style addressing ({endpoint}/{bucket}/{key}). SigV4 is HMAC-SHA256 key-chaining, so this builds on hash.hmac + hash.compute + encoding (hex) + time (the request timestamp) + http. Needs the default jennifer binary (net via http).
The object verbs return the http.Response: a 4xx / 5xx is a value to branch on, and errorOf reads S3's XML error body (<Code>NoSuchKey</Code>, the message and request id) into an S3Error, the struct the module throws. Every reply but an object download (get / getBytes) is capped at 16 MiB (Error{kind: "limit"} past it). An endpoint other than https:// (or http:// to a loopback host) throws an S3Error before a signed request is sent, unless the client sets allowInsecureHttp.
Import with import "s3.j" as s3;. See the s3 guide for prose and examples.
Contents
| Name | What it is |
|---|---|
abortMultipartUpload | Abort a multipart upload, discarding its uploaded parts (call this on failure so the parts do not linger an... |
completeMultipartUpload | Complete a multipart upload, assembling the uploaded parts into the final object. |
connect | Build a client for an S3 endpoint. |
copy | Copy an object server-side (no download / re-upload): PUT the destination with an x-amz-copy-source of... |
createMultipartUpload | Begin a multipart upload (for objects beyond the ~5 GB single-PUT limit, or to stream parts). |
delete | DELETE an object. |
errorOf | Read a failed response into an S3Error: its status and, from S3's XML error body, the Code, Message a... |
get | GET an object. |
getBytes | GET an object as raw bytes (the byte-safe download). |
head | HEAD an object: its metadata (status + headers) without the body. |
isTruncated | Report whether a ListObjectsV2 XML body was truncated (more pages remain). |
listObjects | List a bucket's objects (S3 ListObjectsV2). |
listObjectsFrom | Fetch one further page of a ListObjectsV2 listing, starting after token (the value from... |
nextContinuationToken | Extract the continuation token to fetch the next page, or "" when none. |
objectKeys | Extract the object keys from a ListObjectsV2 XML body (the <Key> elements). |
presign | Build a presigned URL that grants time-limited access to an object without exposing the secret key, using S... |
put | PUT (upload / overwrite) an object with the given body. |
putBytes | PUT (upload / overwrite) an object from a raw bytes body, written byte-for-byte so a binary payload round... |
putBytesWith | PUT an object (raw bytes body) with an explicit content type and user metadata - the binary counterpart t... |
putWith | PUT an object (string body) with an explicit content type and user metadata. |
uploadPart | Upload one part of a multipart upload (each part >= 5 MiB except the last). |
Client | struct: A configured S3 client: the endpoint (scheme + host, no trailing slash), the signing region, and the access... |
S3Error | struct: A failed S3 request, as errorOf reads it and the module throws it. |
Functions
abortMultipartUpload
s3.abortMultipartUpload(
client as Client,
bucketName as string,
key as string,
uploadId as string)Abort a multipart upload, discarding its uploaded parts (call this on failure so the parts do not linger and accrue storage cost).
client- the client
bucketName- the bucket
key- the object key
uploadId- the id from
createMultipartUpload - Returns
http.Response - the response (204 on success)
completeMultipartUpload
s3.completeMultipartUpload(
client as Client,
bucketName as string,
key as string,
uploadId as string,
etags as list of string)Complete a multipart upload, assembling the uploaded parts into the final object. etags are the ETags from uploadPart in part-number order (part 1 first).
client- the client
bucketName- the bucket
key- the object key
uploadId- the id from
createMultipartUpload etags- the part ETags, in part-number order
- Returns
http.Response - the response (200 with a CompleteMultipartUploadResult body)
- Throws
S3Error - kind "s3" for an
<Error>body under a 200 (a failure after S3 answered)
connect
s3.connect(
endpoint as string,
region as string,
accessKey as string,
secretKey as string)Build a client for an S3 endpoint. The endpoint is any S3-compatible base URL, http:// or https:// plus a host (a name, an IPv4 address or a bracketed IPv6 address) and an optional port; one trailing / is dropped. A path, a query, a fragment, userinfo or a malformed host or port throws.
endpoint- the base URL
region- the signing region
accessKey- the access key id
secretKey- the secret access key
- Returns
Client - a configured client (30 s timeout; set
.timeoutMsto change it) - Throws
S3Error - kind "s3" for a malformed endpoint
copy
s3.copy(
client as Client,
srcBucket as string,
srcKey as string,
dstBucket as string,
dstKey as string)Copy an object server-side (no download / re-upload): PUT the destination with an x-amz-copy-source of /{srcBucket}/{srcKey} (signed). Source and destination may share a bucket (a rename when paired with delete).
client- the client
srcBucket- the source bucket
srcKey- the source object key
dstBucket- the destination bucket
dstKey- the destination object key
- Returns
http.Response - the response (200 with a CopyObjectResult body on success)
- Throws
S3Error - kind "s3" for an
<Error>body under a 200 (a copy that failed after S3 answered)
createMultipartUpload
s3.createMultipartUpload(
client as Client,
bucketName as string,
key as string,
contentType as string)Begin a multipart upload (for objects beyond the ~5 GB single-PUT limit, or to stream parts). Returns the upload id to pass to uploadPart / completeMultipartUpload / abortMultipartUpload.
client- the client
bucketName- the bucket
key- the object key
contentType- the object content type ("" = default)
- Returns
string - the upload id
- Throws
S3Error - kind "s3" when S3 refuses the request (its
Codeand status) or the response carries no UploadId
delete
s3.delete(client as Client, bucketName as string, key as string)DELETE an object.
client- the client
bucketName- the bucket
key- the object key
- Returns
http.Response - the response (204 on success)
errorOf
s3.errorOf(resp as http.Response)Read a failed response into an S3Error: its status and, from S3's XML error body, the Code, Message and RequestId. A HEAD or a non-XML body gives the status alone.
resp- a response, typically with a 4xx / 5xx status
- Returns
S3Error - the error, to inspect or
throw
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;
}
}get
s3.get(client as Client, bucketName as string, key as string)GET an object. The response body is the object's contents; a missing object comes back as a 404 http.Response, not an error.
client- the client
bucketName- the bucket
key- the object key
- Returns
http.Response - the response (body = object contents on 200)
getBytes
s3.getBytes(client as Client, bucketName as string, key as string)GET an object as raw bytes (the byte-safe download). Use this for binary objects (images, archives) that a UTF-8 string body cannot hold; get stays the text convenience.
client- the client
bucketName- the bucket
key- the object key
- Returns
http.BytesResponse - the response (body = raw object bytes on 200)
head
s3.head(client as Client, bucketName as string, key as string)HEAD an object: its metadata (status + headers) without the body. A present object returns 200 with Content-Length / Content-Type / x-amz-meta-* headers; a missing one returns 404.
client- the client
bucketName- the bucket
key- the object key
- Returns
http.Response - the response (headers only, empty body)
isTruncated
s3.isTruncated(xml as string)Report whether a ListObjectsV2 XML body was truncated (more pages remain).
xml- the body from a list call
- Returns
bool - true when another page is available
listObjects
s3.listObjects(client as Client, bucketName as string)List a bucket's objects (S3 ListObjectsV2). The response body is the S3 XML listing; pass it to s3.objectKeys to pull out the keys. S3 returns at most 1000 keys per page: check s3.isTruncated on the body and, if true, call s3.listObjectsFrom with s3.nextContinuationToken to fetch the next page (loop until not truncated).
client- the client
bucketName- the bucket
- Returns
http.Response - the response (body = ListBucketResult XML on 200)
listObjectsFrom
s3.listObjectsFrom(client as Client, bucketName as string, token as string)Fetch one further page of a ListObjectsV2 listing, starting after token (the value from s3.nextContinuationToken on the previous page's body).
client- the client
bucketName- the bucket
token- the continuation token from the previous page
- Returns
http.Response - the response (body = ListBucketResult XML on 200)
nextContinuationToken
s3.nextContinuationToken(xml as string)Extract the continuation token to fetch the next page, or "" when none.
xml- the body from a list call
- Returns
string - the next continuation token, or "" if the listing is complete
objectKeys
s3.objectKeys(xml as string)Extract the object keys from a ListObjectsV2 XML body (the <Key> elements).
xml- the body from
s3.listObjects - Returns
list of string - the object keys, in listing order
presign
s3.presign(
client as Client,
method as string,
bucketName as string,
key as string,
expiresSeconds as int)Build a presigned URL that grants time-limited access to an object without exposing the secret key, using SigV4 query-signing (X-Amz-* query parameters). The URL works from any HTTP client until it expires. No request is sent; on jennifer-tiny, where this module does not load (it imports http), the s3presign module has the same presign.
client- the client
method- the HTTP method the URL authorizes (e.g. "GET", "PUT")
bucketName- the bucket
key- the object key
expiresSeconds- the validity window in seconds (1 to 604800 = 7 days)
- Returns
string - the presigned URL
- Throws
S3Error - kind "s3" for an expiry outside 1..604800, a bad bucket name or an empty key
put
s3.put(
client as Client,
bucketName as string,
key as string,
body as string)PUT (upload / overwrite) an object with the given body.
client- the client
bucketName- the bucket
key- the object key
body- the object contents
- Returns
http.Response - the response (200 on success)
putBytes
s3.putBytes(
client as Client,
bucketName as string,
key as string,
data as bytes)PUT (upload / overwrite) an object from a raw bytes body, written byte-for-byte so a binary payload round-trips intact.
client- the client
bucketName- the bucket
key- the object key
data- the object contents
- Returns
http.Response - the response (200 on success)
putBytesWith
s3.putBytesWith(
client as Client,
bucketName as string,
key as string,
data as bytes,
contentType as string,
metadata as map of string to string)PUT an object (raw bytes body) with an explicit content type and user metadata - the binary counterpart to putWith.
client- the client
bucketName- the bucket
key- the object key
data- the object contents
contentType- the object content type ("" = default)
metadata- user metadata (stored as x-amz-meta-<key>)
- Returns
http.Response - the response (200 on success)
putWith
s3.putWith(
client as Client,
bucketName as string,
key as string,
body as string,
contentType as string,
metadata as map of string to string)PUT an object (string body) with an explicit content type and user metadata. The Content-Type and each x-amz-meta-<key> header are part of the signature, so they reach S3 intact. Pass "" for the default content type and {} for no metadata.
client- the client
bucketName- the bucket
key- the object key
body- the object contents
contentType- the object content type (e.g. "text/html"; "" = default)
metadata- user metadata (stored as x-amz-meta-<key>)
- Returns
http.Response - the response (200 on success)
uploadPart
s3.uploadPart(
client as Client,
bucketName as string,
key as string,
uploadId as string,
partNumber as int,
data as bytes)Upload one part of a multipart upload (each part >= 5 MiB except the last). Returns the part's ETag, which completeMultipartUpload needs (collect them in part-number order).
client- the client
bucketName- the bucket
key- the object key
uploadId- the id from
createMultipartUpload partNumber- the 1-based part number
data- the part contents
- Returns
string - the part's ETag
- Throws
S3Error - kind "s3" when S3 refuses the part (
Code, status) or sends no ETag
Structs
Client
A configured S3 client: the endpoint (scheme + host, no trailing slash), the signing region, and the access-key pair. Value-semantic; build with connect.
def struct Client {
endpoint as string,
region as string,
accessKey as string,
secretKey as string,
timeoutMs as int,
allowInsecureHttp as bool
};endpoint- e.g. "https://s3.us-east-1.amazonaws.com" or "http://localhost:9000"
region- the signing region, e.g. "us-east-1"
accessKey- the access key id
secretKey- the secret access key
timeoutMs- per-request idle timeout in milliseconds (0 disables it);
connectdefaults it to 30000 allowInsecureHttp- allow plain
http://to a non-loopback host (default false)
S3Error
A failed S3 request, as errorOf reads it and the module throws it. Every error of kind "s3" is one, so its fields are safe to read once the kind matches.
def struct S3Error {
kind as string,
message as string,
status as int,
code as string,
requestId as string
};kind- always
"s3" messages3: HTTP <status>: <Code>: <Message>, or what went wrongstatus- the response's HTTP status; 0 for a local check
code- S3's error
Code(NoSuchKey,NoSuchBucket,AccessDenied,SignatureDoesNotMatch,SlowDown, ...); "" when the body had none requestId- S3's
RequestId, for the provider's support; "" when none