Get startedGet started for free

Sharing files through a website

1. Sharing files through a website

In the last lessons, we learned how to access public and private files in S3 for use in our own scripts and for sharing with others. In this lesson, we will cover how to share files through a website.

2. Serving HTML Pages

S3 is able to serve HTML pages. This becomes useful when we want to share results of an analysis with management, and update the results via a pipeline. But Wait! Don't run away. This is not going to turn into a web dev bootcamp. Many python data science tools have the ability to generate simple HTML from data. The goal is not to have us making beautiful websites, but to share our analyses with the world.

3. HTML table in Pandas

You are familiar with the pandas to_csv method. Pandas has another method - to_html. We can use it to write a pandas DataFrame to an HTML file.

4. HTML Table in Pandas with links

The optional render_links argument converts any URLs to be clickable.

5. Certain columns to HTML

We can use the columns parameter to pass a list of only columns we want rendered. In this example, we are only showing service_name and info_link columns.

6. Borders

We also have some basic border controls via the border argument. 0 value will remove the border, while a value of 1 will show it. There are a few more configuration arguments, but you can look through them on the documentation for pandas.to_html

7. Uploading an HTML file to S3

Uploading an html file is like uploading any other file, but we need to set the ContentType so that the browser knows to treat it as HTML. We use the upload_file method to upload it to S3. We specify the local filename and bucket. We will upload this file with the key "table.html". In the ExtraArgs dictionary, we specify the ContentType as 'text/html'. Finally, we set the ACL to public-read.

8. Accessing HTML file

We can use the same pattern we learned in lesson 1 for accessing public files to access table.html.

9. HTML Page

Because we set the content type as text/html, the browser knows to show it as HTML. And here's our table! HTML files are treated just like regular files. We can also make this page private and provide a pre-signed URL for temporary access!

10. Uploading other types of content

Uploading images - such as the ones generated from Python's charting libraries - is very similar to uploading HTML files, but if we want the browser to render them, we need to set the proper contentType during upload. In the case of a PNG, we use image/png as the contentType.

11. IANA Media Types

IANA has a list of all file types based on their file extension. Some common ones are JSON, PNG, PDF, and CSV. You can find more on IANA's website below.

12. Generating an index page

So what if we have multiple HTML and image files that we want to share, and we want to generate a listing for them? Well - we can do that with the tools we already learned. We list the objects in the bucket using client_list_objects. We convert the response contents into a DataFrame.

13. Generating an index page

We create a column "Link" where we create the URL for every key in the object. Then, we write the DataFrame to HTML!

14. Uploading index page

We upload the index page we generated like we would any other HTML file! It's now available at the Public URL

15. Review

We learned how to generate an HTML table in pandas. How to upload the HTML file to S3 We learned how to upload an image file to S3 We learned how to share the URL for our HTML page

16. Let's practice!

Now that we know how to share files with the world through an S3 Bucket website, let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.