This repository is dedicated to providing basic implementations and operations for working with AWS S3 buckets. Our goal is to efficiently read and write images using the S3 bucket infrastructure. Additionally, we utilize the local machine's disk as a cache to minimize the number of requests made to AWS.
The rationale behind this approach is caching and minimizing latency. When we store images on the local disk instead of sending every request to AWS, we significantly reduce the latency associated with retrieving images. By leveraging the local cache, we can quickly access the images without relying solely on network requests to AWS. This approach not only improves the overall performance of the image server but also reduces the dependency on network speed and availability.
Our images remain securely stored in the S3 bucket while benefiting from the reduced latency provided by the local disk cache. This strategy proves especially useful in scenarios where frequent access to images is required, as it eliminates the need for repetitive round trips to the remote storage infrastructure.
Additionally, storing images on the local disk facilitates easy migration of the project to another virtual machine. The images can be fetched from the S3 bucket, ensuring a seamless transition without the need to transfer all the images to the new environment.
In summary, by utilizing the disk cache alongside the AWS S3 bucket, we strike a balance between low latency and reliable image storage, enhancing the overall performance and scalability of the image server.
This project serves as an example usage. For enhanced validation, we recommend utilizing third-party packages such as Joi. Additionally, you have the flexibility to create your own Store
by extending your store class with the BaseStore
class.
To get started with the project, follow these steps:
- Create a new file called
.env
by copying the.env.example
file and filling in the required environment variables. - Install the project dependencies by executing the command
yarn install
. - Build the project using the command
yarn build
, and then launch the project withyarn start:prod
.
Optionally, if you want to run the project in development
mode, you can start it by executing the command yarn start:dev
.
By following these steps, you will set up the necessary configuration and launch the image server. 🌟
To use this project, you'll need an AWS S3 bucket. Follow these steps to create a bucket:
- Sign in to your AWS account (or create one if you don't have it yet).
- Use the search bar at the top of the page to find and select the S3 service.
- Click the Create bucket button to create a new bucket.
- Provide a unique name for your bucket and configure the remaining settings. In most cases, the default settings are suitable and secure.
Once you've completed these steps, your AWS S3 bucket will be created. Now, let's proceed to creating a new user and policy to manage the bucket. But before that...
Policies are fundamental for ensuring the security of your AWS resources, including your S3 bucket. It's crucial to set them up correctly and implement appropriate access controls to protect your data.
Make sure you have checked the Block all public access checkbox while creating the S3 bucket. If you didn't, please delete the old bucket and create a new one with that option checked to prevent unauthorized access.
Remember, properly configuring policies and access controls is essential for maintaining the security and privacy of your AWS resources.
To access the S3 bucket programmatically, we need to create a separate user and assign that user to a pre-made policy for the bucket using the IAM (Identity and Access Management) service in AWS.
Here's how you can do it:
-
Create a new policy for the bucket:
-
Use the search bar at the top of the page to find and select the IAM service.
-
Select Policies under Access management from the sidebar.
-
Click the Create policy button.
-
Select the S3 service.
-
Open the Read section, find
GetObject
, select it, and then you can close the Read section. -
Open the Write section, find
PutObject
, select it, and then you can close the Write section. -
Click the Add arn button in the Resources section.
-
Enter the bucket's name in the Resource bucket name section.
-
Check the Any object name checkbox in the Resource object name section, then click the Add ARNs button to finish.
-
-
Click the Next button at the bottom of the page.
-
Give your policy a name, and then click the Create policy button to finish.
-
-
Create a new user:
-
Select Users under Access management from the sidebar.
-
Click the Add users button to create a new user.
-
Provide a name for your user, and then click the Next button.
-
Select Attach policies directly, search for the created policy name, and select it.
-
Click the Next button, and then click the Create user button to finish the process.
-
-
Create access tokens for the user:
-
Select the created user.
-
Click the Security credentials tab.
-
Scroll down and click the Create access key button.
-
Select Application running outside AWS or Other.
-
Click the Next button, and then click the Create access key button.
-
Copy and securely save the Access key and Secret access key values. Treat the secret access key as a password and keep it private, as it provides access to the user.
⚠️ -
Click the Done button to finish the process.
-
Now, you can access the AWS user account programmatically using the access key and secret access key. Ensure that you fill the .env
file with the correct information, including the access keys.
Make sure to handle the secret access key securely and keep it confidential to maintain the security of your AWS resources.
If you find this project useful and would like to support me, you can do so by visiting my website.