AWS Storage Blog
Creating a simple public file repository on Amazon S3
Organizations often need to store and serve various types of static content, such as data sets, archives, and file repositories, on the web. This content is typically accessed by end-users for research and analysis purposes or general content distribution. However, presenting this content in a user-friendly and easily browsable manner can be a significant challenge, particularly when dealing with large volumes of data, high traffic, or complex file structures.
Previously, organizations had to set up FTP servers or configure web server features like Apache’s DirectoryIndex or Nginx’s Autoindex to create browsable directory listings for their static content. Many have now chosen Amazon Simple Storage Service (S3) as their preferred storage solution for static web content combined with Amazon CloudFront for a highly scalable, cost-optimized, and serverless solution to publish content on the web.
In this post, I walk through deploying a simple web-based file browser solution for publishing files in an Amazon S3 bucket. This approach utilizes S3 and CloudFront to provide a user-friendly interface for browsing your bucket’s contents, eliminating the need to deploy and maintain additional web server infrastructure. I’ll also review ways to quickly move data into the public S3 bucket, customize the solution with your own domain, and tailor the appearance to match your organization’s branding requirements.
Setting up a simple public file repository on Amazon S3: Solution overview and architecture
This solution uses a static website hosted on Amazon S3 fronted by CloudFront. Once deployed, users can upload files they wish to publish on the public internet to a specially configured “public files” S3 general purpose bucket. When users access the URL of the file browser application, the application lists the contents of the S3 bucket and renders the results in a traditional file browser hierarchical format.
- End user loads a web page from CloudFront and Amazon S3.
- After page load, the browser silently retrieves temporary credentials from Amazon Cognito.
- The browser uses the credentials from Cognito to list the files in the public “files” S3 bucket.
- Then, the file list is rendered in the browser as a traditional file browser/directory index.
- Optionally, a user can select one of the listed files to download it through CloudFront and Amazon S3.
Prerequisites
For this walkthrough, you must have the following prerequisites:
- An AWS account
- AWS SAM CLI installed and configured with credentials
- Python version 3.11 installed and in your PATH variable
- Choose an AWS Region where the required services are supported. Most AWS Regions are supported, but consult the availability page for details.
- Amazon CloudFront (including Standard Log support)
- Amazon S3
- Amazon Cognito
- AWS CloudFormation
- AWS Lambda
Setting up a simple public file repository on Amazon S3: Walkthrough
The solution is packaged as an automated deployment through the AWS Serverless Application Model (AWS SAM) command line interface (CLI).
The AWS SAM CLI is an open-source command line tool used to locally build, test, debug, and deploy serverless applications defined with AWS SAM templates.
Download the source code and extract locally: AWS Samples – public-file-browser-for-amazon s3.
Build and deploy the stack
1. In a terminal, navigate to the ./sam/
directory within the downloaded code repository.
2. Run the following command to build and package the project for deployment:
sam build
3. Deploy the SAM template to your account. The wizard guides you through the process of deploying the SAM AWS CloudFormation. Details on this process are found in the SAM build documentation.
a. Run the following command:
sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
b. Enter values for the deployment parameters:
i. Stack Name
: Choose a unique CloudFormation stack name. End users do not see this.
ii. AWS Region
: The supported AWS Region you chose in the Prerequisites section.
iii. SiteName
: Publicly visible title for the site, shown on the top of the page and in the title bar.
iv. FilesOpenTabMode
: How do you want the browser to react when a user selects a file in the following interface values. For a consistent user experience, In New Tab
is recommended over In Same Tab
.
v. VisibleStorageClasses
: Comma delimited list of storage classes to show. Recommend keeping this default.
vi. CrossOriginRestriction
: Browser security setting, set to *
for first deployment, then see Step 6.
c. Select the default inputs for the remaining items with the last prompt before deployment being:
Deploy this changeset?
4. Wait for the deployment to complete. This process takes approximately five minutes with a final prompt stating:
Successfully created/updated stack - [STACK-NAME] in [REGION]
5. Once the deployment completes, note the following entries in the Outputs
section.
a. FileBrowserURL
: This URL is for the public web interface. Needed in Step 6.
b. PublicFilesBucket
: The name of the S3 bucket for storing PUBLICLY ACCESSIBLE files that display in the user browser.
c. WebInterfaceAppBucket
: The name of the S3 bucket that stores the code that runs the file browser web interface.
6. IMPORTANT: Complete Steps 3 and 4 again, keeping all values the same except for the CrossOriginRestriction
parameter, and input the value from the FileBrowserURL
output in Step 5. For example:
Parameter CrossOriginRestriction [*]: https://d111111abcdef8.cloudfront.net
The deployment of the public file browser for Amazon S3 web application is now completed. AWS SAM CLI uses CloudFormation to orchestrate the deployment of the front-end static website and public file storage S3 bucket.
Optionally, you can view the deployed resources by logging in to the AWS Management Console, navigating to the CloudFormation service, selecting the CloudFormation stack name selected in the preceding deployment, and selecting the Resources tab.
Setting up a simple public file repository on Amazon S3: Test the solution
In this testing phase, you upload contents to the configured “public files” S3 bucket deployed as part of this solution. After uploading, you browse the public URL and verify the upload files are listed.
- Log in to the S3 console, navigate to your list of buckets, and select the Amazon S3 bucket that matches the
PublicFilesBucket
output value from Step 5 in the setup instructions. The S3 bucket name should matchpublic-file-browser-files-[...]
. Select the Upload button.
2. Select the files you want to upload to test the solution. By default, S3 objects and resources are private. However, this solution has a specially configured bucket that makes all contents publicly accessible. You can review the remaining options on the page. The default options are acceptable for most use cases and include automatic encryption for stored files. Select Upload at the bottom of the page.
3. Wait for your upload to finish and show a successful status. The length of time to upload is based on your network connection speed and data size for the test files.
4. Select the Close button to return to the S3 bucket contents and verify that the new files appear.
5. Finally, view the public file browser for Amazon S3 web interface by going to the URL in the FileBrowserURL
value from the output section of deployment Step 5. The uploaded files are listed in the web interface.
Next steps
With the public file browser for Amazon S3 solution fully deployed and tested, the next steps are to customize the solution by adding your own files. Optionally, you can move the solution to your DNS domain name, or modify the user interface. See the following resources if interested in further customizing the solution.
How do I move files into the public-file-browser-files-[...]
quickly in bulk?
- Sync from a local system:
- For large uploads:
- If your files are already in a different Amazon S3 bucket:
- How can I copy all objects from one Amazon S3 bucket to another bucket?
- For large Amazon S3 buckets with millions of files, consider using S3 Batch Operations
How do I use this solution with my own DNS domain name (such as publicfiles.example.com) instead of the CloudFront name?
- How can I configure CloudFront to serve my content using an alternate domain name over HTTPS?
- Note that you have to update the Cross-Origin Resource Sharing (CORS) rules in the Amazon S3 configuration of BOTH the files and website S3 buckets.
- See also: Using custom URLs by adding alternate domain names (CNAMEs)
How do I modify the public user interface, such as to add text, change the style, or add my logo?
- The public website files are located in the
public-file-browser-website-[...]
bucket. These files can be downloaded, modified, and re-uploaded containing customizations. Note that CloudFront caches these files, so you must create an invalidation to clear the cache when a file is updated. See How do I remove a cached file from CloudFront? - If you choose to update the files in the
./website/
directory of the source code repository for future deployments, then you must update the./sam/seed_s3_data/website.zip
by following the instructions in the repository’sREADME.md
file.
Additional considerations in security
As a best practice, the solution enables the following features:
- Amazon CloudWatch distribution access logs
- Stored in an S3 bucket:
public-file-browser-logging-[...]
- Stored in an S3 bucket:
- Amazon S3 bucket access logs (static website and uploaded images)
- Stored in an S3 bucket:
public-file-browser-logging-[...]
- Stored in an S3 bucket:
- Amazon S3 Versioning
- This feature is enabled for all S3 buckets, such as the primary
public-file-browser-files-[...]
bucket used to store public files. This means all data written to the S3 bucket is retained as a “previous version”, even if overwritten or deleted. You incur charges for storing previous versions of objects. - If you expect to replace or update files with the same name frequently, then you should configure the S3 bucket with a Lifecycle Configuration to expire noncurrent object versions. See How do I create Amazon S3 lifecycle configuration rules for noncurrent object versions?
- This feature is enabled for all S3 buckets, such as the primary
- Default Encryption-at-Rest for all objects stored in Amazon S3
- Default TLS Version Options (TLSv1)
- To simplify deployment, this solution uses the default CloudFront domain and certificate that limits the available TLS version options to TLSv1. To use another supported TLS version follow the instructions above to use a custom domain, new certificate, and configure the Security Policy on the CloudFront distribution.
Cleaning up
This is a serverless solution, thus the costs are directly related to usage – only a few cents monthly to store the website source code. The persistent cost to maintaining this application when not in use is in the data stored within the website, public files, and logging S3 buckets. Costs are also incurred for users accessing the website, such as CloudFront data transfer and Amazon S3 LIST/GET request costs. Notably there is no charge for Amazon S3 Data Transfer to CloudFront.
To avoid incurring future charges, follow these instructions to delete the resources created by the solution:
- In the Console, navigate to CloudFormation and select the stack name specified in the deployment section of this post.
- Select Delete near the top of the console and select delete again in the pop-up confirmation to begin deleting the stack. This process takes approximately five minutes.
- Deletion of the CloudFormation stack likely fails if there are contents remaining in the website and public files’ S3 buckets. You can choose to delete the stack anyway, thereby retaining these resources, or manually empty these S3 buckets before attempting the delete again.
- Empty and then delete the logging S3 bucket that stores access logs for the application. These are preserved by default for audit purposes. This S3 bucket is formatted as
public-file-browser-logging-[...]
.
Conclusion
In this post I presented a simple web-based file browser solution for publishing files in an Amazon S3 bucket. I also showed ways to quickly move data into the public S3 bucket, customize the browser URL with your own domain, and change the appearance of the solution by customizing the code to match your organization’s branding requirements.
You can use the public file browser for Amazon S3 solution to reduce costs and provide a user-friendly interface for browsing your bucket’s contents. This approach allows for serving large volumes of data, high traffic, and complex file structures while also eliminating the need to deploy and maintain additional web server infrastructure. Additionally the solution’s cost directly scales with its usage so you’re not paying for idle infrastructure.
Each component of this application includes an AWS Free Tier. Use the AWS Free Tier to learn and experiment with new technologies.
More related resources from AWS
- Amazon S3 + Amazon CloudFront: A Match Made in the Cloud
- Amazon S3 Frequently Asked Questions
- Implementing Default Directory Indexes in Amazon S3-backed Amazon CloudFront Origins Using CloudFront Functions
- Implementing Default Directory Indexes in Amazon S3-backed Amazon CloudFront Origins Using Lambda@Edge
- Building Applications with Serverless Architectures