Overview
R2BrowseService provides S3-compatible ListObjectsV2, folder creation, and delete operations for Cloudflare R2. The service handles pagination, virtual folder management, and recursive object enumeration.
Located at:
Fiaxe/Services/R2BrowseService.swift:12Type Definition
Methods
listObjects()
Lists objects and virtual folders at a given prefix (one level deep with delimiter).R2Credentials
required
R2 credentials for authentication.
String
default:""
Prefix to filter objects by. Use empty string for root level, or
"folder/" for a specific folder.ListResult containing objects, folders, and pagination info.
Throws: R2BrowseError or URLError if the request fails.
Implementation Details
The method automatically handles pagination by fetching all pages:list-type=2parameterdelimiter=/to create virtual foldersprefixto scope the listingcontinuation-tokenfor pagination
listAllKeys()
Lists every object key that starts with a prefix (fully recursive, no delimiter).R2Credentials
required
R2 credentials for authentication.
String
required
Prefix to filter objects by. All objects starting with this prefix will be returned, including nested objects.
R2BrowseError or URLError if the request fails.
Implementation Details
This method performs a flat listing without the delimiter parameter, allowing recursive enumeration:createFolder()
Creates a virtual folder by putting a zero-byte object with a trailing slash.R2Credentials
required
R2 credentials for authentication.
String
required
Folder key to create. Trailing slash is added automatically if not present. Example:
"documents" or "documents/".R2BrowseError if the creation fails.
Implementation Details
application/x-directory content type.
deleteObject()
Deletes an object by key.R2Credentials
required
R2 credentials for authentication.
String
required
Object key to delete. Can be a file or folder (with trailing slash).
R2BrowseError if the deletion fails.
Implementation Details
Data Types
ListResult
Result from a ListObjectsV2 call.[R2Object]
Array of file objects returned from the
<Contents> elements in the XML response.[R2Object]
Array of virtual folder objects from the
<CommonPrefixes> elements. Only present when using a delimiter.Bool
Whether there are more results available. Always
false after listObjects() completes all pagination.String?
Token for fetching the next page.
nil when all results have been retrieved.R2BrowseError
Errors thrown by the browse service.(Int, String)
HTTP error with status code and response body. Example:
httpError(403, "Access Denied").String
XML parsing error with description.
URL Building
The service includes a private URL builder that correctly handles percent-encoding:XML Parsing
The service includes two custom XML parsers:ListBucketResultParser
Parses the S3 ListObjectsV2 XML response with delimiter:- Extracts
<Contents>elements as file objects - Extracts
<CommonPrefixes>elements as folder objects - Handles
<IsTruncated>and<NextContinuationToken>for pagination - Parses ISO8601 dates with fractional seconds
FlatListParser
Parses the S3 ListObjectsV2 XML response without delimiter:- Extracts only object keys from
<Contents>elements - Used by
listAllKeys()for recursive enumeration - Handles pagination tokens
Usage Examples
Related Services
- AWSV4Signer - Handles AWS Signature V4 request signing
- R2UploadService - Upload files to R2