Monday, June 30, 2025

Build a Dropbox-like Cloud File Storage with Azure

 

☁ Build a Dropbox-like Cloud File Storage with Azure

๐Ÿ“ฆ Store files → ๐Ÿ”„ Sync across devices → ๐Ÿค Collaborate — without managing servers.


✅ Phase 1: Setup Azure Resources

1. Create Azure Storage Account

  • Go to Azure Portal → Create Resource → Storage Account

  • Name: dropboxclone

  • Region: closest to users

  • Redundancy: LRS (Locally Redundant)

  • Enable Static Website (under Data management)

๐Ÿ“ Why: Blob Storage will store and serve files; Static Website hosts your frontend.


2. Create Blob Container

  • Go to Storage Account → Containers → + Container

  • Name: user-files

  • Access Level: Blob (anonymous read for public files)

๐Ÿ“ Why: Files uploaded here become publicly downloadable via URL.


๐Ÿ–ผ Phase 2: Build Frontend (HTML + JavaScript)

HTML (index.html)

<!DOCTYPE html>
<html>
<head>
    <title>Dropbox Clone</title>
    <!-- Include Azure Storage Blob SDK -->
    <script src="https://cdn.jsdelivr.net/npm/@azure/storage-blob@12.8.0/dist/azure-storage-blob.min.js"></script>
</head>
<body>
    <h1>My Dropbox Clone</h1>
    <input type="file" id="fileInput" />
    <button onclick="uploadFile()">Upload</button>
    <h3>Files:</h3>
    <div id="fileList"></div>
    <script src="script.js"></script>
</body>
</html>


๐Ÿ“ Why: Simple upload form + file list, no backend needed.


JavaScript (script.js)

Replace "YOUR_SAS_TOKEN" with your generated SAS token:


const account = "dropboxclone";

const sas = "YOUR_SAS_TOKEN";  // create in Azure → Shared access signature

const container = "user-files";


async function uploadFile() {

    const file = document.getElementById('fileInput').files[0];

    const blobService = new AzureStorage.Blob.BlobServiceClient(

        `https://${account}.blob.core.windows.net?${sas}`

    );

    const client = blobService.getContainerClient(container);

    await client.uploadBlockBlob(file.name, file, file.size);

    alert("Uploaded!");

    listFiles();

}


async function listFiles() {

    const blobService = new AzureStorage.Blob.BlobServiceClient(

        `https://${account}.blob.core.windows.net?${sas}`

    );

    const client = blobService.getContainerClient(container);

    let html = "";

    for await (const blob of client.listBlobsFlat()) {

        html += `<a href="https://${account}.blob.core.windows.net/${container}/${blob.name}" download>${blob.name}</a><br>`;

    }

    document.getElementById("fileList").innerHTML = html;

} 

๐Ÿ“ Why: Upload and instantly list files from blob storage.


๐ŸŒ Phase 3: Deploy frontend to Azure Static Website

  • Go to Storage Account → Static Website → Upload index.html and script.js

  • Copy Primary endpoint URL (e.g., https://dropboxclone.z22.web.core.windows.net)

๐Ÿ“ Why: Accessible from any browser/device.


๐Ÿ”„ Phase 4: Enable Cross-Device Sync (Serverless)

Use Azure Functions:

  1. Create Function App → Runtime: Node.js

  2. Add HTTP Trigger Function sync.js:

module.exports = async function (context, req) {
    const fileUrl = req.body.fileUrl;
    context.bindings.outputBlob = fileUrl;
    return { body: "Synced to devices!" };
};


  1. Configure output binding to blob storage.

๐Ÿ“ Why: Automatically sync uploaded files info across devices.


Call sync from frontend:

async function syncFile(fileUrl) {
    await fetch("https://your-function.azurewebsites.net/api/sync", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ fileUrl })
    });
}



๐Ÿค Phase 5: Collaboration Features

✅ Share files

  • Generate SAS tokens in Azure → Shared Access Signature.

  • Share links like:

https://dropboxclone.blob.core.windows.net/user-files/report.docx?sp=r&st=2023-10-01T00:00:00Z&se=2023-10-31T00:00:00Z&spr=https&sig=XXX

๐Ÿ“ Why: Share read/write access securely without exposing storage keys.


๐Ÿ“ก Real-time updates

  • Use Azure Event Grid to detect blob changes.

  • Setup webhook → frontend refreshes file list.

๐Ÿ“ Why: Like Dropbox desktop app auto-refresh.


๐Ÿงช Phase 6: Test the system

WhatHow
UploadOpen static site, upload file
Cross-device syncOpen same URL on phone; file shows
CollaborationShare SAS link; teammate downloads or edits

๐Ÿงฉ Key Concepts

FeatureAzureDropbox Equivalent
File storageBlob StorageCloud drive
Cross-device syncAzure FunctionsDesktop sync
Share linksSAS TokensShared links
WebsiteStatic sitedropbox.com

๐Ÿงฐ Troubleshooting

IssueFix
CORS errorsStorage Account → CORS → Allow * origins
Expired SASGenerate new token
Function errorAzure Portal → Function → Monitor

๐Ÿš€ Why this approach works

✅ Blob Storage: scalable & cost-effective
✅ SAS: secure, limited sharing
✅ Functions: no servers to manage
✅ Static website: low-cost global frontend

No comments:

Post a Comment