Class S3
- All Implemented Interfaces:
ObjectStorage
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 ClassesModifier and TypeClassDescriptionstatic final classOpenss3://andr2://storage URLs forObjectStorages; the generated wiring registers it under whichever of the two the configuration names.static final classWhatheadObject(String, String)reports; the provider-neutralObjectInfounder the name this class has always returned. -
Method Summary
Modifier and TypeMethodDescriptionvoidcreateBucket(String bucket) Creates a bucket, and says nothing when it already exists.voiddeleteObject(String bucket, String key) Deletes an object.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.getName()s3for AWS and for every S3-compatible endpoint, R2 included.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. -
getName
s3for AWS and for every S3-compatible endpoint, R2 included.- Specified by:
getNamein interfaceObjectStorage
-
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.
- Specified by:
createBucketin interfaceObjectStorage- 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.- Specified by:
putObjectin interfaceObjectStorage- Throws:
IOException
-
getObject
Downloads an object. Throws when it is missing, rather than returning null.- Specified by:
getObjectin interfaceObjectStorage- Throws:
IOException
-
headObject
The object's metadata, or null when it does not exist.- Specified by:
headObjectin interfaceObjectStorage- Throws:
IOException
-
deleteObject
Description copied from interface:ObjectStorageDeletes an object. Deleting one that is not there is not an error.- Specified by:
deleteObjectin interfaceObjectStorage- 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.- Specified by:
listObjectsin interfaceObjectStorage- 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.
- Specified by:
presignGetin interfaceObjectStorage- Throws:
IOException
-
presignPut
The upload counterpart: a URL a client can PUT to, forseconds.- Specified by:
presignPutin interfaceObjectStorage- Throws:
IOException
-