Class S3
Amazon S3, and anything that speaks its API (MinIO, Cloudflare R2, Backblaze B2, Wasabi, Ceph) -- which is why the endpoint is configurable rather than assembled from a region alone.
Two addressing styles exist and the choice is not cosmetic: virtual-hosted
(bucket.s3.region.amazonaws.com) is what AWS requires for new buckets, and
path-style (endpoint/bucket/key) is what a local MinIO or a bucket whose name
is not DNS-safe needs. Both are supported because a backend is usually
developed against the second and deployed against the first.
The presigned URL is the method to reach for from a mobile app: it lets the device upload or download directly and keeps the object bytes out of the server, which is most of the reason to use object storage from an app.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidcreateBucket(String bucket) Creates a bucket, and says nothing when it already exists.voiddeleteObject(String bucket, String key) static S3forEndpoint(Credentials credentials, String region, String endpoint) An S3-compatible endpoint -- MinIO, R2, Ceph -- addressed path-style.static S3AWS S3 in one region, with credentials resolved the usual way.byte[]Downloads an object.headObject(String bucket, String key) The object's metadata, or null when it does not exist.listObjects(String bucket, String prefix, int max) Lists up tomaxobjects under a prefix.presignGet(String bucket, String key, int seconds) A URL that downloads the object without any credentials, forseconds.presignPut(String bucket, String key, int seconds) The upload counterpart: a URL a client can PUT to, forseconds.Uploads an object.
-
Method Details
-
forRegion
AWS S3 in one region, with credentials resolved the usual way.
The region is taken from AWS_REGION when not given, because that is what Lambda and ECS set and hard-coding it is how a service ends up deployable in exactly one place.
- Throws:
IOException
-
forEndpoint
An S3-compatible endpoint -- MinIO, R2, Ceph -- addressed path-style.
endpointis a host with an optional port and an optional scheme: "minio.internal", "localhost:9000", "http://localhost:9000". TLS is assumed unless the endpoint says http:// -- a default of "encrypted" is the one that fails safely. -
createBucket
Creates a bucket, and says nothing when it already exists.
Usually infrastructure's job rather than the application's, but a first run against a fresh MinIO or a test fixture needs it, and the alternative is a shell script that speaks a protocol this class already speaks.
- Throws:
IOException
-
putObject
public String putObject(String bucket, String key, byte[] content, String contentType) throws IOException Uploads an object. Returns its ETag, which is the server's receipt.- Throws:
IOException
-
getObject
Downloads an object. Throws when it is missing, rather than returning null.- Throws:
IOException
-
headObject
The object's metadata, or null when it does not exist.- Throws:
IOException
-
deleteObject
- Throws:
IOException
-
listObjects
Lists up to
maxobjects under a prefix.ListObjectsV2, and paginated: S3 caps a page at 1000 keys whatever you ask for, and a caller that ignores the continuation token silently sees only the first page. This follows the token until the listing is complete or
maxis reached.- Throws:
IOException
-
presignGet
A URL that downloads the object without any credentials, for
seconds.Nothing is sent here: a presigned URL is a computation, so this costs no round trip and can be handed straight to a client.
- Throws:
IOException
-
presignPut
The upload counterpart: a URL a client can PUT to, forseconds.- Throws:
IOException
-