This is a simple Directory Listing script for Cloudflare R2 and hosted on Cloudflare Workers. It is inspired by the Directory Listing of Gitea downloads site.
Clone this repository, install dependencies and edit the configs:
git clone https://github.com/cmj2002/r2-dir-list.git
cd r2-dir-list
npm install
mv src/config.ts.example src/config.ts
mv wrangler.toml.example wrangler.tomlYou should edit:
bucketnameinsrc/config.tsandwrangler.tomlto your bucket name.bucketdomain.example.cominsrc/config.tsandwrangler.tomlto your bucket domain. It must have been set as a custom domain of your Cloudflare R2 bucket.example.cominwrangler.toml'szone_nameto yours.- Other settings like
name,desp,showPoweredByandlegalInfoinsrc/config.tsto your own.
You may want to search bucketdomain, bucketname and example.com in your code to ensure you have edited all of them.
Then you can run wrangler deploy to deploy it to your Cloudflare Workers.
https://datasets.caomingjun.com/
This is a production website of mine, which hosts some machine learning datasets used in my papers and codes to share with other reserchers. It is a Cloudflare R2 bucket with this worker in front of it.
It will only overwrite the response when all of the following conditions are met:
- Response from R2 has a status of 404
- The requested pathname ends with
/ - There exist "subdirectories" or "files" under the current "directory" (The quotation marks here are used because directories and files are abstract concepts in object storage)
In such a case, it will generate a HTML page with the list of "subdirectories" and "files" under the current "directory" and return it. Otherwise, it will just return the response from R2. So putting this worker in front of your R2 bucket will not affect any normal access to your bucket.