cmj2002 / r2-dir-list

Directory Listing for Cloudflare R2
MIT License
25 stars 8 forks source link
cloudflare cloudflare-r2 cloudflare-workers directory-listing file-listing

R2 Directory Listing

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.

Usage

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.toml

You should edit:

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.

Demo

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.

How it works

It will only overwrite the response when all of the following conditions are met:

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.