How to Easily Create AWS S3 Bucket Public/Private Access

JudisWeb – How to create a Public Bucket AWS S3 – Storage of files, media, videos sometimes becomes a problem when the server storage capacity starts to full and server performance is increasingly burdened by having to process these files when accessed publicly. AWS S3 is the best alternative as a cloud storage media that can be accessed publicly or privately.

What is AWS S3?

Amazon Web Services S3 (Amazon Simple Storage Service) is a service from AWS as object storage. Which offers features for data availability and data security as well as scalability. And can be used as big data analysis, websites, cloud apps. Designed with 99.9% durability meaning that the data we store will not be lost or damaged and it is easy to manage and configure access control both publicly and privately.

By default policies on AWS S3 are private. This means that only we can access the stored files, and only a few parties/clients we specify to access them by providing: rules against the file.

And to make Bucket S3 public or make it as web hosting is very easy.

AWS S3 Bucket

AWS S3 Bucket is a cloud-storage service from AWS where we can store anything in the form of files, images, videos, even a website (static). Before entering into the discussion of S3 Bucket, if you need an AWS account, if you don’t have an AWS account, please read the tutorial I made here or in this case use AWS Free Tier to deploy S3 Bucket.

AWS S3 Free Tier Specifications: 5 GB storage, 20,000 Get Requests, and 2,000 Put Requests free of charge.

If you already have an AWS account, log in to AWS Management Console.

On the AWS Management Console dashboard, look for the section Services and select S3.

Making S3 Bucket

AWS S3 Dashboard
AWS S3 Dashboard

After logging in to the AWS S3 Dashboard, click Create bucket.

General configuration

On General Information, fill in the S3 Bucket Name and select the Region where we will place the Bucket. Here I use the Singapore region as a storage area.

s3 bucket name
Create S3 Bucket

There are some rules for naming buckets:

  • The bucket name must be between 3 and 63 characters long.
  • Bucket names can only contain lowercase letters, numbers, periods (.), and hyphens (-).
  • Bucket names must begin and end with a letter or number.
  • Bucket names must not be formatted as IP addresses (for example, 172.122.23.4).

Copy settings from existing bucket – optional, we can create a new bucket with the same settings as the old bucket that we created earlier, for example setting Permissions, Tags, ACLs or Access Control List Public/Private.

Block Public Access settings for this bucket

s3 block all
Block all public access

In section Block Public Access settings for this bucket checked then all access to the bucket is private and cannot be accessed by the public.

Screenshots 20211026 162000275
Allow public access

Public access can be used as static web hosting.

Bucket Versioning

By activating Bucket Versioning, we are allowed to store object variants in the same bucket and the object variants can be used as backup objects for recovery, for example when running the application an error or failure occurs in processing the object.

s3 bucket no versioning
Disable Bucket Versioning

My example will create a simple text file that contains:

S3 Bucket Tutorial by JudisWeb

https://judisweb.com

And I I save the file with the filename: S3-important-doc.txt, if I upload the same file with different contents it will be saved with a different version.

If the Bucket Versioning mode is disabled then any same file name is automatically overwritten with a new file with the same name (overwrite).

I changed the contents of the same file and uploaded it to S3.

Amazon S3 Bucket Tutorial by JudisWeb 

https://judisweb.com
bucket versioning
Bucket Versioning

Default encryption

When server-side encryption is enabled, Amazon S3 encrypts objects before saving them to disk and decrypts them when we download objects.

Advanced Settings

Object Lock: The object will be locked permanently, cannot be overwritten with the same file name and so as not to be deleted.

Accessing S3 Bucket

After creating the bucket, next we will upload files into the bucket. When accessing the contents of the bucket, the navigation is very easy to operate. Creating folders, uploading files, setting meta data, etc.

s3 access object

Example: we create a folder documents, and upload files S3-important-doc.txt. We can open with the button Open when we click on the object and access it.

S3 private object access by open
S3 access granted

But when we open via S3 Object URL or public access then no access is allowed.

s3 private object uri
S3 Object URL
s3 access denied
S3 Access Denied

Turning S3 Bucket Into Web Hosting

Turning S3 into Web hosting is an interesting option to save publicly accessible HTML, JS, CSS, image (JPG, PNG) or video files because they are static files.

The method is very easy, if you have made an S3 Bucket with Block all public access no need to delete the bucket but you can change it in the section Permissions.

Screenshots 20211026 172804314 1
Edit Block public access

Click Permissions, and in part Block Public Access click Edit then uncheck and Save changes.

PicsArt 10 26 06.21.21
S3 Public Access

S3 Bucket Policy

Then in the Bucket Policy click Edit, then enter the Policy code in JSON:

s3 bucket policy arn public
S3 Bucket Policy
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<BUCKET_ARN>/*"
        }
    ]
}

Change as in the Policy and Save changes.

S3 Bucket Web Hosting Test

To try as a web hosting, then here we will create and upload files (static files) HTML, JS, CSS, JPG.

We will create a new folder “test-website” in the S3 Bucket and upload all the files and open it with a web browser.

HTML File: index.html

<!doctype html>
<html lang="en">
<head>
	<!-- Required meta tags --> 
	<meta charset="utf-8"> 
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
	<!-- Bootstrap CSS --> 
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
	<link rel="stylesheet" href="style.css"></link>
	<script src="alertdate.js"></script>
	<title>Welcome to S3 Bucket Tutorial by JudisWeb</title>
</head>
<body>
	<div align="center">
		<h1>Welcome to S3 Bucket Web Hosting</h1>
		<p>Test S3 Bucket web hosting by clicking the button below</p>
		<input class="button" type="button" value="SHOW ME THE DATE!!!" onclick="showDate()">
		<p></p>
		<p><img src="opengraph.jpg.webp" alt="JudisWeb" height="228" width="400" title="JudisWeb"></p>

<p>Visit <a href="https://judisweb.com" target="_blank">JudisWeb</a> for more AWS Tutorial</p> </div> </body> </html>

CSS Files: style.css

.button {
  display: inline-block;
  padding: 0.5rem 1rem;
  border-radius: 0.1875rem;
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
  background: #3180c2;
  color: #fff;
  border: 0;
  line-height: 1.5;
  font-size: 1rem;
  vertical-align: middle;
  text-shadow: none;
  -webkit-transition: background 0.1s;
  -moz-transition: background 0.1s;
  transition: background 0.1s;
}

.button:hover {
  background: #3e8dce;
  color: #fff;
}

.button:active, .button:focus {
  background: #2c73ae;
  box-shadow: inset -1px -1px 1px rgba(0, 0, 0, 0.15);
  color: #fff;
  outline: 0;
}

.button:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.button:disabled:hover {
  background: #3180c2;
}

Javascript File: alertdate.js

function showDate() {
	var date = new Date();
	var options = { weekday: "long", month: "long", day: "numeric", year: "numeric" };
	date = date.toLocaleString( "en-US", options );
	alert("Today is: " + date);
} 

Save all the files and upload them to S3 Bucket.

Screenshots of 20211026 214646790
Upload static files: HTML, CSS, JS, JPG/WEBP

Open the index.html file via a web browser using the S3 Object URL.

PicsArt 10 26 09.49.26
S3 Object URL

And if it is successful through step by step, the static file that has been successfully uploaded makes S3 Bucket as web hosting.

Screenshots of 20211026 214618229
Test S3 Bucket Web hosting

HTML, JS, CSS, JPG/WEBP static files have been successfully accessed as website pages uploaded to S3 Bucket and publicly accessible.

Conclusion

With Amazon S3, it is one of the best options to make it an alternative cloud-storage, where any file that we upload to the S3 Bucket can be accessed privately or publicly. And besides that, S3 can also be used as web hosting as a storage medium for static images, videos, js, css, html files that can be accessed by the public.

This is a tutorial on how to easily create an AWS S3 Bucket Public/Private Access.

Hope it is useful.




Leave a comment

Your email address will not be published.