Use Cloudflare R2 as storage for microservices
When executing a microservice it is necessary to "call" a file, whether to copy, download, or write to it, it is very important to choose the correct location for this. In on-premises work environments, without users working remotely or from home, the safest and simplest option is to use storage accessible by all devices on the corporate network; however, in scenarios where there are mobile devices that cannot remotely access the corporate network repository, it may be necessary to opt for a secure and public access repository design.
Suppose we want to install an application on all devices regardless of whether they are at home, on the go, or on the office network. To help us meet these types of requirements, there are multiple cloud service options, where Cloudflare acts as a security and performance proxy between users and servers. Its main services (CDN and DDoS protection) make anything on the internet faster, safer, and more reliable.
R2 is its cloud object storage service (similar to Amazon S3) distinguished by its policy of zero egress fees, also allowing free accounts with more than sufficient conditions, even for production environments.
In order to use it, you need to:
- Create an account associated with an email by following the steps in this link: https://developers.cloudflare.com/fundamentals/account/create-account/
- At the end, the associated email must be verified. If you haven't received the email, remember to check your spam folder.
- Once the account is created and activated, R2 must be activated from the user menu/billing/subscriptions. A payment method will be requested for overages, but it's free if certain conditions are met.
R2 free accounts include:
Free | |
---|---|
Storage | 10 GB per month |
Class A operations | 1 million requests per month |
Class B operations | 10 million requests per month |
Egress (data transfer to the Internet) | Free 1 |
Full details available at: https://developers.cloudflare.com/r2/pricing/#free-tier
Upload files
In order to host and utilize uploaded files, you will need to:
- Create a bucket.
- Inside the bucket, there is a button called
Upload
that allows you to upload files.
Establish access methods
Cloudflare allows various access methods like:
For demonstration purposes, in this article we will use Public buckets.
Accessing files from microservices
Each file uploaded to R2 generates a unique link in its properties. You can see it by clicking on the file:
That link allows you to obtain the file from the microservice using, for example, PowerShell's Invoke-WebRequest
cmdlet or simply requesting a microservice created with AI with instructions that include the download URL:
It's always recommended, if not using AI-created microservices, to log milestones during execution so that in case of any error or malfunction, identifying the problem becomes easier: