Giter VIP home page Giter VIP logo

dharmveer97 / aws-image-uploading Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 113 KB

Code and resources for optimizing image uploading speed to AWS S3 buckets in a Next.js app directory using @apollo/server , including directory API routes for AWS S3 bucket integration. Utilizes @aws-sdk, @aws-sdk/client-s3, @as-integrations/next, dropzone, graphql, and graphql-tag.

JavaScript 95.51% CSS 4.49%
aws aws-s3 dropzone image-processing imagecompression imageupload aws-sdk nextjs-app-router nextjs14 apollo-server buckets3 formik graphql sdk yup getsignedurl multipleimagepicker next next-js browser-image-compression

aws-image-uploading's Introduction

Getting Started

First, run the development server:

npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev

Setting up AWS S3 for Image Uploads

Follow these steps to enable image uploading to an AWS S3 bucket in your Next.js application:

1. Create an S3 Bucket

  • Go to the AWS S3 Management Console.
  • Click on "Create bucket."
  • Enter a unique bucket name and select the region from the URL (e.g., ca-central-1).
  • Click "Create."

2. Set Up Environment Variables

Add the following environment variables to your .env file:

NEXT_PUBLIC_REGION=your-region
NEXT_PUBLIC_AWS_BUCKET=your-bucket-name
NEXT_PUBLIC_AWS_ACCESS_KEY_ID=your-access-key-id
NEXT_PUBLIC_AWS_SECRET_ACCESS_KEY=your-secret-access-key

3. Create an S3 Bucket Policy

Add the following policy to your S3 bucket's CORS configuration to allow cross-origin requests:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
        }
    ]
}

4. Configure Cross-Origin Resource Sharing (CORS)

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "PUT",
            "POST",
            "DELETE",
            "HEAD"
        ],
        "AllowedOrigins": [
            "http://localhost:3000",
            "https://example.app",
            "http://example.com",
            "http://example.app"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    }
]

Create IAM role

Additional Information

  • Customize the CORS policy according to your application's needs, specifying the allowed headers, methods, and origins.
  • For security reasons, restrict permissions in the S3 bucket policy based on your application's requirements.

With these steps completed, your Next.js application should be configured to upload images to your AWS S3 bucket.

Additional Information

  • Customize the CORS policy according to your application's needs, specifying the allowed headers, methods, and origins.

  • For security reasons, restrict permissions in the S3 bucket policy based on your application's requirements.

  • Additionally, in the policy section, you can create policies for specific actions such as requests, updates, or deletes. It is not recommended to provide full access for the live environment.

  • It is essential to carefully review and configure the bucket policy to ensure the security and integrity of your AWS S3 bucket.

aws-image-uploading's People

Contributors

dharmveer97 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.