Create the GET endpoint
Import the APIRoute type from "astro" and create a GET endpoint that accepts the locals parameter.
This endpoint will serve individual files from the bucket based on the file key provided in the URL query parameters.
Get the file key from the request
Extract the file key from the URL query parameters using url.searchParams.get("key"). The key represents the file name or path within the bucket.
Validate that a key is provided - if missing, return a 400 Bad Request error with a descriptive message to help with debugging.
Retrieve the object from the bucket
Access the CLOUD_FILES binding and use the .get() method to retrieve the specific file from the bucket using the provided key.
The method returns an R2Object if the file exists, or null if not found.
Key concepts:
- Bucket access: The
CLOUD_FILESbinding provides direct access to the bucket - Object retrieval: The
.get()method is the primary way to fetch individual files
Extract data and metadata from the object
Convert the R2Object to a usable format using the .arrayBuffer() method, which returns the file data as a Uint8Array. Extract the content type from the object's httpMetadata property, with a fallback to an empty string if not available.
Return the file with proper headers
Create a new Response object with the file data and set the Content-Type header to match the file's content type. This ensures browsers and clients can properly handle the file based on its type.