<- Back

what is AWS S3

S3 is a service provided by Amazon to store digital assets.

How to integrate the S3 with nextJS front end

  • Create aws account
  • Create S3 bucket in AWS
  • Install sdk - npm install aws-sdk
  • Upload files to bucket
  • Write below code to access assets
  • Note that you will need to supply accessKeyId, secretAccessKey and region.
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
const AWS = require('aws-sdk');

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

  AWS.config.update({
    accessKeyId: process.env.AWS_S3_ACCESS_KEY,
    secretAccessKey: process.env.AWS_S3_SECRET_ACCESS_KEY,
    region: 'us-east-2',
  });

const s3 = new AWS.S3();

//in below params object, you need to set properties like bucket, key (file path) and expiry time
const params = {
  Bucket: 'softpost-bucket',
  Key: 'How to install gherkin and cucumber for java plugins in InyelliJ IDEA .mp4',
  Expires: 120, // URL expiration time in seconds
};

const signedUrl = s3.getSignedUrl('getObject', params);

console.log(`Signed URL: ${signedUrl}`);

  //respond to client
  res.status(200).json({name: `${signedUrl}`});
}

Web development and Automation testing

solutions delivered!!