DEV Community

Cover image for Optimizing Large File Uploads: Secure Client-Side Multipart Uploads to AWS S3
Owen Roth
Owen Roth

Posted on

Optimizing Large File Uploads: Secure Client-Side Multipart Uploads to AWS S3

Uploading large files to the cloud can be challenging — network interruptions, browser limitations, and huge file sizes can easily disrupt the process. Amazon S3 (Simple Storage Service) is a scalable, high-speed, web-based cloud storage service designed for online backup and archiving of data and applications. However, uploading large files to S3 requires careful handling to ensure reliability and performance.

Enter AWS S3’s multipart upload: a powerful solution that breaks big files into smaller chunks, enabling faster, more reliable uploads by tackling each part independently and even uploading parts in parallel. This method not only overcomes file size limits (S3 requires multipart upload for files larger than 5GB) but also minimizes the risk of failure, making it a perfect fit for applications needing seamless, robust file uploads.

In this guide, we’ll unpack the ins and outs of client-side multipart uploads to S3, showing you why it’s the smart choice for handling large files, how to get it up and running securely, and what challenges to watch out for. I’ll provide step-by-step instructions, code examples, and best practices to help you implement a reliable client-side file upload solution.

Ready to upgrade your file upload experience? Let’s dive in!

Server vs. Client-Side Uploads

When designing a file upload system, you have two primary options: uploading files through your server (server-side) or uploading files directly from the client to S3 (client-side). Each approach has its pros and cons.

Server-Side Uploads

Server-Side Upload Infrastructure

Pros:

  • Enhanced Security: All uploads are managed by the server, keeping AWS credentials secure.

  • Better Error Handling: Servers can manage retries, logging, and error handling more robustly.

  • Centralized Processing: Files can be validated, processed, or converted on the server before storing in S3.

Cons:

  • Higher Server Load: Large uploads consume server resources (CPU, memory, bandwidth), which can impact performance and increase operational costs.

  • Potential Bottlenecks: The server can become a single point of failure or a performance bottleneck during high upload traffic, leading to slow uploads or downtime.

  • Increased Costs: Handling uploads server-side may require scaling your infrastructure to handle peak loads, raising operational expenses.

Client-Side Uploads

Client-Side Upload Infrastructure (Single / Multipart)

Pros:

  • Reduced Server Load: Files are sent directly from the user’s device to S3, freeing up server resources.

  • Improved Speed: Users experience faster uploads since they bypass the application server.

  • Cost Efficiency: Eliminates the need for server infrastructure to handle large uploads, potentially lowering costs.

  • Scalability: Ideal for scaling file uploads without stressing backend servers.

Cons:

  • Security Risks: Requires careful handling of AWS credentials and permissions. Presigned URLs must be securely generated to prevent unauthorized access.

  • Limited Control: Less server-side oversight over uploads; error handling and retries are often managed on the client.

  • Browser Constraints: Browsers have memory and API limitations, which can hinder handling of very large files or affect performance on lower-end devices.

Step-by-Step Guide to Implementing Secure Client-Side Uploads

Implementing client-side uploads securely involves coordinating between your frontend application and a secure backend service. The backend service’s primary role is to generate presigned URLs, allowing the client to upload files directly to S3 without exposing sensitive AWS credentials.

Prerequisites

  • AWS Account: Access to an AWS account with permissions to use S3.
  • AWS SDK Knowledge: Familiarity with the AWS SDK for JavaScript (v3) or making direct API calls to AWS services.
  • Frontend and Backend Development Skills: Understanding of both client-side (JavaScript, React, etc.) and server-side (Node.js, Express, etc.) programming.

1. Setting Up the Right Architecture

To implement client-side uploads effectively, you need:

  • Frontend Application: Handles file selection, splitting files into parts if necessary, and uploading parts to S3 using presigned URLs.
  • Backend Service: A secure server that provides APIs for generating presigned URLs and initializing or completing multipart uploads. It keeps your AWS credentials secure and enforces any necessary business logic or validation.

This architecture ensures that sensitive operations are handled securely on the backend, while the frontend manages the upload process.

2. Creating the Upload Service on the Backend

Why Use Presigned URLs?

Presigned URLs allow clients to interact with S3 directly, performing operations like uploading files without requiring AWS credentials on the client side. They are secure because:

  • They are time-limited and expire after a specified duration.
  • They can be restricted to specific operations (e.g., PUT for uploading).
  • They are specific to a particular S3 object key.

Implementing the S3UploadService

Create a service class on your server responsible for:

a. Defining the S3 bucket and region
b. Establishing AWS credentials securely.
c. Providing methods to generate presigned URLs and manage multipart uploads.

// services/S3UploadService.js

import {
  S3Client,
  CreateMultipartUploadCommand,
  CompleteMultipartUploadCommand,
  UploadPartCommand,
  AbortMultipartUploadCommand,
  PutObjectCommand,
  GetObjectCommand,
  DeleteObjectCommand,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

// Import credential providers
import {
  fromIni,
  fromInstanceMetadata,
  fromEnv,
  fromProcess,
} from '@aws-sdk/credential-providers';

export class S3UploadService {
  constructor() {
    this.s3BucketName = process.env.S3_BUCKET_NAME;
    this.s3Region = process.env.S3_REGION;

    this.s3Client = new S3Client({
      region: this.s3Region,
      credentials: this.getS3ClientCredentials(),
    });
  }

  // Method to generate AWS credentials securely
  getS3ClientCredentials() {
    if (process.env.NODE_ENV === 'development') {
      // In development, use credentials from environment variables
      return fromEnv();
    } else {
      // In production, use credentials from EC2 instance metadata or another secure method
      return fromInstanceMetadata();
    }
  }

  // Generate a presigned URL for single-part upload (PUT), download (GET), or deletion (DELETE)
  async generatePresignedUrl(key, operation) {
    let command;
    switch (operation) {
      case 'PUT':
        command = new PutObjectCommand({
          Bucket: this.s3BucketName,
          Key: key,
        });
        break;
      case 'GET':
        command = new GetObjectCommand({
          Bucket: this.s3BucketName,
          Key: key,
        });
        break;
      case 'DELETE':
        command = new DeleteObjectCommand({
          Bucket: this.s3BucketName,
          Key: key,
        });
        break;
      default:
        throw new Error(`Invalid operation "${operation}"`);
    }

    // Generate presigned URL
    return await getSignedUrl(this.s3Client, command, { expiresIn: 3600 }); // Expires in 1 hour
  }

  // Methods for multipart upload
  async createMultipartUpload(key) {
    const command = new CreateMultipartUploadCommand({
      Bucket: this.s3BucketName,
      Key: key,
    });
    const response = await this.s3Client.send(command);
    return response.UploadId;
  }

  async generateUploadPartUrl(key, uploadId, partNumber) {
    const command = new UploadPartCommand({
      Bucket: this.s3BucketName,
      Key: key,
      UploadId: uploadId,
      PartNumber: partNumber,
    });

    return await getSignedUrl(this.s3Client, command, { expiresIn: 3600 });
  }

  async completeMultipartUpload(key, uploadId, parts) {
    const command = new CompleteMultipartUploadCommand({
      Bucket: this.s3BucketName,
      Key: key,
      UploadId: uploadId,
      MultipartUpload: { Parts: parts },
    });
    return await this.s3Client.send(command);
  }

  async abortMultipartUpload(key, uploadId) {
    const command = new AbortMultipartUploadCommand({
      Bucket: this.s3BucketName,
      Key: key,
      UploadId: uploadId,
    });
    return await this.s3Client.send(command);
  }
}
Enter fullscreen mode Exit fullscreen mode

Note: Ensure that your AWS credentials are securely managed. In production, it’s recommended to use IAM roles attached to your EC2 instances or ECS tasks, rather than hardcoding credentials or using environment variables.

3. Implementing the Backend API Endpoints

Create API endpoints in your backend to handle requests from the frontend. These endpoints will utilize the S3UploadService to perform actions.

// controllers/S3UploadController.js

import { S3UploadService } from '../services/S3UploadService';

const s3UploadService = new S3UploadService();

export const generatePresignedUrl = async (req, res, next) => {
  try {
    const { key, operation } = req.body; // key is the S3 object key (file identifier)
    const url = await s3UploadService.generatePresignedUrl(key, operation);
    res.status(200).json({ url });
  } catch (error) {
    next(error);
  }
};

export const initializeMultipartUpload = async (req, res, next) => {
  try {
    const { key } = req.body;
    const uploadId = await s3UploadService.createMultipartUpload(key);
    res.status(200).json({ uploadId });
  } catch (error) {
    next(error);
  }
};

export const generateUploadPartUrls = async (req, res, next) => {
  try {
    const { key, uploadId, parts } = req.body; // parts is the number of parts
    const urls = await Promise.all(
      [...Array(parts).keys()].map(async (index) => {
        const partNumber = index + 1;
        const url = await s3UploadService.generateUploadPartUrl(key, uploadId, partNumber);
        return { partNumber, url };
      })
    );
    res.status(200).json({ urls });
  } catch (error) {
    next(error);
  }
};

export const completeMultipartUpload = async (req, res, next) => {
  try {
    const { key, uploadId, parts } = req.body; // parts is an array of { ETag, PartNumber }
    const result = await s3UploadService.completeMultipartUpload(key, uploadId, parts);
    res.status(200).json({ result });
  } catch (error) {
    next(error);
  }
};

export const abortMultipartUpload = async (req, res, next) => {
  try {
    const { key, uploadId } = req.body;
    await s3UploadService.abortMultipartUpload(key, uploadId);
    res.status(200).json({ message: 'Upload aborted' });
  } catch (error) {
    next(error);
  }
};
Enter fullscreen mode Exit fullscreen mode

Set up the routes for these endpoints in your Express app or whichever framework you’re using.

4. Implementing the Frontend Uploader Class

The frontend will handle selecting files, deciding whether to perform a single-part or multipart upload based on file size, and managing the upload process.

In general, AWS recommends "when your object size reaches 100 MB, you should consider using multipart uploads instead of uploading the object in a single operation." Source

// Uploader.js

import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';

// Helper functions to interact with your backend API
async function requestPresignedUrl(key, operation) {
  const response = await axios.post('/api/generate-presigned-url', { key, operation });
  return response.data.url;
}

async function initializeMultipartUpload(key) {
  const response = await axios.post('/api/initialize-multipart-upload', { key });
  return response.data.uploadId;
}

async function getUploadPartUrls(key, uploadId, parts) {
  const response = await axios.post('/api/generate-upload-part-urls', {
    key,
    uploadId,
    parts,
  });
  return response.data.urls; // Array of { partNumber, url }
}

async function completeMultipartUpload(key, uploadId, parts) {
  const response = await axios.post('/api/complete-multipart-upload', {
    key,
    uploadId,
    parts,
  });
  return response.data.result;
}

async function abortMultipartUpload(key, uploadId) {
  await axios.post('/api/abort-multipart-upload', { key, uploadId });
}

export class Uploader {
  constructor(file, onProgress) {
    this.file = file;
    this.onProgress = onProgress; // Callback function to report progress
    this.chunkSize = 5 * 1024 * 1024; // 5MB default chunk size
    this.fileKey = uuidv4(); // Unique file identifier for S3
    this.uploadId = null; // AWS multipart upload ID
  }

  async start() {
    if (this.file.size <= this.chunkSize) {
      await this.uploadSinglePart();
    } else {
      await this.uploadMultipart();
    }
  }

  async uploadSinglePart() {
    try {
      const url = await requestPresignedUrl(this.fileKey, 'PUT');
      await axios.put(url, this.file, {
        headers: {
          'Content-Type': this.file.type,
        },
        onUploadProgress: (progressEvent) => {
          if (this.onProgress) {
            const percentCompleted = Math.round(
              (progressEvent.loaded * 100) / progressEvent.total
            );
            this.onProgress(percentCompleted);
          }
        },
      });
      console.log('File uploaded successfully');
    } catch (error) {
      console.error('Error uploading file:', error);
    }
  }

  async uploadMultipart() {
    try {
      // Step 1: Initialize multipart upload
      this.uploadId = await initializeMultipartUpload(this.fileKey);

      // Step 2: Split the file into parts
      const parts = this.createFileParts();

      // Step 3: Get presigned URLs for each part
      const uploadPartUrls = await getUploadPartUrls(
        this.fileKey,
        this.uploadId,
        parts.length
      );

      // Step 4: Upload each part
      const uploadedParts = [];
      for (let i = 0; i < parts.length; i++) {
        const partNumber = i + 1;
        const { url } = uploadPartUrls.find((u) => u.partNumber === partNumber);
        const part = parts[i];

        const response = await axios.put(url, part, {
          headers: {
            'Content-Type': 'application/octet-stream',
          },
          onUploadProgress: (progressEvent) => {
            if (this.onProgress) {
              // Calculate overall progress
              const totalLoaded =
                parts
                  .slice(0, i)
                  .reduce((acc, p) => acc + p.size, 0) + progressEvent.loaded;
              const percentCompleted = Math.round(
                (totalLoaded * 100) / this.file.size
              );
              this.onProgress(percentCompleted);
            }
          },
        });

        // Collect ETag and PartNumber for completing upload
        uploadedParts.push({
          ETag: response.headers.etag.replace(/"/g, ''), // Remove quotes from ETag
          PartNumber: partNumber,
        });
      }

      // Step 5: Complete multipart upload
      await completeMultipartUpload(this.fileKey, this.uploadId, uploadedParts);
      console.log('File uploaded successfully');
    } catch (error) {
      console.error('Error uploading file:', error);
      if (this.uploadId) {
        await abortMultipartUpload(this.fileKey, this.uploadId);
      }
    }
  }

  createFileParts() {
    const parts = [];
    let start = 0;
    while (start < this.file.size) {
      const end = Math.min(start + this.chunkSize, this.file.size);
      parts.push(this.file.slice(start, end));
      start = end;
    }
    return parts;
  }
}
Enter fullscreen mode Exit fullscreen mode

Usage Example

// In your React component
import React, { useState } from 'react';
import { Uploader } from './Uploader';

function FileUploadComponent() {
  const [progress, setProgress] = useState(0);

  const handleFileUpload = (event) => {
    const file = event.target.files[0];
    const uploader = new Uploader(file, (percent) => setProgress(percent));
    uploader.start();
  };

  return (
    <div>
      <input type="file" onChange={handleFileUpload} />
      <progress value={progress} max="100">{progress}%</progress>
    </div>
  );
}

export default FileUploadComponent;
Enter fullscreen mode Exit fullscreen mode

5. Security Considerations and Best Practices

  • Limit Presigned URL Permissions: Ensure that presigned URLs only grant necessary permissions (e.g., only allow PUT operations for uploads).
  • Set Appropriate Expiration Times: Presigned URLs should expire after a reasonable time (e.g., 15 minutes to 1 hour) to minimize the window for misuse.
  • Validate File Metadata: On the backend, validate any metadata or parameters sent from the client to prevent manipulation (e.g., enforce allowed file types or sizes).
  • Use HTTPS: Always use HTTPS for communication between the client and your backend, and when accessing S3, to protect data in transit.
  • Monitor and Log: Implement logging and monitoring on both the backend and S3 to detect any unusual activities or errors.

6. Additional Considerations

Limiting Object Size

While AWS S3 supports objects up to 5 TiB (terabytes) in size, uploading such massive files directly from a browser is impractical and often impossible due to browser limitations and client-side resource constraints. Browsers can crash or become unresponsive when handling extremely large files, especially if they need to be processed in memory.

Recommendation:
  • Set Practical Limits: Define a maximum file size that your application will support for client-side uploads (e.g., 100 GB or less).
  • Inform Users: Provide feedback to users about the maximum allowed file size and handle validation on the client side before initiating the upload.

Retry Strategy

Uploading large files increases the risk of network interruptions or failures during the upload process. Implementing a robust retry strategy is crucial to enhance the user experience and ensure successful uploads.

Strategies
  • Automatic Retries: Automatically retry failed parts a limited number of times before prompting the user.
  • Resumable Uploads: Keep track of uploaded parts so that the upload can resume from where it left off rather than starting over.
  • Error Handling: Provide informative error messages to the user if retries fail, possibly suggesting actions like checking their network connection.

Multipart Upload Cleanup

Incomplete multipart uploads can accumulate in your S3 bucket, consuming storage space and potentially incurring costs.

Considerations
  • Abort Unfinished Uploads: If an upload fails or is canceled, ensure that your application calls the AbortMultipartUpload API to clean up any uploaded parts.
  • Lifecycle Rules: Configure S3 Lifecycle policies to automatically abort incomplete multipart uploads after a certain period (e.g., 7 days). This helps manage storage costs and keeps your bucket clean.

Example Lifecycle Rule Configuration:

{
  "Rules": [
    {
      "ID": "AbortIncompleteMultipartUpload",
      "Status": "Enabled",
      "AbortIncompleteMultipartUpload": {
        "DaysAfterInitiation": 7
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Handling Multipart Uploads Off the Main Thread

Uploading large files can be resource-intensive and may cause the browser’s main thread to become unresponsive, leading to a poor user experience.

Solution:
  • Use Web Workers: Offload the upload process to a Web Worker. Web Workers run in the background, separate from the main execution thread of the web application, allowing you to perform resource-intensive operations without blocking the UI.
Benefits:
  • Improved Performance: Frees up the main thread, ensuring that the UI remains responsive during the upload process.
  • Reduced Memory Usage: Helps manage memory more effectively, as large data processing can be handled within the worker.
  • Enhanced Stability: Reduces the risk of the browser becoming unresponsive or crashing during large uploads.

7. Browser Compatibility Considerations

When implementing client-side multipart uploads, browser compatibility is indeed a concern. Different browsers may have varying levels of support for the APIs and features required for handling large file uploads, such as the *File API, Blob slicing, Web Workers, and network request handling*. Navigating these differences successfully is crucial to ensure a consistent and reliable user experience across all supported browsers.

Compatibility Concerns:

  • File API and Blob Methods: Most modern browsers support Blob.slice(), but older browsers may use Blob.webkitSlice() or Blob.mozSlice().
  • Web Workers: Supported in modern browsers, but not in some older ones or with limitations in Internet Explorer.
  • Fetch API and XMLHttpRequest: While fetch() is widely supported, upload progress events with fetch() are not consistently available across all browsers.
  • Maximum Concurrent Connections: Limit the number of simultaneous uploads based on the lowest common denominator among your supported browsers (e.g., 6 concurrent connections).
  • Memory Constraints: Process files in small chunks and avoid loading the entire file into memory at once.
  • CORS: Configure S3 CORS policies to support the necessary HTTP methods (e.g., PUT, POST) and headers.

Conclusion

By implementing client-side uploads with presigned URLs and multipart upload, you can efficiently handle file uploads of any size directly to S3, reducing server load and improving performance. Remember to keep security at the forefront by securely managing AWS credentials and limiting the permissions and lifespan of presigned URLs.

This guide provided a step-by-step approach to setting up a secure and scalable file upload system using AWS S3, the AWS SDK for JavaScript, and presigned URLs. With the provided code examples and best practices, you’re well on your way to enhancing your application’s file upload capabilities.

Top comments (0)