AWS Storage Blog: Creating a simple public file repository on Amazon S3
This AWS Samples code allows customers to create a simple PUBLIC file repository using Amazon S3 and Amazon CloudFront. This sample code deploys a website and a public files S3 bucket which can be loaded with any files they wish to publish publicly online.
The solution is packaged as an automated deployment via the AWS Serverless Application Model (SAM) CLI.
For this walkthrough, you need to have the following prerequisites:
Download the source code and extract locally: AWS Samples - Public File Browser for Amazon S3
./sam/
directorysam build
sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
SiteName
- Publicly visible title for the site, shown on the top of the page and in the title bar.FilesOpenTabMode
- How do you want the browser to react when a user selects a file in the following interface values. In New Tab
is recommended over In Same Tab
.VisibleStorageClasses
- Comma delimited list of storage classes to show. Recommend keeping this default.CrossOriginRestriction
- Browser security setting, set to *
for first deployment, then see Step 6.Deploy this changeset?
Successfully created/updated stack - [STACK-NAME] in [REGION]
FileBrowserURL
- This URL is for the public web interface. Needed in Step 6.PublicFilesBucket
- The name of the S3 bucket for storing PUBLICLY ACCESSIBLE files that display in the user browser.WebInterfaceAppBucket
- The name of the S3 bucket that stores the code that runs the file browser web interface.CrossOriginRestrictio
n parameter, and input the value from the FileBrowserURL
output in Step 5. For example:\
Parameter CrossOriginRestriction [*]: https://d111111abcdef8.cloudfront.net
This concludes the deployment of the Public File Browser for Amazon S3 web application. AWS SAM CLI uses AWS CloudFormation to orchestrate the deployment of the front-end static website and public file storage bucket. The entire application is deployed.
To add files to the public interface simply move files into the S3 Bucket indicated in the PublicFilesBucket
output from the above SAM Deploy command. The bucket name should start with public-file-browser-files-
followed by a random string.
Note that this solution has Amazon S3 Versioning enabled for all data, which could result in deleted data being retained and incurring costs. See the Security section below for details.
This is an entirely serverless solution. Therefore, costs are directly related to usage, both in data storage and data retrieval by end users. Reference below and consult the S3 and CloudFront pricing pages.
S3 object/prefix lists are ordered lexicographically (UTF-8 byte order). For this to make sense in most use cases the solution automatically switches between two modes:
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:
publicfiles.example.com
) instead of the CloudFront name?See How can I configure CloudFront to serve my content using an alternate domain name over HTTPS?
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’s README.md
file.
As a best practice, the solution enables the following features:
public-file-browser-logging-[...]
public-file-browser-logging-[...]
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.See CONTRIBUTING for more information.
Run the following from the website directory using local-web-server:
> ws -r '/ -> index.html' '/pfb_for_s3/(.*) -> /$1' --log.format dev
The file ./sam/seed_s3_data/website.zip
contains a statically zipped copy of the ./website/
directory. This
zip file is used to automatically load the public-file-browser-website-[...]
bucket with the actual website code
during deployment. Before re-deploying you will need to re-create ./sam/seed_s3_data/website.zip
using the command
below from the root of the repository. You may then follow the DEPLOYMENT guide.
> zip -FS -x "*.DS_Store" -r ./sam/seed_s3_data/website.zip website
This library is licensed under the MIT-0 License. See the LICENSE file.