Cloud & Hosting (AWS)

AWS S3 Setup & Deployment Guide

Unlimited object storage – create buckets, upload from the CLI and Node.js, share with presigned URLs, and host static sites securely with CloudFront.

Updated Official documentation ↗

1. Introduction & use cases

Amazon S3 (Simple Storage Service) stores files – called objects – in buckets. It is extremely durable, scales without limits and charges for what you store and transfer. Each object is reached by a key (its path), e.g. uploads/2026/photo.jpg.

Use S3 for user uploads, backups, logs, build artifacts and data lakes, and – together with CloudFront – for hosting static websites. New buckets block public access by default; share files with presigned URLs or through CloudFront instead of making buckets public.

Where it is used

  • User uploads (images, documents) for web/mobile apps
  • Backups and archives (with cheaper storage classes)
  • Static websites and SPA hosting behind CloudFront
  • CI artifacts, logs and data for analytics

2. Prerequisites

You needVersion / notes
AWS accountWith MFA on the root user and a non-root admin identity
AWS CLI v2Installed and configured – see the AWS guide
Node.js LTSFor the SDK example

3. Local setup

  1. 1Create a bucket

    Names are global and DNS-safe (lowercase, numbers, hyphens). Keep Block Public Access on.

    BASH
    aws s3 mb s3://my-app-uploads-2026 --region ap-south-1
    aws s3api get-public-access-block --bucket my-app-uploads-2026
  2. 2Upload, list and download

    sync copies only changed files – great for deploying folders.

    BASH
    aws s3 cp photo.jpg s3://my-app-uploads-2026/images/photo.jpg
    aws s3 ls s3://my-app-uploads-2026/images/
    aws s3 sync ./dist s3://my-app-uploads-2026/site/ --delete
    aws s3 cp s3://my-app-uploads-2026/images/photo.jpg ./copy.jpg
  3. 3Share a private file temporarily

    A presigned URL works for a limited time without making the object public.

    BASH
    aws s3 presign s3://my-app-uploads-2026/images/photo.jpg --expires-in 3600

4. Create & run a project

  1. 1Install the AWS SDK v3 packages

    Only install the clients you need.

    BASH
    mkdir s3-demo && cd s3-demo
    npm init -y && npm pkg set type=module
    npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
  2. 2Upload from Node.js and create a download link

    Credentials come from your profile locally and from the IAM role on AWS.

    index.js
    import { S3Client, PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
    import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
    import { readFile } from 'node:fs/promises';
    
    const s3 = new S3Client({ region: process.env.AWS_REGION });
    const Bucket = process.env.S3_BUCKET;
    const Key = 'images/hello.jpg';
    
    await s3.send(new PutObjectCommand({ Bucket, Key, Body: await readFile('hello.jpg'), ContentType: 'image/jpeg' }));
    const url = await getSignedUrl(s3, new GetObjectCommand({ Bucket, Key }), { expiresIn: 600 });
    console.log('Uploaded. Link valid for 10 minutes:', url);
  3. 3Let browsers upload directly (presigned PUT)

    Your API creates a presigned PutObjectCommand URL; the browser uploads the file straight to S3, saving server bandwidth. Allow your site’s origin with a CORS rule: save it as cors.json wrapped as {"CORSRules": [ … ]} and apply it with aws s3api put-bucket-cors --bucket my-app-uploads-2026 --cors-configuration file://cors.json.

    cors.json
    [
      {
        "AllowedOrigins": ["https://example.com", "http://localhost:5173"],
        "AllowedMethods": ["GET", "PUT"],
        "AllowedHeaders": ["*"],
        "ExposeHeaders": ["ETag"],
        "MaxAgeSeconds": 3000
      }
    ]
  4. 4Run it

    Use your CLI profile for credentials.

    BASH
    AWS_PROFILE=dev node --env-file=.env index.js

✅ When it works you should see output like Figure 1.

5. Development configuration

Environment variables

.env
AWS_REGION=ap-south-1
S3_BUCKET=my-app-uploads-2026
# locally: AWS_PROFILE=dev   · on AWS: IAM role

Key dependencies

Package / toolUsed for
@aws-sdk/client-s3S3 API for JavaScript (v3, modular)
@aws-sdk/s3-request-presignerPresigned URLs
boto3Python SDK
aws s3 / s3apiCLI commands
S3 storage classesStandard, Intelligent-Tiering, Glacier for archives

Folder structure

project structure
my-app-uploads-2026 (bucket)
├── images/
│   └── 2026/12/photo.jpg
├── documents/
├── backups/
└── site/            # static site files (served via CloudFront)

Essential commands

CommandWhat it does
aws s3 ls s3://bucket/prefix/List objects
aws s3 cp / mv / rmCopy, move, delete
aws s3 sync dir s3://bucket/ --deleteMirror a folder
aws s3 presign s3://bucket/keyTemporary link
aws s3api put-bucket-versioning --bucket b --versioning-configuration Status=EnabledKeep old versions

6. Production deployment

Server & hosting requirements

RequirementRecommendation
AccessBlock Public Access ON; access via IAM roles, presigned URLs or CloudFront OAC
Least privilegeApp role allowed only s3:GetObject/PutObject on its bucket/prefix
ProtectionVersioning + default encryption (SSE-S3 on by default)
CostsLifecycle rules to move/delete old data
Static siteCloudFront distribution + ACM certificate + DNS

Deployment steps

  1. 1Give your app a least-privilege policy

    Attach to the EC2/Lambda/ECS role.

    iam-policy.json
    {
      "Version": "2012-10-17",
      "Statement": [{
        "Effect": "Allow",
        "Action": ["s3:GetObject", "s3:PutObject"],
        "Resource": "arn:aws:s3:::my-app-uploads-2026/images/*"
      }]
    }
  2. 2Turn on versioning

    Protects against accidental deletes and overwrites.

    BASH
    aws s3api put-bucket-versioning --bucket my-app-uploads-2026 --versioning-configuration Status=Enabled
  3. 3Add a lifecycle rule

    Example: move backups to Glacier after 30 days and delete them after a year; delete old object versions after 30 days. Apply it with aws s3api put-bucket-lifecycle-configuration --bucket my-app-uploads-2026 --lifecycle-configuration file://lifecycle.json.

    lifecycle.json
    {
      "Rules": [{
        "ID": "backups",
        "Filter": { "Prefix": "backups/" },
        "Status": "Enabled",
        "Transitions": [{ "Days": 30, "StorageClass": "GLACIER" }],
        "Expiration": { "Days": 365 },
        "NoncurrentVersionExpiration": { "NoncurrentDays": 30 }
      }]
    }
  4. 4Host a static website

    Upload the build to a private bucket and serve it through CloudFront with Origin Access Control and HTTPS – step by step in the CloudFront guide.

    BASH
    npm run build
    aws s3 sync dist/ s3://my-site-bucket/ --delete

🗺 The whole path from your code to the live site is drawn in the deployment diagram.

7. Images & screenshots

Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.

terminal$ aws s3 cp photo.jpg s3://my-app-uploads-2026/images/photo.jpgupload: ./photo.jpg to s3://my-app-uploads-2026/images/photo.jpg$ AWS_PROFILE=dev node --env-file=.env index.jsUploaded. Link valid for 10 minutes: https://my-app-uploads-2026.s3.ap-south-1.amazon…
Figure 1: terminal output of the sample AWS S3 project (section 4).
1Private bucket2IAM role /presigned URL3Upload + sync4Versioning +lifecycle5CloudFront forpublic
Figure 2: production deployment flow (section 6).
my-app-uploads-2026 (bucket)├── images/│ └── 2026/12/photo.jpg├── documents/├── backups/└── site/ # static site files (served via CloudFront)
Figure 3: project folder structure (section 5).

8. Common errors & solutions

AccessDenied on upload or download

The identity’s policy (or the bucket policy) does not allow the action on that ARN. Check bucket/* vs bucket in the Resource and the region/profile used.

BucketAlreadyExists

Bucket names are global across all AWS accounts. Add a unique suffix.

Browser upload blocked by CORS

Add a CORS rule with your exact origin and the PUT method; presigned URLs also need the same Content-Type that was signed.

SignatureDoesNotMatch with presigned URLs

The request differs from what was signed (method, headers, Content-Type) or the clock is wrong.

Accidentally public data

Keep Block Public Access on at account and bucket level; use CloudFront OAC or presigned URLs instead of public buckets.

Best practices

  • Never make buckets public; use CloudFront or presigned URLs.
  • One bucket per purpose/environment, with clear prefixes.
  • Versioning + lifecycle rules for safety and cost.
  • Use IAM roles, not access keys, from AWS compute.
  • Set correct Content-Type and cache headers when uploading site assets.