Skip to main content

Overview

The R2Credentials struct stores authentication credentials and configuration for connecting to a Cloudflare R2 bucket. It provides computed properties for endpoint URLs and methods for generating public URLs for uploaded objects.

Properties

UUID
required
Unique identifier for this credential set
String
required
Cloudflare account ID used to construct the R2 endpoint URL
String
required
R2 access key ID for S3-compatible authentication
String
required
R2 secret access key for S3-compatible authentication
String
required
Name of the R2 bucket to use for storage operations
String?
Optional custom domain for public URLs. If provided, public URLs will use this domain instead of the default R2 endpoint
Bool
Returns true if any required credential fields are empty
URL
The S3-compatible endpoint URL for this R2 account, formatted as:

Methods

publicURL(forKey:)

Constructs the public URL for an uploaded object.
String
required
The object key (path) in the R2 bucket
Returns: URL - The public URL for accessing the object Behavior:
  • If customDomain is set, returns {customDomain}/{key}
  • Otherwise, returns {endpoint}/{bucketName}/{key}

Source Code

R2Credentials.swift

Protocols

  • Sendable - Thread-safe value type
  • Codable - Can be encoded/decoded to/from JSON or other formats
  • Equatable - Can be compared for equality
  • Identifiable - Has a unique id property

Usage Example