BBackend CourseLeroy Labs
0/0
Search the course

File storage, covering DB blobs, Cloudinary signed uploads, S3 + CloudFront

Where files should live, direct-to-storage uploads with signatures, presigned URLs, CDNs and signed cookies for private media, transcoding, and migrating providers safely.

● advancedHostel-Web-AppReport-Manager-2IR3D (Kontentt)SperoTek (in dev)

Where can files live?

Option Pros Cons
DB bytea Transactional with your data, one backup Bloats DB, backups and the connection pool; slow to stream
Local disk Simple Lost when the container restarts; doesn’t scale out
Object storage (S3, R2, GCS, Cloudinary) Cheap, durable (11 nines), CDN-friendly Another service; eventual consistency for listings

Upload patterns

1. Through your server (simple)

browser --multipart--> API --stream--> S3

Your API pays the bandwidth and memory, so cap the size (spring.servlet.multipart.max-file-size: 25MB, as in IR3D).

2. Direct to storage with a signature (scalable)

1. browser -> API: "I want to upload an avatar"
2. API -> browser: { signature, timestamp, apiKey, folder }   (or an S3 presigned PUT URL)
3. browser --file--> Cloudinary/S3 directly
4. browser -> API: "done, here is the public_id/key"  -> API validates & saves the reference

The API never touches the bytes, and the signature limits where and for how long the client may upload.

// S3 presigned PUT (AWS SDK v2)
PutObjectPresignRequest req = PutObjectPresignRequest.builder()
    .signatureDuration(Duration.ofMinutes(5))
    .putObjectRequest(b -> b.bucket(bucket).key("uploads/" + userId + "/" + UUID.randomUUID())
                            .contentType("image/jpeg"))
    .build();
URL url = presigner.presignPutObject(req).url();

Private media delivery

Public URLs are fine for hostel photos. For paid or private content, use one of:

  • Presigned GET URLs, which expire.
  • CloudFront signed URLs or cookies, which grant access to a whole path for a time window, which suits video segments (HLS).

In your projects

Hostel-Web-Appinfrastructure/cloudinary/CloudinarySignatureService · ImageUpload.tsx

Direct signed uploads:

String signature = cloudinary.apiSignRequest(paramsToSign, apiSecret);
return new CloudinarySignatureDto(signature, timestamp, apiKey, folder);

The React ImageUpload component uploads straight to Cloudinary, and the API stores the resulting URL. Cloudinary also resizes and serves images through its CDN.

IR3D (Kontentt)MediaStorageService · CloudinaryStorageService · S3StorageService · MediaAccessService · CloudinaryToS3Migration · VoiceNoteTranscoder
  • A pluggable provider (app.storage.provider = cloudinary | s3) via @ConditionalOnProperty.
  • S3 behind a CloudFront CDN with invalidation on overwrite, plus signed upload params.
  • CloudFront signed cookies for private media (app.media.signed-access.enabled).
  • CloudinaryToS3Migration, a one-off migration with a dry-run mode, which is the right way to move millions of files.
  • Voice notes are transcoded with ffmpeg (VoiceNoteTranscoder) into a consistent format and size.
Report-Manager-2export/domain/ExportJob.java (byte[] content)

Generated Excel and PDF exports are stored in the DB (private byte[] content, contentType) with 60-day cleanup. That’s reasonable for a modest number of files: transactional, no S3 needed, and purged regularly.

SperoTek (in dev)reporting/domain/ReportExport.java

The same idea: exports are generated asynchronously into byte[] content, downloaded via /exports/{id}/download, and cleaned up hourly.

  • Validate uploads. Check the size, the real content type from magic bytes (not the extension), and image dimensions. Strip EXIF GPS data from photos.
  • Never use user filenames as keys. Generate keys like uploads/(userId)/(uuid).jpg.
  • Virus scanning for documents (ClamAV, S3 malware-scanning services).
  • Lifecycle rules. S3 can auto-expire temporary uploads or move them to cheaper tiers.
  • Cloudflare R2 has zero egress fees and is S3-compatible, which makes it a strong alternative to S3 + CloudFront.

Check yourself

1. Main benefit of signed direct uploads?
2. Private video segments for paying users on a CDN → use…