DEV Community

Cover image for Encore Launch Week Day 2: Public Buckets & CDN
Marcus Kohlberg for Encore

Posted on • Originally published at encore.dev

Encore Launch Week Day 2: Public Buckets & CDN

It's Day 2 of Launch Week and we're back with another launch...

Encore.go and Encore.ts now support Public Object Storage Buckets

Today we're launching Public Buckets, an extension of our recently released Object Storage primitive for Encore.go and Encore.ts.

This feature simplifies working with public-facing file storage directly in your application code and adds CDN support for optimal content delivery.

How Object Storage works in Encore

Encore lets you integrate infrastructure as type-safe objects in your application code. Whether you're creating a database, a Pub/Sub topic, or an Object Storage bucket, it's as easy as writing a few lines of code.

Example

To create a bucket in Encore.go you define it as a constant within a service:

package user

import "encore.dev/storage/objects"

var ProfilePictures = objects.NewBucket("profile-pictures", objects.BucketConfig{
    Versioned: false,
})
Enter fullscreen mode Exit fullscreen mode

That's it! Encore sets up the infrastructure automatically when you run your app locally.

For manual cloud deployments, you just provide a runtime configuration and Encore handles the rest.

Using Public Buckets for simple file sharing

Encore now supports creating public buckets where objects can be accessed directly via HTTP/HTTPS without authentication.
This is useful for serving static assets like images, videos, or other public files.

To create a public bucket, set Public: true in the BucketConfig.

Here's how to do it in Encore.go:

var PublicAssets = objects.NewBucket("public-assets", objects.BucketConfig{
    Public: true,
})
Enter fullscreen mode Exit fullscreen mode

Once configured as public, you can get the public URL for any object using the PublicURL method:

// Get the public URL for an object
url := PublicAssets.PublicURL("path/to/image.jpg")

// The URL can be used directly or shared publicly
fmt.Println(url) // e.g. https://assets.example.com/path/to/image.jpg
Enter fullscreen mode Exit fullscreen mode

For manual cloud deployments, you just need to provide a provide a base url in the runtime configuration, and Encore handles the rest.

When deploying with Encore Cloud it will automatically configure the bucket to be publicly accessible and configure CDN for optimal content delivery, using:

With Public Buckets, Encore makes managing object storage and public file access simple, powerful, and fully type-safe.

Simple testing in the Development Dashboard

Encore's Development Dashboard makes testing very simple.
Call your endpoints, such as the list endpoint, to view and trace requests and get detailed insights.
Traces provide insights including file uploads and bucket queries, helping you debug and optimize.

Encore Cloud now supports Cloudflare R2

Encore Cloud now supports Cloudflare R2 as an object storage provider. R2 can often be a cheaper alternative to AWS S3.
This is great for anyone looking to save costs on object storage, while getting access to a global CDN.

Cloudflare R2 configuration

Join the Live stream

Watch today's live stream at 14:00 CET for an in-depth walkthrough and a live Q&A session.

What's next

  • 💿 Install Encore to try it yourself:
    • macOS: brew install encoredev/tap/encore
    • Linux: curl -L https://encore.dev/install.sh | bash
    • Windows: iwr https://encore.dev/install.ps1 | iex
  • 🌟 Star the project on Github
  • ❤️ Join our Developer Community to ask questions.

Top comments (0)