<?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: Ahad Ali</title>
    <description>The latest articles on DEV Community by Ahad Ali (@ahadcommit).</description>
    <link>https://dev.to/ahadcommit</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%2F3037130%2Feb5b698f-291a-4222-9555-16cbac5135be.webp</url>
      <title>DEV Community: Ahad Ali</title>
      <link>https://dev.to/ahadcommit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahadcommit"/>
    <language>en</language>
    <item>
      <title>Upload Files from Next.js to AWS S3 Using Presigned URLs</title>
      <dc:creator>Ahad Ali</dc:creator>
      <pubDate>Mon, 10 Nov 2025 16:33:34 +0000</pubDate>
      <link>https://dev.to/ahadcommit/upload-files-from-nextjs-to-aws-s3-using-presigned-urls-50k9</link>
      <guid>https://dev.to/ahadcommit/upload-files-from-nextjs-to-aws-s3-using-presigned-urls-50k9</guid>
      <description>&lt;p&gt;&lt;a href="https://example.com" rel="nofollow noopener noreferrer"&gt;Example Link&lt;/a&gt;&lt;br&gt;
In modern web apps, users often need to upload images, documents, or videos. Amazon S3 is one of the best cloud storage options — but directly uploading files from a frontend like Next.js can expose your AWS credentials.&lt;/p&gt;

&lt;p&gt;In this tutorial, you’ll learn how to securely upload files from a Next.js app to AWS S3 using presigned URLs, step by step.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;Step 1 – Create and Configure an S3 Bucket&lt;br&gt;
Step 2 – Add Environment Variables&lt;br&gt;
Step 3 – Install AWS SDK v3&lt;br&gt;
Step 4 – Create API Route for Presigned URL&lt;br&gt;
Step 5 – Upload from Frontend&lt;br&gt;
Step 6 – Common Errors &amp;amp; Fixes&lt;br&gt;
Step 7 – Final Directory Structure&lt;br&gt;
Step 8 – Conclusion&lt;/p&gt;
&lt;h2&gt;
  
  
  🧠 Why Presigned URLs?
&lt;/h2&gt;

&lt;p&gt;When you generate a presigned URL on the server, it gives the client temporary permission to upload files to S3 — without exposing your AWS secret keys.&lt;br&gt;
✅ Secure&lt;br&gt;
✅ Fast (direct upload to AWS, not via backend)&lt;br&gt;
✅ Scalable for production&lt;/p&gt;
&lt;h2&gt;
  
  
  ⚙️ Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before you begin:&lt;br&gt;
AWS account (with S3 access)&lt;br&gt;
Node.js &amp;amp; Next.js project setup&lt;br&gt;
Basic understanding of environment variables&lt;/p&gt;
&lt;h2&gt;
  
  
  🔧 Step 1: Create and Configure an S3 Bucket
&lt;/h2&gt;

&lt;p&gt;Go to your AWS Console → S3 → Create Bucket&lt;/p&gt;

&lt;p&gt;Choose a unique bucket name&lt;/p&gt;

&lt;p&gt;Enable required region (e.g., ap-south-1)&lt;/p&gt;

&lt;p&gt;In Permissions, uncheck “Block all public access” if you want public access for uploaded files (optional).&lt;/p&gt;

&lt;p&gt;Add this CORS configuration (important):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "AllowedHeaders": ["*"],
    "AllowedMethods": ["GET", "PUT"],
    "AllowedOrigins": ["*"],
    "ExposeHeaders": []
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔑 Step 2: Add Environment Variables
&lt;/h2&gt;

&lt;p&gt;Create a .env.local file in your Next.js project and add:&lt;br&gt;
AWS_ACCESS_KEY_ID=your_access_key&lt;br&gt;
AWS_SECRET_ACCESS_KEY=your_secret_key&lt;br&gt;
AWS_REGION=ap-south-1&lt;br&gt;
S3_BUCKET_NAME=your_bucket_name&lt;/p&gt;

&lt;p&gt;Make sure not to commit these to GitHub (they’re secret).&lt;/p&gt;
&lt;h2&gt;
  
  
  🧩 Step 3: Install AWS SDK v3
&lt;/h2&gt;

&lt;p&gt;Run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @aws-sdk/client-s3

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧠 Step 4: Create an API Route for Presigned URL
&lt;/h2&gt;

&lt;p&gt;Inside your Next.js app, create the following file:&lt;br&gt;
 /app/api/upload-url/route.js (for Next.js 13+ with App Router)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

const s3 = new S3Client({
  region: process.env.AWS_REGION,
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  },
});

export async function GET(req) {
  const { searchParams } = new URL(req.url);
  const fileName = searchParams.get("file");
  const fileType = searchParams.get("type");

  const command = new PutObjectCommand({
    Bucket: process.env.S3_BUCKET_NAME,
    Key: fileName,
    ContentType: fileType,
  });

  const signedUrl = await getSignedUrl(s3, command, { expiresIn: 60 });

  return new Response(JSON.stringify({ url: signedUrl }), {
    headers: { "Content-Type": "application/json" },
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 This route:&lt;br&gt;
Generates a signed URL valid for 60 seconds&lt;/p&gt;

&lt;p&gt;Returns it to the client so the file can be uploaded securely&lt;/p&gt;
&lt;h2&gt;
  
  
  🧠 Step 5: Upload from Frontend
&lt;/h2&gt;

&lt;p&gt;Now, create a simple upload component:&lt;br&gt;
 /app/upload/page.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client";

import { useState } from "react";

export default function UploadPage() {
  const [file, setFile] = useState(null);
  const [uploading, setUploading] = useState(false);

  const handleUpload = async () =&amp;gt; {
    if (!file) return;

    setUploading(true);

    // 1. Request presigned URL from API
    const res = await fetch(
      `/api/upload-url?file=${file.name}&amp;amp;type=${file.type}`
    );
    const { url } = await res.json();

    // 2. Upload file directly to S3
    const upload = await fetch(url, {
      method: "PUT",
      body: file,
      headers: { "Content-Type": file.type },
    });

    if (upload.ok) {
      alert("✅ File uploaded successfully!");
    } else {
      alert("❌ Upload failed.");
    }

    setUploading(false);
  };

  return (
    &amp;lt;div className="flex flex-col items-center mt-20"&amp;gt;
      &amp;lt;input
        type="file"
        onChange={(e) =&amp;gt; setFile(e.target.files[0])}
        className="mb-4"
      /&amp;gt;
      &amp;lt;button
        onClick={handleUpload}
        disabled={uploading}
        className="bg-blue-600 text-white px-4 py-2 rounded"
      &amp;gt;
        {uploading ? "Uploading..." : "Upload"}
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⚠️ Step 6: Common Errors &amp;amp; Fixes
&lt;/h2&gt;

&lt;p&gt;Error&lt;br&gt;
Cause&lt;br&gt;
Fix&lt;br&gt;
403 Forbidden&lt;br&gt;
Wrong IAM permissions&lt;br&gt;
Ensure bucket allows PutObject for your AWS user&lt;br&gt;
CORS error&lt;br&gt;
CORS policy missing in bucket&lt;br&gt;
Add correct CORS config (see Step 1)&lt;br&gt;
AccessDenied&lt;br&gt;
Wrong region or key&lt;br&gt;
Check .env.local values&lt;/p&gt;

&lt;h2&gt;
  
  
  🧾 Step 7: Final Directory Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/
 ├─ api/
 │   └─ upload-url/
 │       └─ route.js
 ├─ upload/
 │   └─ page.jsx
.env.local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✅ Conclusion
&lt;/h2&gt;

&lt;p&gt;You’ve successfully learned how to upload files from Next.js to AWS S3 using presigned URLs.&lt;br&gt;
This approach is secure, efficient, and scalable for production use.&lt;br&gt;
If you found this helpful:&lt;br&gt;
⭐ Check out the full source code on GitHub: github.com/ahadali0500/nextjs-s3-upload&lt;/p&gt;

&lt;p&gt;🧠 Follow me for more DevOps + Next.js guides!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>nextjs</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Upload Files to AWS S3 with Laravel 11 &amp; Next.js Using Pre-signed URLs</title>
      <dc:creator>Ahad Ali</dc:creator>
      <pubDate>Thu, 10 Apr 2025 08:25:52 +0000</pubDate>
      <link>https://dev.to/ahadcommit/upload-files-to-aws-s3-with-laravel-11-nextjs-using-pre-signed-urls-46m3</link>
      <guid>https://dev.to/ahadcommit/upload-files-to-aws-s3-with-laravel-11-nextjs-using-pre-signed-urls-46m3</guid>
      <description>&lt;p&gt;*&lt;em&gt;Table of Contents&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1. Introduction&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Setting Up AWS S3 Bucket&lt;/li&gt;
&lt;li&gt;Configuring Laravel Backend
   Installing AWS SDK
   Setting Up Environment Variables
   Creating the Route and Controller&lt;/li&gt;
&lt;li&gt;Implementing Next.js Frontend
  Uploading Files Using Pre-signed URLs
  Creating the Upload Component&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Conclusion&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Introduction &lt;a&gt;&lt;/a&gt;&lt;br&gt;
Uploading files directly to Amazon S3 using pre-signed URLs is a common pattern that allows clients to upload files without the files passing through your server. This method reduces server load and can improve upload performance. In this guide, we'll walk through setting up a Laravel backend to generate these pre-signed URLs and a Next.js frontend to upload files using them&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
