Amazon S3 is the storage service provided by Amazon. It is one of the services from AWS which is widely used with minimum cost. In this article, let us understand S3 and how I used S3 in my project along with a simple demo.
In case you are someone new to AWS then take a look at this article to know what AWS is and what solution they offer.
- Amazon S3 – Overview
- Websites
- CORS
- DEMO
- Python Code to upload and download files
- Conclusion
Amazon S3 – Overview
Amazon S3 stands for Simple Storage Services. It allows people to store objects (files) in buckets (directories). Buckets must have a globally unique name. Even though S3 is a global service, buckets are defined at the regional level. Every object on S3 is stored as a Key. The full path looks like this,
s3://my-bucket-name/directory1/my_file.json.
Also, keep in mind that there is no concept of “directories”. Think that, keys are very long names that contain slashes.
According to documentation, “Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, and inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.”
Websites
When I started to learn AWS S3, I was thrilled to know that S3 can act as a static website. This allowed me to frontend languages like angular and host them in S3 which was fun. For doing so, we need to make the S3 bucket public so that the objects on the bucket are accessible to the public.
CORS
During one of my projects I created a static page using an S3 bucket and tried to access files from another S3 bucket which was also public. Later I found out that it was because of CORS. CORS stands for Cross Origin Resource Sharing. The browsers like google chrome, firefox are responsive for restricting these kinds of resource sharing. So we need to enable CORS on the S3 bucket acknowledging the browser to share its resources.
DEMO
Lets us how to create an S3 bucket and how to add and delete objects from it.
Open the S3 console and click on create a bucket.
Enter the bucket name and select the region whichever is near you. Also, in case you are planning to use this S3 bucket as a static website make sure to uncheck the “block all public access”. Scroll down and click “Create bucket”.
Now click on the bucket you created and click on the Upload button.
Click on add files and upload your files. Also if you want that object to be public click on Permissions and click on “Grant public-read access” and finally click “Upload”. Now you have successfully uploaded your S3 objects.
If you want to delete it, select the object and click delete. Enter Permanently delete and click delete objects
Your object is successfully deleted!!
Python Code to upload and download files
import boto3
s3_client = boto3.client('s3')
open('hello.txt').write('Hello, world!')
# Upload the file to S3
s3_client.upload_file('hello.txt', 'MyBucket', 'hello-remote.txt')
# Download the file from S3
s3_client.download_file('MyBucket', 'hello-remote.txt', 'hello2.txt')
print(open('hello2.txt').read())
Conclusion
Amazon S3 is one of those services which will be used by most people using AWS. And why shouldn’t they when something like this is offered at very low? Start using this service and save your time on building something similar to this.
Happy Programming!!
2 thoughts on “What is Amazon S3 and how to get started?”