<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aishani Singh on Medium]]></title>
        <description><![CDATA[Stories by Aishani Singh on Medium]]></description>
        <link>https://medium.com/@singhaishani2003?source=rss-6e36b3a4d516------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*jkpGijHH6n37qtKJ</url>
            <title>Stories by Aishani Singh on Medium</title>
            <link>https://medium.com/@singhaishani2003?source=rss-6e36b3a4d516------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 07:43:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@singhaishani2003/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Optimizing Amazon S3 Costs: From a Monolithic Upload Folder to a Policy-Driven Storage Architecture]]></title>
            <link>https://medium.com/@singhaishani2003/optimizing-amazon-s3-costs-from-a-monolithic-upload-folder-to-a-policy-driven-storage-architecture-a36e3beb22eb?source=rss-6e36b3a4d516------2</link>
            <guid isPermaLink="false">https://medium.com/p/a36e3beb22eb</guid>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[storage]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Aishani Singh]]></dc:creator>
            <pubDate>Sun, 09 Aug 2026 05:28:01 GMT</pubDate>
            <atom:updated>2026-08-09T10:10:43.527Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A practical guide to S3 prefixes, object tagging, lifecycle policies, storage classes, file validation, compression, data isolation, and migrating an existing production bucket.</em></p><p>Amazon S3 looks deceptively simple. You upload an object, get a URL, store that URL in your database, and you’re done.</p><p>At a small scale, this works perfectly well. But as a SaaS platform grows — especially a multi-tenant or white-label platform — that same approach becomes expensive, hard to manage, hard to clean up, and increasingly hard to secure.</p><p>This article walks through how to evolve from a <strong>monolithic S3 upload structure</strong> into an organized, cost-aware, policy-driven architecture.</p><p>The examples below use generic entities on purpose:</p><ul><li>X → tenant</li><li>Y → sub-tenant</li></ul><p>The goal isn’t to describe one company’s implementation — it’s to explain a reusable pattern you can adapt to any SaaS system storing user-generated content.</p><h3>1. The Original Problem</h3><p>Picture a white-label SaaS platform used by thousands of businesses, where different businesses use the same underlying product under their own brand. Users upload profile pictures, documents, images, attachments, marketing assets, communication media, and other customer-generated content.</p><p>Originally, the S3 structure looked roughly like this:</p><pre>bucket/uploads/<br>  2026-01-01-user1.png<br>  2026-01-01-user2.jpg<br>  2026-01-02-document.pdf<br>  2026-01-03-whatsapp.jpg<br>  2026-01-03-instagram.png<br>  ...</pre><p>Everything lived in one large logical namespace. At first this doesn’t look like a problem — S3 can obviously handle millions of objects. The real question isn’t <em>“can S3 store this much?”</em> It’s:</p><blockquote><strong><em>Can the application efficiently manage, classify, retain, delete, secure, and optimize millions of objects?</em></strong></blockquote><p>That’s a completely different question.</p><h3>2. Why the Flat Structure Broke Down</h3><p>There was no meaningful separation between agencies, resellers, and customers — everything sat under the same broad uploads/ prefix. That made it hard to reason about ownership, retention, deletion, auditing, migration, or any tenant-specific operation.</p><h3>3. The GDPR / Orphaned-Object Problem</h3><p>Suppose the database stores:</p><pre>profile_picture_url = https://bucket.s3.amazonaws.com/uploads/abc.png</pre><p>The user uploads a new picture, and the database is updated to point at new-image.png. What happens to abc.png? The database no longer references it — but S3 still holds it. That&#39;s an <strong>orphaned object</strong>.</p><p>Repeated over millions of users, this quietly inflates storage costs and, more importantly, means the organization keeps data it no longer has a reason to retain — a real problem once privacy and retention requirements enter the picture.</p><h3>4. The Real Realization</h3><p>The problem was never <em>“our bucket is too large.”</em> It was:</p><blockquote><strong><em>We didn’t have enough metadata and structure to make intelligent decisions about our objects.</em></strong></blockquote><p>An object shouldn’t just be image.png. The system should know who owns it, what type of data it is, which channel produced it, its retention period, whether it&#39;s temporary, and whether it can move to cheaper storage. That realization leads directly to the new architecture.</p><h3>5. The New Architecture, in One Sentence</h3><pre>Prefix + Tags + Lifecycle Policies<br>   +<br>Application-level validation + Compression + Controlled deletion + Security controls</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*odbCg_rd50XsRgtyDo8JlA.png" /></figure><h3>6. Designing an S3 Prefix</h3><p>The first improvement is meaningful prefixes. Instead of uploads/file.png, use something like:</p><pre>tenant/&lt;blinded-tenant-id&gt;/sub-tenant/&lt;blinded-sub-tenant-id&gt;/profile/image.webp</pre><p>The exact structure depends on the application, but the principle is constant:</p><blockquote><strong><em>The prefix should represent logical ownership and object purpose — not just the fact that something was uploaded.</em></strong></blockquote><p>A typical tree:</p><pre>tenant/X/sub-tenant/Y/<br>  profile/<br>  documents/<br>  conversations/<br>  temporary/</pre><h3>7. Prefixes Are Organization, Not Security</h3><p>This is an important distinction. tenant/X/ and tenant/Y/ existing as separate prefixes does <strong>not</strong>, by itself, mean tenant X can&#39;t read tenant Y&#39;s data. Access control still needs IAM policies, bucket policies, application-level authorization, presigned URLs, and backend validation. <strong>Prefixes give you organization. The authorization layer gives you security.</strong></p><h3>8. Don’t Expose Internal IDs in the Path</h3><p>If the original path is tenant/&lt;mongodb-id&gt;/sub-tenant/&lt;sub-tenant-id&gt;/image.png, you&#39;re leaking an internal database identifier into every object URL. Instead, transform the ID into a blinded representation — conceptually:</p><pre>Internal ID → HMAC(secret, ID) → blinded identifier → S3 prefix</pre><p>So /customer/6659abc123... becomes something like /customer/8a71f.... The path no longer reveals the application&#39;s internal identifier.</p><p><strong>Note:</strong> HMAC is not encryption — it’s a keyed construction that produces a deterministic value that can’t practically be reproduced without the secret key. The secret itself must stay outside the object path and be managed securely.</p><h3>9. Introducing Object Tags</h3><p>Prefixes solve organization. <strong>Tags solve classification.</strong> An object can carry tags like:</p><pre>channel=whatsapp<br>object_type=profile_image<br>retention=temporary<br>tenant_type=reseller</pre><p>Now every object carries both <em>where it belongs</em> and <em>what it represents</em>.</p><h3>10. Why Tags Are Powerful</h3><p>Say you have 10 million objects, and you decide temporary media should only live for 30 days. Without classification, finding those objects is an application-level nightmare. With tags, lifecycle rules can act on them directly:</p><pre>IF retention = temporary  → delete after 30 days<br>IF storage_policy = archive → transition to cheaper storage</pre><p>This is the point where S3 stops being a file bucket and becomes a <strong>policy-driven storage system</strong>.</p><h3>11. S3 Lifecycle Policies</h3><p>Instead of application code that constantly asks <em>“is this file old? should I delete it? archive it? move it?”</em>, lifecycle rules let S3 enforce predefined policy:</p><pre>Standard Storage (0 days) → Infrequent Access (30 days) → Archive (90 days) → Delete (retention expires)</pre><p>Actual periods should come from your data-retention requirements, not from a generic template.</p><h3>12. S3 Storage Classes at a Glance</h3><ul><li><strong>S3 Standard</strong> — frequently accessed data, low latency, unpredictable but generally high access. The default starting point.</li><li><strong>S3 Intelligent-Tiering</strong> — for data whose access pattern is unpredictable; S3 automatically moves objects between tiers based on usage.</li><li><strong>S3 Standard-IA</strong> (“Infrequent Access”) — accessed less often but still needs reasonably fast retrieval (older documents, historical reports). Carries retrieval charges, so don’t apply it blindly.</li><li><strong>S3 One Zone-IA</strong> — cheaper than Standard-IA but stored in a single Availability Zone; only for data that can tolerate that resilience trade-off.</li><li><strong>S3 Glacier Instant Retrieval</strong> — rarely accessed, but needs to come back quickly when it is.</li><li><strong>S3 Glacier Flexible Retrieval</strong> — long-term archives, historical data, compliance retention, where slower retrieval is acceptable.</li><li><strong>S3 Glacier Deep Archive</strong> — very long-term, “I almost never need this but must retain it,” very low cost, not built for normal application traffic.</li><li><strong>S3 Express One Zone</strong> — built for very high request performance and low latency in a single AZ. It’s a distinct workload-optimized class, not another rung on the Standard → IA → Glacier ladder.</li></ul><h3>13. The Storage-Class Decision</h3><p>Conceptually, access frequency decreases as you move down this ladder:</p><pre>S3 Standard → Intelligent-Tiering → Standard-IA → One Zone-IA<br>   → Glacier Instant Retrieval → Glacier Flexible Retrieval → Glacier Deep Archive → Delete</pre><p>This is a conceptual spectrum, not a mandatory path — you don’t have to walk every object through every class. The right class depends on access frequency, retrieval latency needs, durability requirements, retention rules, retrieval costs, object size, and business importance.</p><h3>14. Tags + Lifecycle = Automated Cost Optimization</h3><p>This is where the architecture pays off. Tag objects retention=temporary and a lifecycle rule deletes them after N days automatically. Tag them retention=archive and a rule transitions them to Glacier. Tag them access_pattern=infrequent and a rule moves them to IA. No nightly cron job required — S3 enforces the policy itself.</p><h3>15. The Profile Picture Replacement Problem</h3><p>A user uploads profile-v1.png. Later they upload profile-v2.png. The database now points at v2 — but v1 is still sitting in S3, forgotten. That&#39;s an orphaned object.</p><p>One fix is straightforward application logic: update the database, then delete the previous object. But here’s an important engineering principle:</p><blockquote><strong><em>Don’t introduce an event-driven architecture just because it sounds sophisticated.</em></strong><em> If a simple, reliable, understandable mechanism is sufficient, prefer it. Queues, workers, and event systems can introduce more failure modes than they solve.</em></blockquote><p>The right design depends on upload volume, consistency requirements, failure tolerance, and whether deletion needs to be synchronous.</p><p>What if the database update succeeds but the S3 delete fails? That’s not catastrophic — the database now correctly points at the new object, and a periodic cleanup job can find objects that are no longer referenced and remove them. That’s often more resilient than assuming every delete call will always succeed.</p><h3>16. The Frontend Placeholder Trap</h3><p>If the UI shows a placeholder image before a real upload happens, and the upload flow mistakenly treats that placeholder as real user content, you end up storing objects nobody actually uploaded. The rule: <strong>placeholder ≠ user upload</strong>, and that guard belongs at the application layer, before anything reaches S3.</p><h3>17. Validate and Rate-Limit Before Storage Ever Happens</h3><p>Storage optimization starts before the object reaches S3:</p><ul><li><strong>Size validation</strong> — reject files over your limit (e.g. 30 MB) before they enter the pipeline.</li><li><strong>Rate limiting</strong> — enforce this at the reverse-proxy layer (e.g. NGINX) so an attacker can’t generate massive upload traffic just by hammering the endpoint. Neither of these is an S3 feature — they’re part of the upload architecture <em>around</em> S3.</li></ul><h3>18. Never Trust the File Extension</h3><p>A naive check like filename.endsWith(&quot;.png&quot;) proves nothing about the actual content — someone can rename malicious.exe to image.png and sail past that check. A file-type detection library that inspects the binary signature of the file (rather than the filename) catches this:</p><pre>image.png → read file signature → detect actual type<br>  → application/x-executable → REJECT</pre><h3>19. The Full Validation Pipeline</h3><p>Putting it together: rate limiting → size check → file-type detection → (reject or continue) → compression → upload → prefix + tags assigned → lifecycle rules take over. That’s a very different — and far more robust — system than a raw frontend → S3 upload.</p><h3>20. Compression Is a Real Lever, With Trade-Offs</h3><p>A 6 MB photo compressed to 1.5 MB is a 4x reduction. At 1 million images, that’s the difference between 6 TB and 1.5 TB of storage. Actual savings depend on original size, algorithm, format, and quality settings.</p><p>A useful mental model is how messaging apps treat media: a normal photo gets compressed/resized on the assumption that users trade a small quality loss for faster uploads, faster downloads, and lower storage. But content explicitly uploaded <em>as a document</em> should usually keep its original fidelity — a legal document shouldn’t be silently degraded.</p><blockquote><strong><em>Compression should be driven by object purpose, not blindly applied to every upload.</em></strong></blockquote><p>Profile pictures can typically take aggressive optimization; legal documents often need to preserve the original; high-resolution design assets may not tolerate compression at all. Define compression policy per object type, not globally.</p><h3>21. The Three-Layer Model</h3><p>The whole architecture collapses into three layers, and this is the easiest way to remember it:</p><p><strong>Layer 1 — Prefix (Structure).</strong> Answers: <em>where does this object belong?</em> Example: tenant/X/sub-tenant/Y/profile/</p><p><strong>Layer 2 — Tags (Classification).</strong> Answers: <em>what kind of object is this?</em> Example: channel=whatsapp, object_type=media, retention=temporary</p><p><strong>Layer 3 — Lifecycle Policy (Automation).</strong> Answers: <em>what should happen to it over time?</em> Example: temporary → delete, archive → Glacier, infrequent → IA</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WtQjfP55JCHfFRpgPCvlRQ.png" /></figure><h3>22. Cost Optimization Is Bigger Than “Move Everything to Glacier”</h3><p>That’s the reflexive answer people give — and it’s incomplete. S3 costs come from storage, requests, data retrieval, data transfer, processing, replication, incomplete multipart uploads, and plain unnecessary objects. So:</p><pre>Cost Optimization =<br>  Storage optimization + Access-pattern optimization + Object-size optimization<br>  + Lifecycle management + Deletion + Upload controls</pre><p>The cheapest storage class isn’t automatically the cheapest architecture.</p><h3>23. Migrating an Existing Production Bucket</h3><p>Designing the new structure is the easy part. Migrating live data — where the database already holds URLs pointing at old objects — is the hard part. A safe, <strong>database-aware</strong> migration follows this order:</p><pre>Read existing S3 URL → identify ownership → determine new prefix → generate new key<br>  → copy/move object → update database reference → verify → remove old object</pre><p>The order matters: never delete the old object before confirming the new one exists and the database correctly points to it.</p><p>The migration should also be <strong>idempotent</strong> — safe to re-run without creating duplicates, because at scale jobs fail, servers restart, requests time out, and workers crash. Before deleting anything, verify: old object exists → new object created → new object accessible → database updated → reference verified → <em>then</em> delete the old object. For high-value data, add object-size comparison, content checksums, and metadata verification to that chain.</p><h3>24. Security Is a Layered System, Not a Single Trick</h3><p>“We hid the ID” is not a security model — blinding identifiers is just one piece. A robust upload architecture layers network controls (rate limiting), application controls (validation, size limits, authorization), and storage controls (IAM, bucket policy, encryption, access control), backed by ongoing VAPT, dependency scanning, monitoring, and logging.</p><p>For an upload system specifically, worthwhile things to test: can users bypass the size limit? Can a renamed executable get past file-type validation? Can tenant X reach tenant Y’s objects? Can someone manipulate object keys directly? Can presigned URLs be obtained or reused by someone unauthorized? Can the rate limit be bypassed? Can dangerous content reach downstream processing? The goal is making sure the architecture can’t be defeated just by manipulating the client request.</p><h3>25. Before vs. After</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BRdCJv7_d5u72UGVeEbFsw.png" /></figure><p><strong>Before:</strong> a flat uploads/ prefix with no tenant separation, no classification, orphaned objects accumulating, no retention control, and internal IDs sometimes exposed in the path.</p><p><strong>After:</strong> tenant-scoped, blinded prefixes; tags for channel/type/retention; lifecycle rules driving retain/transition/delete automatically; compression and size controls applied per object type; access governed by the application and IAM together.</p><p>Four questions now have clear answers for every object:</p><ul><li><strong>Where?</strong> → Prefix</li><li><strong>What?</strong> → Tags</li><li><strong>How long?</strong> → Lifecycle</li><li><strong>How much?</strong> → Compression / size controls</li><li><strong>Who can access it?</strong> → Application + IAM/access controls</li></ul><h3>26. Key Lessons</h3><ol><li><strong>Don’t treat S3 like a filesystem.</strong> Prefixes provide organization, not traditional folders — design object keys intentionally.</li><li><strong>Store metadata that enables decisions.</strong> If you don’t know what an object represents, you can’t automate what happens to it.</li><li><strong>Lifecycle policies should reflect business requirements</strong> — access frequency, retrieval needs, retention, compliance, and business importance, not a generic template.</li><li><strong>Delete what you don’t need.</strong> Unreferenced objects become orphans; cleanup is part of the architecture, not an afterthought.</li><li><strong>Validate before uploading</strong> — size, file type, authorization, rate, and business rules, before the object ever reaches S3.</li><li><strong>Never trust the extension alone</strong> — inspect actual file content.</li><li><strong>Compression can produce enormous savings</strong>, but it should respect the purpose of the object.</li><li><strong>Don’t over-engineer deletion.</strong> Start with the simplest reliable solution; add queues, workers, or events only when scale and reliability actually demand it.</li><li><strong>Migration is part of architecture.</strong> A new storage structure is only half the problem — existing objects and database references need a safe migration path.</li></ol><h3>Conclusion</h3><p>The biggest lesson here isn’t about finding one cheaper AWS service. It’s about <strong>designing the system so that unnecessary work and unnecessary data don’t exist in the first place</strong>.</p><p>A flat uploads/ directory works fine when a product has a few thousand files. Once the same platform serves thousands of tenants, resellers, and customers, storage stops being a simple bucket and becomes a lifecycle problem. Combine well-designed prefixes, blinded identifiers, object tags, lifecycle policies, appropriate storage classes, upload validation, rate limiting, compression, orphan cleanup, database-aware migration, and layered security controls, and S3 turns from a file dump into a structured, policy-driven, cost-aware storage layer.</p><p>The goal was never just <em>“store files in S3 cheaply.”</em> It’s to know <strong>what every object is, who it belongs to, how long it should exist, how often it’s likely to be accessed, and what should happen to it throughout its lifecycle.</strong></p><p>That’s the difference between <em>using</em> S3 and <em>engineering</em> an S3 storage architecture.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a36e3beb22eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>