Skip to content

Instantly share code, notes, and snippets.

@willybeans
Last active January 26, 2024 00:46
Show Gist options
  • Save willybeans/03297606b21da25d9e321fec3b848f3c to your computer and use it in GitHub Desktop.
Save willybeans/03297606b21da25d9e321fec3b848f3c to your computer and use it in GitHub Desktop.
fetching files from S3 bucket in AWS
const aws = require("@aws-sdk/client-s3");
//
// Throws an error if the any required environment variables are missing.
//
if (!process.env.AWS_ACCESS_KEY_ID) {
throw new Error(
"Please specify the name of an AWS storage account in environment variable AWS_ACCESS_KEY_ID."
);
}
if (!process.env.AWS_SECRET_ACCESS_KEY) {
throw new Error(
"Please specify the access key to an AWs storage account in environment variable AWS_SECRET_ACCESS_KEY."
);
}
if (!process.env.AWS_REGION) {
throw new Error(
"Please specify the access region for AWs storage account in environment variable AWS_REGION."
);
}
//if you leave this blank, it will default to the local config file
const s3Client = new aws.S3Client({
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
const getObjectParams = {
Bucket: "video-storage-project",
Key: "SampleVideo_1280x720_1mb.mp4",
};
async function listBuckets() {
try {
const command = new aws.ListBucketsCommand({});
const { Buckets } = await s3Client.send(command);
console.log("List Buckets: ");
console.log(Buckets.map((bucket) => bucket.Name).join("\n"));
console.log("---end of buckets---");
return Buckets;
} catch (e) {
console.error("error at read buckets: ", e);
}
}
async function fetchFileFromS3() {
try {
const { Body, ContentLength } = await s3Client.send(
new aws.GetObjectCommand(getObjectParams)
);
return { Body, ContentLength };
} catch (e) {
console.error("error fetching file from s3");
console.error(e);
}
}
module.exports = { fetchFileFromS3, listBuckets };
const express = require("express");
const { fetchFileFromS3, listBuckets } = require("./aws_connect");
const multer = require("multer");
//
// Throws an error if the any required environment variables are missing.
//
if (!process.env.PORT) {
throw new Error(
"Please specify the port number for the HTTP server with the environment variable PORT."
);
}
const PORT = process.env.PORT;
console.log(
`Serving videos from AWS access key id ${process.env.AWS_ACCESS_KEY_ID}.`
);
const app = express();
//
// Registers a HTTP GET route to retrieve videos from storage.
//
app.get("/video", async (req, res) => {
const videoPath = req.route.path;
console.log("Serving file from ", videoPath);
const videoStream = await fetchFileFromS3();
res.writeHead(200, {
"Content-Type": "video/mp4",
"Content-Length": videoStream.ContentLength,
});
videoStream.Body.pipe(res);
});
// http://localhost:3000/video? path=SampleVideo_1280x720_1mb.mp4. =
app.get("/listBuckets", async (req, res) => {
const buckets = await listBuckets();
res.status(200).send("Buckets retreived", JSON.stringify(buckets));
});
// Set up multer for file uploads
const upload = multer({ dest: "uploads/" });
// Define a route to handle file uploads
app.post("/upload", upload.single("file"), async (req, res) => {
const fileStream = Readable.from(req.file.buffer);
const params = {
Bucket: "YOUR_S3_BUCKET_NAME",
Key: req.file.originalname,
Body: fileStream,
};
try {
await s3Client.send(new PutObjectCommand(params));
console.log("File uploaded successfully");
res.status(200).send("File uploaded");
} catch (err) {
console.error(err);
res.status(500).send("Failed to upload file");
}
});
//
// Starts the HTTP server.
//
app.listen(PORT, () => {
console.log(`Microservice online`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment