Go through the code below carefully and comments are there to help you out. Aws-sdk is used to call AWS services using APIs.Multer is used to handle multipart/form data but primarily it is used for uploading images.Now main things to upload photo will be there in Routes.js file, but before moving ahead few dependencies needs to be installed: Module.exports = mongoose.model('products', ProductSchema) We will use the below product model for our API: const mongoose = require('mongoose') So we have to store the photo of our product and retrieve the link by which the photo is publicly accessible and we will store it in our MongoDB database. We will take example of shop only, referenced in article in the starting. (Don't try to use these they are not correct□□) PORT = 5000ĪWS_ACCESS_KEY_ID="AKIA2FYET3LYEFGEF2AHJ"ĪWS_ACCESS_KEY_SECRET="ycOIx4DIVmeoU+8bajusH4/Gnan0Xv2YZrB5K8gO" gitignore file so that you don't share your credentials with others. Now after making S3 bucket and generating the credentials, Let's get started with writing code for uploading of image.įirstly save the credentials that we generated in the. Give your bucket a unique name, select your server and uncheck the block all public access settings and then click on create your bucket will be created.Īfter creating the bucket let's generate our access keys and IDs which will be used in Node.js for the connection.Ĭlick on my credentials from top and then go access keys and generate and save it somewhere or download it (don't share it with anyone). You can read more here.įirst let's make our S3 bucket with the help of AWS console, sign-in to your console and then search for S3 in the search bar on the top and click on it. We can handle all our items using the AWS console, it provides 99.99 % availability and 99.999% durability. It is object storage here we can save any type of data that will be stored in the form of objects and can be retrieved from anywhere over the internet. MongoDB or any other Database (to store the link of the photo).Īmazon Simple Storage Service (S3) is a cloud storage service.If you don't have make your account here (for 12months you can use some service free of cost.) Create API Endpoints for REST API in Node.js.How to Make REST API with Node.js, Express, and MongoDB.Knowledge about how API works and how it is programmed using Node.js, If you don't know how to make APIs with Node.js.(it's optional but it is preferred to make a code repository). Any IDE of your choice like (VSCode, etc.).Making APIs that requires photos, so uploading photos to the Amazon S3 bucket and accessing it with the public url is one way and then we can save that public url in our MongoDB database for further retrieval.
0 Comments
Leave a Reply. |