<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: hamza rehman</title>
    <description>The latest articles on DEV Community by hamza rehman (@hamza_rehman_b781e16e23a1).</description>
    <link>https://dev.to/hamza_rehman_b781e16e23a1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2695175%2F172055a3-e6f8-44cc-91e4-87c2a596192c.jpeg</url>
      <title>DEV Community: hamza rehman</title>
      <link>https://dev.to/hamza_rehman_b781e16e23a1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamza_rehman_b781e16e23a1"/>
    <language>en</language>
    <item>
      <title>🚀 1. Efficient Video Uploads to AWS S3 with React</title>
      <dc:creator>hamza rehman</dc:creator>
      <pubDate>Sun, 12 Jan 2025 12:09:30 +0000</pubDate>
      <link>https://dev.to/hamza_rehman_b781e16e23a1/1-efficient-video-uploads-to-aws-s3-with-react-1cj4</link>
      <guid>https://dev.to/hamza_rehman_b781e16e23a1/1-efficient-video-uploads-to-aws-s3-with-react-1cj4</guid>
      <description>&lt;p&gt;&lt;strong&gt;The Problem with Base64 and Buffer Uploads&lt;/strong&gt;&lt;br&gt;
Many developers follow the approach of uploading videos to their backend as Base64 or Buffer and then transferring them to S3. While this works, it’s inefficient because:&lt;/p&gt;

&lt;p&gt;It increases server workload.&lt;br&gt;
Slows down the upload process.&lt;br&gt;
May expose your backend to unnecessary risks.&lt;br&gt;
The Better Solution: Pre-Signed URLs&lt;br&gt;
Pre-signed URLs let users upload files directly to S3 without exposing your AWS credentials. It’s faster, more secure, and reduces backend processing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gsev1tz8ihlpkw0r931.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gsev1tz8ihlpkw0r931.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backend: Generating Pre-Signed URLs&lt;/strong&gt;&lt;br&gt;
Here’s how you can create a pre-signed URL using aws-sdk in a Node.js backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AWS = require('aws-sdk');
const express = require('express');
const app = express();

AWS.config.update({
  accessKeyId: 'YOUR_ACCESS_KEY',
  secretAccessKey: 'YOUR_SECRET_KEY',
  region: 'YOUR_REGION',
});

const s3 = new AWS.S3();

app.get('/generate-presigned-url', (req, res) =&amp;gt; {
  const params = {
    Bucket: 'your-bucket-name',
    Key: `${Date.now()}-${req.query.filename}`,
    Expires: 60, // URL valid for 60 seconds
    ContentType: req.query.contentType,
  };

  s3.getSignedUrl('putObject', params, (err, url) =&amp;gt; {
    if (err) {
      return res.status(500).json({ error: 'Error generating URL' });
    }
    res.json({ url });
  });
});

app.listen(5000, () =&amp;gt; {
  console.log('Server running on port 5000');
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Frontend: Uploading Videos Directly to S3&lt;/strong&gt;&lt;br&gt;
In your React app, use the pre-signed URL to upload files directly to S3. This bypasses your backend entirely for the upload step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';

const uploadToS3 = async (file) =&amp;gt; {
  const response = await fetch(`/generate-presigned-url?filename=${file.name}&amp;amp;contentType=${file.type}`);
  const { url } = await response.json();

  await axios.put(url, file, {
    headers: { 'Content-Type': file.type },
  });

  console.log('File uploaded successfully');
};

const handleFileUpload = async (event) =&amp;gt; {
  const file = event.target.files[0];
  if (file) {
    await uploadToS3(file);
  }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🌟 2. Streaming Videos Like a Pro&lt;/strong&gt;&lt;br&gt;
Why Adaptive Streaming?&lt;br&gt;
Using adaptive streaming (HLS) ensures smooth playback across different network conditions. HLS creates .m3u8 files that allow players to adjust the video quality dynamically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Convert Videos to HLS&lt;/strong&gt;&lt;br&gt;
Use AWS tools like:&lt;/p&gt;

&lt;p&gt;AWS Elastic Transcoder&lt;br&gt;
AWS Elemental MediaConvert&lt;br&gt;
These services convert uploaded videos into HLS format for adaptive streaming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Serve Videos via CloudFront&lt;/strong&gt;&lt;br&gt;
Integrate Amazon CloudFront with your S3 bucket to enable global content delivery. CloudFront caches your videos at edge locations, reducing buffering for users worldwide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React Video Playback&lt;/strong&gt;&lt;br&gt;
Use react-player or video.js to play .m3u8 files in your React app. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactPlayer from 'react-player';

const VideoPlayer = () =&amp;gt; {
  return (
    &amp;lt;div className="video-player"&amp;gt;
      &amp;lt;ReactPlayer
        url="https://your-cloudfront-url/video.m3u8"
        controls
        width="100%"
        height="100%"
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default VideoPlayer;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🎯 3. Why This Approach Works&lt;/strong&gt;&lt;br&gt;
1.Faster Uploads: Direct S3 uploads minimize backend involvement.&lt;br&gt;
2.Scalable Playback: Adaptive streaming ensures smooth video playback, even on slow networks.&lt;br&gt;
3.Global Reach: CloudFront reduces latency for users worldwide.&lt;br&gt;
4.Cost-Effective: Process videos once and serve them efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💬 What’s Your Approach?&lt;/strong&gt;&lt;br&gt;
I’d love to hear how you handle video uploads and streaming in your projects. Are you using AWS, or have you explored other tools? Let’s discuss and share best practices in the comments! 🙌&lt;/p&gt;

</description>
      <category>aws</category>
      <category>react</category>
      <category>videostreaming</category>
      <category>s3</category>
    </item>
  </channel>
</rss>
