Search This Blog

2024/05/04

AWS DynamoDB: How to install locally in Ubuntu?

It is possible to install dynamodb locally,lets explore how.

First Download jar file

wget https://d1ni2b6xgvw0s0.cloudfront.net/v2.x/dynamodb_local_latest.tar.gz

Now extract the the archieve.

Rename extracted folder as dynamodb

Copy it to location where you want to install binary

mv dynamodb /usr/share/

Now on command line go to folder in which dynamodb is copied,
in this case /usr/share/dynamodb.

cd /usr/share/dynamodb

Test i it runs from terminal by running this

sudo java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

Now if you want to test the server each time you would have to run this command
To keep it running each time without running manually we need a service.

To create a service

sudo nano /etc/systemd/system/dynamodb.service

add ollowing to it

[Unit]
Description=DynamoDB Service
[Service]
User=root
WorkingDirectory=/usr/share/dynamodb
ExecStart=/usr/share/dynamodb/dynamodb.sh
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target

save & exit


Now we will create a shell script file referred in service above.

cd /usr/share/dynamodb
nano dynamodb.sh

add

#!/bin/sh
sudo java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb


save and exit.

Make shell script executable file

chmod u+x dynamodb.sh

Now let system know that we have created a new service

sudo systemctl daemon-reload
sudo systemctl enable dynamodb
sudo systemctl start dynamodb
sudo systemctl status dynamodb


output of last command in my case is like below

● dynamodb.service - Dynamo DB Local Service
Loaded: loaded (/etc/systemd/system/dynamodb.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-05-04 11:13:52 IST; 11min ago
Main PID: 33499 (dynamodb.sh)
Tasks: 41 (limit: 18708)
Memory: 150.8M
CPU: 4.333s
CGroup: /system.slice/dynamodb.service
├─33499 /bin/sh /usr/share/dynamodb/dynamodb.sh
├─33500 sudo java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
└─33501 java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

May 04 11:13:52 sangram-Inspiron-14-5430 sudo[33500]: root : PWD=/usr/share/dynamodb ; USER=root ; COMMAND=/usr/bin/java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
May 04 11:13:52 sangram-Inspiron-14-5430 sudo[33500]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: Initializing DynamoDB Local with the following configuration:
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: Port: 8000
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: InMemory: false
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: Version: 2.4.0
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: DbPath: null
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: SharedDb: true
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: shouldDelayTransientStatuses: false
May 04 11:13:53 sangram-Inspiron-14-5430 dynamodb.sh[33501]: CorsParams: null


Now we need to install awscli on ubuntu as ollows

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"
-o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install


Now run aws configure
and give below response to when prompted.

AWS Access Key ID [****************yId"]: fakeMyKeyId
AWS Secret Access Key [****************Key"]: fakeSecretAccessKey
Default region name ["fakeRegion"]: fakeRegion
Default output format [None]:


The response need to be given as given above.


Now you can check lists o tables in dynamodb by running follwoing command

aws dynamodb list-tables --endpoint-url http://localhost:8000

You may also like to check how to do CRUD oprtion in dynamoDb using exprress
https://msdotnetbuddy.blogspot.com/2023/05/working-with-dynamo-db.html.

References:
https://medium.com/aws-lambda-serverless-developer-guide-with-hands/
amazon-dynamodb-primary-key-partition-key-and-sort-key-how-to-choose-right-
key-for-dynamodb-ea5673cb87c0

2024/04/30

Sequelize-Postgres-Express:Upload file to database

Today we will explore how to upload an file (image) into

mysql database using sequelize.

Lets first create a express application using express generator.

express --view=ejs express-postgres-upload-file

Here is my package.json whivh you can check to install required npm
packages,

{
"name": "express-mysql-upload-file",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"ejs": "~2.6.1",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"multer": "^1.4.5-lts.1",
"pg": "^8.11.5",
"sequelize": "^6.37.3"
}
}

Here we are installing multer,dotenv,sequelize,mysql2 packages.

Run npm i.

create .env file in root folder with content

PG_USER=sangram
PG_PASSWORD="sangram#81"
PG_PORT=5432
PG_DATABASE=playground
PG_SERVER=localhost


Now add upload.js in root folder with ollowing content.

var multer = require("multer");
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./public/uploads/profile_pic/");
},
filename: function (req, file, cb) {
var fileparts = file.originalname.split(".");
var ext = fileparts[fileparts.length - 1];
cb(null, file.fieldname + "-" + Date.now() + "." + ext);
},
});

var upload = multer({ storage: storage });

module.exports = upload;

create models folder in root location & add image.js in it.

module.exports = function (sequelize, DataTypes) {
const Image = sequelize.define('image', {
imageId: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
mimeType: {
type: DataTypes.STRING,
},
fileName: {
type: DataTypes.STRING,
field: 'name'
},
data: {
type: DataTypes.BLOB("long"),
}
}, {
freezeTableName: true
});

return Image;
}

Now create index.js inside model folder with following content

let { sequelize, Sequelize } = require("../connection.js");

let db = {};
db.Images = require("./image.js")(Sequelize,sequelize);

db.Sequelize = Sequelize;
db.sequelize = sequelize;
module.exports = db;

Now add uploads folder in public folder & inside uploads folder
add profile_pic folder.

Now create demo.js inside rotes folder with following content.

var express = require("express");
var router = express.Router();
var db = require("../models");
var upload = require("../uploads");
var fs = require("fs");
var path = require("path");

router.post("/", upload.single("image"), async (req, res, next) => {
try {
let imageCreated = await db.Images.create({
mimeType: req.file.mimetype,
fileName: req.file.filename,
data: fs.readFileSync(
path.join("./public/uploads/profile_pic/" + req.file.filename)
),
});
res.json({
success: true,
message: "File Uploaded to Mysql Successfully",
data: imageCreated,
});
} catch (exp) {
res.json({ success: false, message: exp.message.toString() });
}
});


router.get("/:fileId", async (req, res) => {
try {
var imageFound = await db.Images.findOne({
where: { imageId: req.params.fileId },
});
var buffer = imageFound.data;
var mimeType = imageFound.mimeType;

res.contentType(mimeType);
res.send(buffer);
} catch (exp) {
res.json({ success: false, message: exp.message.toString() });
}
});

module.exports = router;

Inside app.js add

var demoRouter = require('./routes/demo');

and

app.use('/demo', demoRouter);

in suitable location.

Now we are ready to run our application.Usually testing can be done using
postman.

For uploading image

curl --location 'http://localhost:3000/demo' \
--form 'image=@"/home/sangram/Pictures/Photo.jpg"'

Output:
{
"success": true,
"message": "File Uploaded to Mysql Successfully",
"data": {
"imageId": 1,
"mimeType": "image/jpeg",
"fileName": "image-1714485923005.jpg",
"data":{contain binary data},
"updatedAt": "2024-04-30T13:07:35.512Z",
"createdAt": "2024-04-30T13:07:35.512Z"
}
}

Please notice imageId that we are going to use in next api call.

curl --location 'http://localhost:3000/demo/1'

This will output image uploaded in previous step.

The complete code of this application can be found at

https://github.com/gitsangramdesai/express-sequelize-pg-upload-file