Interface ObjectStorage
- All Known Implementing Classes:
AzureBlobStorage, GcsStorage, MemoryObjectStorage, S3
A bucket-and-key object store: Amazon S3 and everything that speaks its API (Cloudflare R2, MinIO, Backblaze B2), Google Cloud Storage, Azure Blob Storage, or the in-memory store a test runs against.
Inject it into a bean, or ask ObjectStorages.current(), and the provider is
the one cn1.storage.url names, so the same code moves between clouds by
changing one line of configuration:
@Service
public class Avatars {
private final ObjectStorage storage;
public Avatars(ObjectStorage storage) {
this.storage = storage;
}
public String uploadUrl(String user) throws IOException {
return storage.presignPut("avatars", user + ".png", 600);
}
}
A "bucket" is what the provider calls its top-level container: an S3 or GCS bucket, an Azure container. A key is the object's name inside it; a slash in a key is part of the name, which every provider's console shows as folders.
The presigned URL is the method to reach for from a mobile app: the device uploads or downloads directly, and the object bytes never pass through the server.
-
Method Summary
Modifier and TypeMethodDescriptionvoidcreateBucket(String bucket) Creates a bucket, and says nothing when this account already owns it.voiddeleteObject(String bucket, String key) Deletes an object.getName()The provider's short name:s3(Amazon S3 and every S3-compatible endpoint, R2 included),gcs,azureormemory.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) Up tomaxkeys underprefix(null or empty for all), following the provider's pagination until the listing ends ormaxis reached.presignGet(String bucket, String key, int seconds) A URL that downloads the object with no credentials, forseconds.presignPut(String bucket, String key, int seconds) The upload counterpart: a URL a client canPUTthe bytes to, forseconds.Uploads an object, replacing one of the same key.
-
Method Details
-
getName
String getName()The provider's short name:s3(Amazon S3 and every S3-compatible endpoint, R2 included),gcs,azureormemory. -
createBucket
Creates a bucket, and says nothing when this account already owns it.
Usually infrastructure's job, but a first run against an emulator or a test fixture needs it.
- Throws:
IOException
-
putObject
Uploads an object, replacing one of the same key. Returns its ETag, the provider's receipt for exactly these bytes.
contentTypemay be null, which storesapplication/octet-stream.- 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
Deletes an object. Deleting one that is not there is not an error.- Throws:
IOException
-
listObjects
Up tomaxkeys underprefix(null or empty for all), following the provider's pagination until the listing ends ormaxis reached. Zero answers an empty list; a negativemaxis refused.- Throws:
IOException
-
presignGet
A URL that downloads the object with no credentials, for
seconds.Computed, not requested, wherever the provider allows it: the URL can be handed straight to a client. A lifetime the provider refuses -- zero or less, or past seven days on S3, R2 and GCS -- throws an IOException on every provider, so moving between them does not change how it fails.
- Throws:
IOException
-
presignPut
The upload counterpart: a URL a client can
PUTthe bytes to, forseconds.Azure needs one header on that
PUTthat the others do not:x-ms-blob-type: BlockBlob. A client that serves more than one provider can send it everywhere; S3 and GCS ignore it.- Throws:
IOException
-