HTML5 and Amazon S3 Multi-Part uploads

Amazon S3 multi-part uploads allow you to upload large files in smaller chunks, providing better reliability and performance. When combined with the HTML5 File API, you can create powerful client-side upload functionality.

Overview of S3 Multi-Part Uploads

Amazon S3 multi-part upload is a feature that enables you to upload a single object as a set of parts. Each part is a contiguous portion of the object's data, and you can upload these parts independently and in any order.

How HTML5 File API Works with S3

The HTML5 File API provides interfaces for accessing files from web applications. When combined with S3 multi-part uploads, you can:

  • Read file contents directly in the browser
  • Split large files into smaller chunks
  • Upload chunks directly to S3
  • Track upload progress for each part

Implementation Process

Here's the basic workflow for implementing HTML5 File API with S3 multi-part uploads:

Step 1: File Selection and Reading

<input type="file" id="fileInput" />
<div id="progress"></div>

<script>
document.getElementById('fileInput').addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
        console.log('File selected:', file.name);
        console.log('File size:', file.size, 'bytes');
        initiateMultipartUpload(file);
    }
});
</script>

Step 2: Initialize Multi-Part Upload

async function initiateMultipartUpload(file) {
    // Send request to your server to initiate S3 multipart upload
    const response = await fetch('/api/initiate-upload', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            fileName: file.name,
            fileType: file.type,
            fileSize: file.size
        })
    });
    
    const data = await response.json();
    const uploadId = data.uploadId;
    const key = data.key;
    
    // Start uploading parts
    uploadFileParts(file, uploadId, key);
}

Step 3: Upload File Parts

async function uploadFileParts(file, uploadId, key) {
    const chunkSize = 5 * 1024 * 1024; // 5MB chunks
    const totalParts = Math.ceil(file.size / chunkSize);
    const uploadPromises = [];
    
    for (let partNumber = 1; partNumber <= totalParts; partNumber++) {
        const start = (partNumber - 1) * chunkSize;
        const end = Math.min(start + chunkSize, file.size);
        const chunk = file.slice(start, end);
        
        uploadPromises.push(uploadPart(chunk, partNumber, uploadId, key));
    }
    
    // Wait for all parts to complete
    const parts = await Promise.all(uploadPromises);
    
    // Complete the multipart upload
    completeMultipartUpload(uploadId, key, parts);
}

Server-Side Requirements

Your server needs to handle several operations:

  • Initiate Upload: Create a multipart upload session with S3
  • Generate Signed URLs: Provide signed URLs for each part upload
  • Complete Upload: Combine all parts into the final object

Key Benefits

Benefit Description
Improved Reliability Failed parts can be retried without restarting entire upload
Better Performance Parallel uploads and resume capability
Large File Support Handle files up to 5TB with individual parts up to 5GB
Progress Tracking Monitor upload progress for each part

Security Considerations

When implementing this solution, ensure you:

  • Use signed URLs with appropriate expiration times
  • Validate file types and sizes on the server
  • Implement proper authentication and authorization
  • Store AWS credentials securely on the server-side only

Conclusion

Combining HTML5 File API with Amazon S3 multi-part uploads creates a robust solution for handling large file uploads. This approach provides better user experience through progress tracking and improved reliability through chunk-based uploading.

Updated on: 2026-03-15T23:18:59+05:30

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements