Skip to content

MongoExpUser/Cloudflare-R2-Bucket-With-AWS-SDK-JS-V3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare-R2-Bucket-With-AWS-SDK-JS-V3


Create/Deploy, Configure, List and Delete Cloudflare R2 Bucket (Object Storage) with AWS SDK for JavaScript/NodeJS V3.

The script can execute the followings:
  1. Create/deploy R2 buckets specified in the JSON input file.

  2. Put/Add Lifecycle Configuration to the R2 buckets.

  3. List all R2 buckets.

  4. Delete specified R2 buckets.


Diagram Depicting R2 Bucket Information and Settings on Cloudflare R2 Console.

Image description

PURPOSE

  • Normally, R2 bucket can be created/deployed, configured, listed and deleted on the Cloudflare console or via Cloudflare CLI (Wrangler).

  • R2 bucket can also be deployed via AWS SDK in any language of choice, especially for those who:

    1. Are already familiar with AWS SDK and
    2. Want to create, configure, list and delete large number of buckets programatically.
  • This repository contains code for creating/deploying, configuring, listing and deleting cloudflare R2 bucket (object storage) via AWS SDK for JavaScript/NodeJS V3, from any computer.

  • AWS SDK for JavaScript/NodeJS V3 is clean, lightweight, fast and it fully supports the async-await syntax.

  • Also, cloudflare R2 bucket has lower storage cost and no egress fee , which makes it attractive to developers for object storage.

RUNNING the NodeJS script

To run the script, follow these steps:

  1. Install NodeJS and @aws-sdk/client-s3 (v3) module, assuming Ubuntu OS and NodeJS v21.x

    • sudo apt-get -y update
    • sudo apt-get install -y ca-certificates curl gnupg
    • sudo mkdir -p /etc/apt/keyrings
    • curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
    • echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
    • sudo apt-get -y update
    • sudo apt-get install -y nodejs
    • sudo npm install @aws-sdk/client-s3
  2. Download or clone the following files, from this repo, into the current working directory (CWD):

    • NodeJS script: index.js
    • JSON file: config.json
  3. Fill in relevant values in config.json file.

    • References for config.json :
  4. Then run the code, assuming sudo access:

    • sudo node index.js

License

Copyright © 2024. MongoExpUser

Licensed under the MIT license.

About

Create, Configure, List and Delete R2 Bucket

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published