Skip to content

Commit

Permalink
Merge pull request #50 from zakhaev26/aws-client
Browse files Browse the repository at this point in the history
Aws client
  • Loading branch information
dishamodi0910 authored Oct 26, 2023
2 parents 18368d8 + 906241b commit 94a3f45
Show file tree
Hide file tree
Showing 13 changed files with 3,302 additions and 0 deletions.
6 changes: 6 additions & 0 deletions New_APIs/aws-client-api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AWS_BUCKET_NAME=<>
AWS_BUCKET_REGION=<>
AWS_ACCESS_KEY_ID=<>
AWS_SECRET_ACCESS_KEY=<>
MONGODB_URI=<>
PORT= 3900
1 change: 1 addition & 0 deletions New_APIs/aws-client-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
33 changes: 33 additions & 0 deletions New_APIs/aws-client-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# AWS S3 Multimedia Storage Client API
This is a client API that facilitates interaction with an AWS S3 bucket for storing and retrieving multimedia files. By utilizing the provided functions, you can easily upload and download multimedia files to and from the S3 bucket.

# Getting Started

## Prerequisites
Ensure you have the necessary AWS credentials and access rights to interact with the desired S3 bucket.
Set up a MongoDB instance for storing file metadata and related information.
## Installation
- Clone the repository.
- Navigate to aws-client-api directory
- Install the required dependencies using `npm install`.

## Usage
- Include the necessary database connection files in the src/dbconn directory.
- Configure the AWS S3 credentials in the awsconfig/aws-config.js file.
- Use the uploadMediaToS3 function to upload multimedia files to the S3 bucket. Ensure that the file input field name in your frontend matches the one specified in the multer uploads.
- Store the unique identifier related to the file for future lookup in the database.
- Leverage the downloadMediaFromS3 function to retrieve files from the S3 bucket when needed.
## Additional Considerations
- Customize the database schema according to your specific requirements, as users may have different data models.
- Implement a cloudfront URI associated with the S3 bucket to ensure faster multimedia file fetching and mitigate streaming issues, especially for large videos and music files.

## Request and Response Snapshots :

> Request (Upload) :
[Request](./utils/req.PNG)

> Response :
[Response](./utils/res.PNG)

> Stream (Via CDN) :
[Stream](./utils/stream.PNG)
3,087 changes: 3,087 additions & 0 deletions New_APIs/aws-client-api/package-lock.json

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions New_APIs/aws-client-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "aws-client-api",
"version": "1.0.0",
"description": "A Wrapper API which helps to upload images/video or any type of Multimedia Files to AWS S3 Bucket and Retrieve them Back using AWS Cloudfront.",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start:dev": "nodemon src/app.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/zakhaev26/apiverse.git"
},
"keywords": [
"api",
"aws"
],
"author": "Soubhik Kumar Gon (zakhaev26)",
"license": "ISC",
"bugs": {
"url": "https://github.com/zakhaev26/apiverse/issues"
},
"homepage": "https://github.com/zakhaev26/apiverse#readme",
"dependencies": {
"@aws-sdk/client-s3": "^3.433.0",
"aws-sdk": "^2.1478.0",
"body-parser": "^1.20.2",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongoose": "^7.6.3",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1"
}
}
39 changes: 39 additions & 0 deletions New_APIs/aws-client-api/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const {uploadMediaToS3 } = require("./aws-config");
const conn = require("./dbconn/conn");
conn();
const AWS_DOC =require("./dbconn/mongostash");

require("dotenv").config()
const express = require("express");
const app = express();
app.use(require('body-parser').urlencoded({extended:true}))
const multer = require("multer")
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.use(express.json())
app.get('/',(req,res)=>{
res.sendFile(__dirname+"/interface.html")
})

app.post("/load/file",upload.single('mmf'),async (req,res)=>{
try{
const {file_id} = req.body;
console.log(req.file)
console.log(file_id)
const hash = await uploadMediaToS3(req.file);
console.log("Hash::",hash);
const newDoc = new AWS_DOC({
file_id,
hash,
});
const finalDoc = await newDoc.save();
console.log(finalDoc);
res.redirect("/")
}catch(e) {
console.log('Error:',e.message);
}
})

app.listen(process.env.PORT,()=>{
console.log(`App live @${process.env.PORT}`);
})
53 changes: 53 additions & 0 deletions New_APIs/aws-client-api/src/aws-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require("dotenv").config();
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const { randomBytes } = require("crypto");

const bucketName = process.env.AWS_BUCKET_NAME;
const bucketRegion = process.env.AWS_BUCKET_REGION;
const accessKey = process.env.AWS_ACCESS_KEY;
const secretAccessKey = process.env.AWS_SECRET_KEY;

const s3 = new S3Client({
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretAccessKey,
},
region: bucketRegion,
});

function generateRandomHash() {
const rawBytes = randomBytes(16);
const hash = rawBytes.toString("hex");
return hash;
}

async function uploadMediaToS3(file) {
try {
const uniquehash = generateRandomHash();
console.log("uniqueHash::",uniquehash)
const params = {
Bucket: bucketName,
Key: uniquehash,
Body: file.buffer,
ContentType: file.mimetype,
};
const command = new PutObjectCommand(params);
await s3.send(command);
console.log(`${file.mimetype} file ${params.Key} was uploaded`);
return params.Key;
} catch (err) {
console.log("S3 error: ", err.message);
}
}

const downloadMediafromS3 = (key) => {
const downloadParams = {
Bucket: bucketName,
Key: key
}
return s3.getObject(downloadParams).createReadStream();
}

module.exports = {
uploadMediaToS3,downloadMediafromS3
};
15 changes: 15 additions & 0 deletions New_APIs/aws-client-api/src/dbconn/conn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require("mongoose");


async function conn() {

try{
await mongoose.connect(process.env.DB_URI);
console.log("Connection successful");
return;
}catch(e) {
console.log(e.message);
}
}

module.exports = conn;
17 changes: 17 additions & 0 deletions New_APIs/aws-client-api/src/dbconn/mongostash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

const mongoose = require("mongoose");

const AWS_DOC = mongoose.model('AWS_DOC', new mongoose.Schema({
file_id:{
type:String,
required:true
},
hash:{
type:String,
required:true
}
}));

module.exports = AWS_DOC;


17 changes: 17 additions & 0 deletions New_APIs/aws-client-api/src/interface.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<title>
aws-client-api
</title>
</head>

<body>
<h1>Aws Client Api</h1>

<form action="http://localhost:3900/load/file" method="POST" enctype="multipart/form-data">
<input type="text" name="file_id" placeholder="File ID"/>
<input type="file" name="mmf" placeholder="Upload File" />
<button type="submit">Upload!</button>
</form>
</body>
</html>
Binary file added New_APIs/aws-client-api/utils/req.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added New_APIs/aws-client-api/utils/res.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added New_APIs/aws-client-api/utils/stream.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 94a3f45

Please sign in to comment.