Interface ObjectStorage

All Known Implementing Classes:
AzureBlobStorage, GcsStorage, MemoryObjectStorage, S3

public interface ObjectStorage

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 Type
    Method
    Description
    void
    Creates a bucket, and says nothing when this account already owns it.
    void
    deleteObject(String bucket, String key)
    Deletes an object.
    The provider's short name: s3 (Amazon S3 and every S3-compatible endpoint, R2 included), gcs, azure or memory.
    byte[]
    getObject(String bucket, String key)
    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 to max keys under prefix (null or empty for all), following the provider's pagination until the listing ends or max is reached.
    presignGet(String bucket, String key, int seconds)
    A URL that downloads the object with no credentials, for seconds.
    presignPut(String bucket, String key, int seconds)
    The upload counterpart: a URL a client can PUT the bytes to, for seconds.
    putObject(String bucket, String key, byte[] content, String contentType)
    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, azure or memory.
    • createBucket

      void createBucket(String bucket) throws IOException

      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

      String putObject(String bucket, String key, byte[] content, String contentType) throws IOException

      Uploads an object, replacing one of the same key. Returns its ETag, the provider's receipt for exactly these bytes.

      contentType may be null, which stores application/octet-stream.

      Throws:
      IOException
    • getObject

      byte[] getObject(String bucket, String key) throws IOException
      Downloads an object. Throws when it is missing, rather than returning null.
      Throws:
      IOException
    • headObject

      ObjectInfo headObject(String bucket, String key) throws IOException
      The object's metadata, or null when it does not exist.
      Throws:
      IOException
    • deleteObject

      void deleteObject(String bucket, String key) throws IOException
      Deletes an object. Deleting one that is not there is not an error.
      Throws:
      IOException
    • listObjects

      List<String> listObjects(String bucket, String prefix, int max) throws IOException
      Up to max keys under prefix (null or empty for all), following the provider's pagination until the listing ends or max is reached. Zero answers an empty list; a negative max is refused.
      Throws:
      IOException
    • presignGet

      String presignGet(String bucket, String key, int seconds) throws IOException

      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

      String presignPut(String bucket, String key, int seconds) throws IOException

      The upload counterpart: a URL a client can PUT the bytes to, for seconds.

      Azure needs one header on that PUT that 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