### SDK Installation Commands Source: https://developers.cloudflare.com/r2/data-catalog/config-examples/spark-scala These commands demonstrate how to install Java 17, Spark 3.5.3, and sbt 1.10.11 using SDKMAN, which are prerequisites for the example. ```bash sdk install java 17.0.14-amzn sdk install spark 3.5.3 sdk install sbt 1.10.11 ``` -------------------------------- ### Example Presigned URLs Source: https://developers.cloudflare.com/r2/examples/aws/boto3 Illustrative examples of presigned URLs generated for GET and PUT operations, showing the structure including authentication parameters. ```text https://.r2.cloudflarestorage.com/my-bucket/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...&X-Amz-Date=&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature= https://.r2.cloudflarestorage.com/my-bucket/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...&X-Amz-Date=&X-Amz-Expires=3600&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Signature= ``` -------------------------------- ### Python SDK Example Source: https://developers.cloudflare.com/r2/api/s3/temporary-credentials Example of using temporary credentials with the `boto3` library in Python. ```APIDOC ## Python SDK Example ### Description This example shows how to use temporary credentials with the `boto3` library in Python to interact with R2. ### Code ```python import boto3 s3 = boto3.client( service_name="s3", endpoint_url="https://.r2.cloudflarestorage.com", aws_access_key_id="", aws_secret_access_key="", aws_session_token="", region_name="auto", ) ``` ``` -------------------------------- ### Install Dependencies and Run Script Source: https://developers.cloudflare.com/r2-sql/tutorials/end-to-end-pipeline Install the necessary Python package and execute the data generation script. ```shell pip install requests python fraud_data_generator.py ``` -------------------------------- ### Example Presigned URLs Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-js-v3/index.md These are example formats of presigned URLs generated for GET and PUT operations, showing the structure and parameters involved. ```url https://my-bucket..r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=&X-Amz-Date=&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=&x-id=GetObject https://my-bucket..r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=&X-Amz-Date=&X-Amz-Expires=3600&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Signature=&x-id=PutObject ``` -------------------------------- ### Install s3mini Source: https://developers.cloudflare.com/r2/examples/aws/s3mini/index.md Install the s3mini package using npm. This is the first step before using s3mini in your project. ```bash npm install s3mini ``` -------------------------------- ### JavaScript SDK Example Source: https://developers.cloudflare.com/r2/api/s3/temporary-credentials Example of using temporary credentials with the `aws4fetch` library in JavaScript. ```APIDOC ## JavaScript SDK Example ### Description This example demonstrates how to use temporary credentials with the `aws4fetch` library in JavaScript to interact with R2. ### Code ```javascript import { AwsClient } from "aws4fetch"; const R2_URL = `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`; const client = new AwsClient({ accessKeyId: ACCESS_KEY_ID, secretAccessKey: SECRET_ACCESS_KEY, sessionToken: SESSION_TOKEN, service: "s3", }); const response = await client.fetch(`${R2_URL}/my-bucket/image.png`); ``` ``` -------------------------------- ### Example Presigned URL for GetObject Source: https://developers.cloudflare.com/r2/api/s3/presigned-urls This is an example of a presigned URL generated for a GET operation on an object. It includes all necessary signature parameters for authentication and authorization. ```url https://my-bucket.123456789abcdef0123456789abcdef.r2.cloudflarestorage.com/photos/cat.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=CFEXAMPLEKEY12345%2F20251201%2Fauto%2Fs3%2Faws4_request&X-Amz-Date=20251201T180512Z&X-Amz-Expires=3600&X-Amz-Signature=8c3ac40fa6c83d64b4516e0c9e5fa94c998bb79131be9ddadf90cefc5ec31033&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject ``` -------------------------------- ### Install unpdf library Source: https://developers.cloudflare.com/r2/tutorials/summarize-pdf Install the unpdf library using npm, yarn, pnpm, or bun. ```bash npm i unpdf ``` ```bash yarn add unpdf ``` ```bash pnpm add unpdf ``` ```bash bun add unpdf ``` -------------------------------- ### DuckDB CLI Setup and Usage for R2 Data Catalog Source: https://developers.cloudflare.com/r2/data-catalog/config-examples/duckdb/index.md This snippet demonstrates the DuckDB CLI commands required to connect to R2 Data Catalog. It includes installing and loading necessary extensions, creating a secret for R2 credentials, attaching the catalog, creating a schema, creating and populating an Iceberg table, and querying the table. ```sql INSTALL iceberg; LOAD iceberg; INSTALL httpfs; LOAD httpfs; CREATE SECRET r2_secret ( TYPE ICEBERG, TOKEN '' ); ATTACH '' AS my_r2_catalog ( TYPE ICEBERG, ENDPOINT '' ); CREATE SCHEMA my_r2_catalog.default; USE my_r2_catalog.default; CREATE TABLE my_iceberg_table AS SELECT a FROM range(4) t(a); SHOW ALL TABLES; SELECT * FROM my_r2_catalog.default.my_iceberg_table; ``` -------------------------------- ### Install boto3 for Python Source: https://developers.cloudflare.com/r2/examples/authenticate-r2-auth-tokens Install the boto3 library, the Amazon Web Services (AWS) SDK for Python, to interact with R2. ```bash pip install boto3 ``` -------------------------------- ### Example Presigned URLs Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-js Examples of generated presigned URLs for both `getObject` and `putObject` operations, illustrating the structure and parameters. ```text https://my-bucket..r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=&X-Amz-Date=&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature= https://my-bucket..r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=&X-Amz-Date=&X-Amz-Expires=3600&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Signature= ``` -------------------------------- ### Setup Pipeline with Wrangler CLI Source: https://developers.cloudflare.com/r2-sql/get-started/index.md Use the Wrangler CLI to interactively set up a pipeline, defining schema, sink, and SQL transformation. This command initiates the setup process in your terminal. ```bash npx wrangler pipelines setup ``` -------------------------------- ### Start Development Server Source: https://developers.cloudflare.com/r2/tutorials/summarize-pdf Restart your local development server to test the changes made to your Worker. ```bash npm run dev ``` -------------------------------- ### Generic Wrangler R2 SQL Query Example Source: https://developers.cloudflare.com/r2-sql/llms-full.txt A general example of the `wrangler r2 sql query` command, showing placeholders for the warehouse and the SQL query itself. ```bash npx wrangler r2 sql query "SELECT * FROM namespace.table_name limit 10;" ``` -------------------------------- ### Install Wrangler with bun Source: https://developers.cloudflare.com/r2/get-started/cli Installs the Wrangler CLI as a development dependency in your project using bun. ```bash bun add -d wrangler ``` -------------------------------- ### Example Presigned URLs Source: https://developers.cloudflare.com/r2/examples/aws/aws4fetch/index.md Illustrative examples of the structure of generated presigned URLs for download and upload operations. ```text https://.r2.cloudflarestorage.com/my-bucket/dog.png?X-Amz-Expires=3600&X-Amz-Date=&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=&X-Amz-SignedHeaders=host&X-Amz-Signature= https://.r2.cloudflarestorage.com/my-bucket/dog.png?X-Amz-Expires=3600&X-Amz-Date=&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Signature= ``` -------------------------------- ### List Buckets Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-js-v3 Example of how to list all buckets associated with your R2 account using the `ListBucketsCommand`. ```APIDOC ## List Buckets This operation lists all buckets in your R2 account. ### Method ```javascript S3.send(new ListBucketsCommand({})) ``` ### Request Example ```javascript console.log(await S3.send(new ListBucketsCommand({}))); ``` ### Response Example ```json { "$metadata": { "httpStatusCode": 200, "requestId": undefined, "extendedRequestId": undefined, "cfId": undefined, "attempts": 1, "totalRetryDelay": 0 }, "Buckets": [ { "Name": "user-uploads", "CreationDate": "2022-04-13T21:23:47.102Z" }, { "Name": "my-bucket", "CreationDate": "2022-05-07T02:46:49.218Z" } ], "Owner": { "DisplayName": "...", "ID": "..." } } ``` ``` -------------------------------- ### Install Development Tools with SDKMAN Source: https://developers.cloudflare.com/r2/data-catalog/config-examples/spark-scala/index.md This command sequence uses SDKMAN to install specific versions of Java, Spark, and sbt, ensuring a compatible development environment. ```bash sdk install java 17.0.14-amzn sdk install spark 3.5.3 sdk install sbt 1.10.11 ``` -------------------------------- ### Example: Create restricted R2 temporary credentials Source: https://developers.cloudflare.com/r2/examples/authenticate-r2-temp-credentials This example demonstrates creating temporary credentials with specific actions (`GetObject`, `HeadObject`), a 15-minute TTL, and restricted access to the `data/` prefix. ```typescript import { createTempCredentials } from "./temp-credentials"; const R2_URL = `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`; const creds = await createTempCredentials( R2_URL, ACCOUNT_ID, PARENT_ACCESS_KEY_ID, PARENT_SECRET_ACCESS_KEY, "my-bucket", { scope: "object-read-only", actions: ["GetObject", "HeadObject"], ttlSeconds: 900, paths: { prefixPaths: ["data/"] }, }, ); ``` -------------------------------- ### Example Audit Log Entry for Bucket Creation Source: https://developers.cloudflare.com/r2/platform/audit-logs/index.md This is an example of an audit log entry showing the creation of a new bucket. It details the action, actor, resource, and timestamp of the event. ```json { "action": { "info": "CreateBucket", "result": true, "type": "create" }, "actor": { "email": "", "id": "3f7b730e625b975bc1231234cfbec091", "ip": "fe32:43ed:12b5:526::1d2:13", "type": "user" }, "id": "5eaeb6be-1234-406a-87ab-1971adc1234c", "interface": "API", "metadata": { "zone_name": "r2.cloudflarestorage.com" }, "newValue": "", "newValueJson": {}, "oldValue": "", "oldValueJson": {}, "owner": { "id": "1234d848c0b9e484dfc37ec392b5fa8a" }, "resource": { "id": "my-bucket", "type": "r2.bucket" }, "when": "2024-07-15T16:32:52.412Z" } ``` -------------------------------- ### Start Local Worker Development Server Source: https://developers.cloudflare.com/r2/get-started/workers-api/index.md Run this command to start the local development server for your Worker. By default, it uses a local R2 simulation. ```bash npx wrangler dev ``` -------------------------------- ### Example: Scoped Credentials for 15 Minutes Source: https://developers.cloudflare.com/r2/examples/authenticate-r2-temp-credentials/index.md This example demonstrates creating temporary credentials valid for 15 minutes, restricted to 'GetObject' and 'HeadObject' actions on objects within the 'data/' prefix. ```typescript import { createTempCredentials } from "./temp-credentials"; const R2_URL = `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`; const creds = await createTempCredentials( R2_URL, ACCOUNT_ID, PARENT_ACCESS_KEY_ID, PARENT_SECRET_ACCESS_KEY, "my-bucket", { scope: "object-read-only", actions: ["GetObject", "HeadObject"], ttlSeconds: 900, paths: { prefixPaths: ["data/"] }, }, ); ``` -------------------------------- ### Navigate to Project Directory Source: https://developers.cloudflare.com/r2/tutorials/summarize-pdf After creating the project, navigate into the newly created project directory to continue with the setup. ```bash cd pdf-summarizer ``` -------------------------------- ### Navigate to the application directory Source: https://developers.cloudflare.com/r2/api/workers/workers-api-usage After creating the application, change into the new project directory. ```bash cd r2-worker ``` -------------------------------- ### Basic SELECT Examples Source: https://developers.cloudflare.com/r2-sql/sql-reference Illustrates selecting all columns, specific columns, and columns with calculated values and aliases. ```sql SELECT * FROM my_namespace.sales_data LIMIT 10 ``` ```sql SELECT customer_id, region, total_amount FROM my_namespace.sales_data LIMIT 10 ``` ```sql SELECT region, total_amount * 1.1 AS total_with_tax FROM my_namespace.sales_data LIMIT 10 ``` -------------------------------- ### Download File using Presigned GET URL with cURL Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-kotlin Example of downloading a file from R2 using a presigned GET URL generated by the AWS SDK for Kotlin. Requires cURL. ```bash curl -X GET "https://" -o downloaded-file.txt ``` -------------------------------- ### Node.js/Bun Usage with s3mini Source: https://developers.cloudflare.com/r2/examples/aws/s3mini/index.md Demonstrates how to initialize and use s3mini in a Node.js or Bun environment. Includes examples for uploading, downloading, listing, and deleting objects. ```typescript import { S3mini } from "s3mini"; const s3 = new S3mini({ accessKeyId: process.env.R2_ACCESS_KEY_ID!, secretAccessKey: process.env.R2_SECRET_ACCESS_KEY!, // Bucket-scoped endpoint — include your bucket name in the path endpoint: `https://${process.env.ACCOUNT_ID}.r2.cloudflarestorage.com/my-bucket`, region: "auto", }); // Upload an object await s3.putObject("hello.txt", "Hello from s3mini!"); // Download an object as a string const text = await s3.getObject("hello.txt"); console.log(text); // List objects with a prefix const objects = await s3.listObjects("/", "hello"); console.log(objects); // Delete an object await s3.deleteObject("hello.txt"); ``` -------------------------------- ### Download using a GET presigned URL Source: https://developers.cloudflare.com/r2/api/s3/presigned-urls Example of downloading an object using a presigned URL with curl. ```APIDOC ## Download using a GET presigned URL ### Description Use a presigned URL with the `GET` method to download an object from an R2 bucket. ### Method GET ### Endpoint `https://my-bucket..r2.cloudflarestorage.com/object-key?X-Amz-Algorithm=...` ### Request Example ```bash curl "https://my-bucket..r2.cloudflarestorage.com/image.png?X-Amz-Algorithm=..." ``` ``` -------------------------------- ### Generate Presigned URL (AWS CLI) Source: https://developers.cloudflare.com/r2/api/s3/presigned-urls Example using the AWS CLI to generate a presigned URL for a GET operation. ```APIDOC ## Generate Presigned URL (AWS CLI) ### Description Generate a presigned URL for a GET operation using the AWS CLI. ### Prerequisites - Account ID - AWS CLI installed and configured ### Command Example ```bash # Generate presigned URL for reading (GET) # The AWS CLI presign command defaults to GET operations aws s3 presign --endpoint-url https://.r2.cloudflarestorage.com \ s3://my-bucket/image.png \ --expires-in 3600 # Output: # https://.r2.cloudflarestorage.com/my-bucket/image.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=... # Note: The AWS CLI presign command only supports GET operations. # For PUT operations, use one of the SDK examples above. ``` ``` -------------------------------- ### Change Directory Source: https://developers.cloudflare.com/r2/data-catalog/get-started/index.md Navigate into the newly created 'r2-data-catalog-notebook' directory to begin setting up your project. ```bash cd r2-data-catalog-notebook ``` -------------------------------- ### Generate Presigned URL (Python) Source: https://developers.cloudflare.com/r2/api/s3/presigned-urls Examples using the Boto3 library for Python to generate presigned URLs for GET and PUT operations. ```APIDOC ## Generate Presigned URL (Python) ### Description Generate presigned URLs for reading (GET) and writing (PUT) objects to R2 using the Boto3 library for Python. ### Prerequisites - Account ID - R2 API token (Access Key ID and Secret Access Key) - Boto3 library installed ### Code Example ```python import boto3 s3 = boto3.client( service_name="s3", endpoint_url='https://.r2.cloudflarestorage.com', aws_access_key_id='', aws_secret_access_key='', region_name="auto", # Required by SDK but not used by R2 ) # Generate presigned URL for reading (GET) get_url = s3.generate_presigned_url( 'get_object', Params={'Bucket': 'my-bucket', 'Key': 'image.png'}, ExpiresIn=3600 # Valid for 1 hour ) # https://my-bucket..r2.cloudflarestorage.com/image.png?X-Amz-Algorithm=... # Generate presigned URL for writing (PUT) # Specify ContentType to restrict uploads to a specific file type put_url = s3.generate_presigned_url( 'put_object', Params={ 'Bucket': 'my-bucket', 'Key': 'image.png', 'ContentType': 'image/png' }, ExpiresIn=3600 ) ``` ``` -------------------------------- ### Interact with Cloudflare R2 using AWS SDK for Java Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-java/index.md This example shows how to initialize the CloudflareR2Client, list available buckets, upload an object, and list objects in a specific bucket. Ensure you replace placeholder values with your actual account credentials and bucket name. ```java import com.cloudflare.r2.CloudflareR2Client; import com.cloudflare.r2.S3Config; public class R2Example { public static void main(String[] args) { S3Config config = new S3Config( "", "", "" ); CloudflareR2Client r2Client = new CloudflareR2Client(config); // List buckets System.out.println("Available buckets:"); r2Client.listBuckets().forEach(bucket -> System.out.println("* " + bucket.name()) ); // Upload an object to a bucket String bucketName = "demos"; r2Client.putObject(bucketName, "example.txt", "Hello, R2!"); System.out.println("Uploaded example.txt to bucket '" + bucketName + "'"); // List objects in a specific bucket System.out.println("\nObjects in bucket '" + bucketName + "':"); r2Client.listObjects(bucketName).forEach(object -> System.out.printf("* %s (size: %d bytes, modified: %s)%n", object.key(), object.size(), object.lastModified()) ); } } ``` -------------------------------- ### Generate Presigned URL (JavaScript) Source: https://developers.cloudflare.com/r2/api/s3/presigned-urls Examples using the AWS SDK for JavaScript to generate presigned URLs for GET and PUT operations. ```APIDOC ## Generate Presigned URL (JavaScript) ### Description Generate presigned URLs for reading (GET) and writing (PUT) objects to R2 using the AWS SDK for JavaScript. ### Prerequisites - Account ID - R2 API token (Access Key ID and Secret Access Key) - AWS SDK for JavaScript installed ### Code Example ```javascript import { S3Client, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"; import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; const S3 = new S3Client({ region: "auto", // Required by SDK but not used by R2 endpoint: `https://.r2.cloudflarestorage.com`, credentials: { accessKeyId: '', secretAccessKey: '', }, }); // Generate presigned URL for reading (GET) const getUrl = await getSignedUrl( S3, new GetObjectCommand({ Bucket: "my-bucket", Key: "image.png" }), { expiresIn: 3600 } // Valid for 1 hour ); // https://my-bucket..r2.cloudflarestorage.com/image.png?X-Amz-Algorithm=... // Generate presigned URL for writing (PUT) // Specify ContentType to restrict uploads to a specific file type const putUrl = await getSignedUrl( S3, new PutObjectCommand({ Bucket: "my-bucket", Key: "image.png", ContentType: "image/png", }), { expiresIn: 3600 } ); ``` ``` -------------------------------- ### Node.js/Bun Example: Basic S3 Operations Source: https://developers.cloudflare.com/r2/examples/aws/s3mini Demonstrates common S3 operations using s3mini in a Node.js or Bun environment. Ensure environment variables for access keys, account ID, and bucket name are set. ```typescript import { S3mini } from "s3mini"; const s3 = new S3mini({ accessKeyId: process.env.R2_ACCESS_KEY_ID!, secretAccessKey: process.env.R2_SECRET_ACCESS_KEY!, // Bucket-scoped endpoint — include your bucket name in the path endpoint: `https://${process.env.ACCOUNT_ID}.r2.cloudflarestorage.com/my-bucket`, region: "auto", }); // Upload an object await s3.putObject("hello.txt", "Hello from s3mini!"); // Download an object as a string const text = await s3.getObject("hello.txt"); console.log(text); // List objects with a prefix const objects = await s3.listObjects("/", "hello"); console.log(objects); // Delete an object await s3.deleteObject("hello.txt"); ``` -------------------------------- ### Cloudflare Workers Integration with s3mini Source: https://developers.cloudflare.com/r2/examples/aws/s3mini/index.md Shows how to integrate s3mini within a Cloudflare Worker. This example handles GET, PUT, and DELETE requests to interact with R2 objects. ```typescript import { S3mini } from "s3mini"; interface Env { R2_ACCESS_KEY_ID: string; R2_SECRET_ACCESS_KEY: string; ACCOUNT_ID: string; } export default { async fetch(request: Request, env: Env): Promise { const s3 = new S3mini({ accessKeyId: env.R2_ACCESS_KEY_ID, secretAccessKey: env.R2_SECRET_ACCESS_KEY, endpoint: `https://${env.ACCOUNT_ID}.r2.cloudflarestorage.com/my-bucket`, region: "auto", }); const url = new URL(request.url); const key = url.pathname.slice(1); // strip leading "/" if (!key) { return new Response("Missing object key", { status: 400 }); } switch (request.method) { case "PUT": { const data = await request.arrayBuffer(); const contentType = request.headers.get("content-type") ?? "application/octet-stream"; await s3.putObject(key, new Uint8Array(data), contentType); return new Response("Created", { status: 201 }); } case "GET": { const response = await s3.getObjectResponse(key); if (!response) { return new Response("Not Found", { status: 404 }); } return new Response(response.body, { headers: { "content-type": response.headers.get("content-type") ?? "application/octet-stream", etag: response.headers.get("etag") ?? "", }, }); } case "DELETE": { await s3.deleteObject(key); return new Response(null, { status: 204 }); } default: return new Response("Method Not Allowed", { status: 405 }); } }, }; ``` -------------------------------- ### Generate Presigned URL for GET Object Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-net Create a temporary, signed URL for accessing an object using `GetPreSignedUrl`. This example generates a presigned URL for retrieving 'file.txt' with a 7-day expiration. ```csharp static string? GeneratePresignedUrl() { AWSConfigsS3.UseSignatureVersion4 = true; var presign = new GetPreSignedUrlRequest { BucketName = "my-bucket", Key = "file.txt", Verb = HttpVerb.GET, Expires = DateTime.Now.AddDays(7), }; var presignedUrl = s3Client.GetPreSignedURL(presign); Console.WriteLine(presignedUrl); return presignedUrl; } ``` ```text https://.r2.cloudflarestorage.com/my-bucket/file.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=&X-Amz-Date=&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature= ``` -------------------------------- ### Show Databases Command Source: https://developers.cloudflare.com/r2-sql/sql-reference Lists all available namespaces (databases) in R2 SQL. ```sql SHOW DATABASES; ``` -------------------------------- ### Enable Sippy via Wrangler CLI Source: https://developers.cloudflare.com/r2/data-migration/sippy Enables Sippy on your R2 bucket using the Wrangler CLI. This command will prompt for object storage provider details and guide you through the setup process. ```bash npx wrangler r2 bucket sippy enable ``` -------------------------------- ### Navigate to Project Directory Source: https://developers.cloudflare.com/r2/tutorials/summarize-pdf/index.md Change the current directory to the newly created 'pdf-summarizer' project folder to begin development. ```bash cd pdf-summarizer ``` -------------------------------- ### Find first regex match position with regexp_instr Source: https://developers.cloudflare.com/r2-sql/sql-reference/scalar-functions/index.md Use `regexp_instr` to get the starting position of the first substring that matches a regular expression pattern. Filter out null values for reliable results. ```sql SELECT department, regexp_instr(department, '[0-9]') AS digit_pos FROM my_namespace.sales_data WHERE department IS NOT NULL LIMIT 5 ``` -------------------------------- ### Run Python R2 example Source: https://developers.cloudflare.com/r2/get-started/s3 Execute the Python script 'example.py' to perform S3 operations on your R2 bucket. ```bash python example.py ``` -------------------------------- ### R2 Bucket Operations in Standard Worker (JavaScript) Source: https://developers.cloudflare.com/r2/api/workers/workers-api-usage Perform PUT, GET, and DELETE operations on R2 objects within a standard Worker. This example uses the default export function for handling requests. ```javascript export default { async fetch(request, env) { const url = new URL(request.url); const key = url.pathname.slice(1); switch (request.method) { case "PUT": { await this.env.R2.put(key, request.body, { onlyIf: request.headers, httpMetadata: request.headers, }); return new Response(`Put ${key} successfully!`); } case "GET": { const object = await this.env.R2.get(key, { onlyIf: request.headers, range: request.headers, }); if (object === null) { return new Response("Object Not Found", { status: 404 }); } const headers = new Headers(); object.writeHttpMetadata(headers); headers.set("etag", object.httpEtag); // When no body is present, preconditions have failed return new Response("body" in object ? object.body : undefined, { status: "body" in object ? 200 : 412, headers, }); } case "DELETE": { await this.env.R2.delete(key); return new Response("Deleted!"); } default: return new Response("Method Not Allowed", { status: 405, headers: { Allow: "PUT, GET, DELETE", }, }); } } } ``` -------------------------------- ### R2 Bucket Operations in Python Worker Source: https://developers.cloudflare.com/r2/api/workers/workers-api-usage Handle PUT, GET, and DELETE requests for R2 objects using Python in a Cloudflare Worker. This example demonstrates accessing the R2 binding and handling request methods. ```python from workers import WorkerEntrypoint, Response from urllib.parse import urlparse class Default(WorkerEntrypoint): async def fetch(self, request): url = urlparse(request.url) key = url.path[1:] if request.method == "PUT": await self.env.R2.put( key, request.body, onlyIf=request.headers, httpMetadata=request.headers, ) return Response(f"Put {key} successfully!") elif request.method == "GET": obj = await self.env.R2.get( key, onlyIf=request.headers, range=request.headers, ) if obj is None: return Response("Object Not Found", status=404) # When no body is present, preconditions have failed body = obj.body if hasattr(obj, "body") else None status = 200 if hasattr(obj, "body") else 412 headers = {"etag": obj.httpEtag} return Response(body, status=status, headers=headers) elif request.method == "DELETE": await self.env.R2.delete(key) return Response("Deleted!") else: return Response( "Method Not Allowed", status=405, headers={"Allow": "PUT, GET, DELETE"}, ) ``` -------------------------------- ### Show Tables Command Source: https://developers.cloudflare.com/r2-sql/sql-reference Lists all tables within a specified namespace. ```sql SHOW TABLES IN namespace_name; ``` -------------------------------- ### Cloudflare Workers Example: S3 Operations via Fetch Source: https://developers.cloudflare.com/r2/examples/aws/s3mini Implements a Cloudflare Worker to handle PUT, GET, and DELETE requests for S3 objects using s3mini. It requires environment variables for credentials and account ID. ```typescript import { S3mini } from "s3mini"; interface Env { R2_ACCESS_KEY_ID: string; R2_SECRET_ACCESS_KEY: string; ACCOUNT_ID: string; } export default { async fetch(request: Request, env: Env): Promise { const s3 = new S3mini({ accessKeyId: env.R2_ACCESS_KEY_ID, secretAccessKey: env.R2_SECRET_ACCESS_KEY, endpoint: `https://${env.ACCOUNT_ID}.r2.cloudflarestorage.com/my-bucket`, region: "auto", }); const url = new URL(request.url); const key = url.pathname.slice(1); // strip leading "/" if (!key) { return new Response("Missing object key", { status: 400 }); } switch (request.method) { case "PUT": { const data = await request.arrayBuffer(); const contentType = request.headers.get("content-type") ?? "application/octet-stream"; await s3.putObject(key, new Uint8Array(data), contentType); return new Response("Created", { status: 201 }); } case "GET": { const response = await s3.getObjectResponse(key); if (!response) { return new Response("Not Found", { status: 404 }); } return new Response(response.body, { headers: { "content-type": response.headers.get("content-type") ?? "application/octet-stream", etag: response.headers.get("etag") ?? "", }, }); } case "DELETE": { await s3.deleteObject(key); return new Response(null, { status: 204 }); } default: return new Response("Method Not Allowed", { status: 405 }); } }, }; ``` -------------------------------- ### Read and Write Objects with R2 Binding (TypeScript) Source: https://developers.cloudflare.com/r2/get-started/workers-api/index.md This TypeScript example demonstrates how to store (PUT) and retrieve (GET) objects from an R2 bucket using the bound bucket name 'MY_BUCKET'. It uses the URL path as the object key. ```typescript export default { async fetch(request, env): Promise { // Get the object key from the URL path // For example: /images/cat.png → images/cat.png const url = new URL(request.url); const key = url.pathname.slice(1); // PUT: Store the request body in R2 if (request.method === "PUT") { await env.MY_BUCKET.put(key, request.body); return new Response(`Put ${key} successfully!`); } // GET: Retrieve the object from R2 const object = await env.MY_BUCKET.get(key); if (object === null) { return new Response("Object not found", { status: 404 }); } return new Response(object.body); }, } satisfies ExportedHandler; ``` -------------------------------- ### Change Directory Source: https://developers.cloudflare.com/r2/data-catalog/get-started Navigate into the newly created project directory. ```bash cd r2-data-catalog-notebook ``` -------------------------------- ### Read and Write Objects with R2 Binding (JavaScript) Source: https://developers.cloudflare.com/r2/get-started/workers-api/index.md This JavaScript example demonstrates how to store (PUT) and retrieve (GET) objects from an R2 bucket using the bound bucket name 'MY_BUCKET'. It uses the URL path as the object key. ```javascript export default { async fetch(request, env) { // Get the object key from the URL path // For example: /images/cat.png → images/cat.png const url = new URL(request.url); const key = url.pathname.slice(1); // PUT: Store the request body in R2 if (request.method === "PUT") { await env.MY_BUCKET.put(key, request.body); return new Response(`Put ${key} successfully!`); } // GET: Retrieve the object from R2 const object = await env.MY_BUCKET.get(key); if (object === null) { return new Response("Object not found", { status: 404 }); } return new Response(object.body); }, }; ``` -------------------------------- ### Initialize uv Project Source: https://developers.cloudflare.com/r2/data-catalog/get-started/index.md Initialize a new project using uv, a Python package manager. This command creates a virtual environment (.venv) and a pyproject.toml file. ```bash uv init ``` -------------------------------- ### Create, Upload Parts, and Complete Multipart Upload (JavaScript) Source: https://developers.cloudflare.com/r2/objects/upload-objects/index.md This JavaScript example demonstrates the complete lifecycle of a multipart upload. It shows how to create a new multipart upload, upload two parts (with placeholders for actual data), and then complete the upload. Error handling is included to abort the upload if any step fails. ```javascript export default { async fetch(request, env) { const key = "large-file.bin"; // Create a new multipart upload const multipartUpload = await env.MY_BUCKET.createMultipartUpload(key); try { // In a real application, these would be actual data chunks. // Each part except the last must be at least 5 MiB. const firstChunk = new Uint8Array(5 * 1024 * 1024); // placeholder const secondChunk = new Uint8Array(1024); // placeholder const part1 = await multipartUpload.uploadPart(1, firstChunk); const part2 = await multipartUpload.uploadPart(2, secondChunk); // Complete the upload with all parts const object = await multipartUpload.complete([part1, part2]); return Response.json({ key: object.key, etag: object.httpEtag, }); } catch (err) { // Abort on failure so incomplete uploads do not count against storage await multipartUpload.abort(); return new Response(`Multipart upload failed: ${err}`, { status: 500 }); } }, }; ``` -------------------------------- ### Generate Presigned URLs with AWS SDK for Kotlin Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-kotlin/index.md Generate presigned URLs for temporary public read or write access to an R2 bucket. These URLs can be used with any HTTP client. The example shows how to generate both PUT and GET URLs with a specified duration. ```kotlin val uploadUrl = r2Client.presignPutObject( input = PutObjectRequest { bucket = bucketName key = "README.md" }, duration = 15.minutes, ).url println(uploadUrl) val getUrl = r2Client.presignGetObject( input = GetObjectRequest { bucket = bucketName key = "README.md" }, duration = 15.minutes, ).url println(getUrl) ``` ```shell curl -X PUT "https://" -H "Content-Type: application/octet-stream" --data-binary "@local-file.txt" ``` ```shell curl -X GET "https://" -o downloaded-file.txt ``` -------------------------------- ### Create Project Directory Source: https://developers.cloudflare.com/r2/data-catalog/get-started Create a new directory to store your R2 data catalog notebook project. ```bash mkdir r2-data-catalog-notebook ``` -------------------------------- ### Create Consumer Worker with C3 Source: https://developers.cloudflare.com/r2/tutorials/upload-logs-event-notifications/index.md Use the C3 CLI to scaffold a new Worker project, selecting 'Hello World example' and 'Worker only' templates. ```bash npm create cloudflare@latest -- consumer-worker ``` ```bash yarn create cloudflare consumer-worker ``` ```bash pnpm create cloudflare@latest consumer-worker ``` -------------------------------- ### Client Setup Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-net This snippet shows how to initialize the S3 client with Cloudflare R2 specific configuration, including credentials and the ServiceURL. ```APIDOC ## Client setup In this example, you will pass credentials explicitly to the `IAmazonS3` initialization. If you wish, use a shared AWS credentials file or the SDK store in-line with other AWS SDKs. Refer to Configure AWS credentials ↗ for more details. ```csharp private static IAmazonS3 s3Client; public static void Main(string[] args) { // Retrieve your S3 API credentials for your R2 bucket via API tokens (see: https://developers.cloudflare.com/r2/api/tokens) var accessKey = ""; var secretKey = ""; var credentials = new BasicAWSCredentials(accessKey, secretKey); s3Client = new AmazonS3Client(credentials, new AmazonS3Config { // Provide your Cloudflare account ID ServiceURL = "https://.r2.cloudflarestorage.com", }); } ``` ``` -------------------------------- ### Install Python Dependency Source: https://developers.cloudflare.com/r2-sql/tutorials/end-to-end-pipeline/index.md Before running the script, install the 'requests' library using pip. ```bash pip install requests ``` -------------------------------- ### Install Wrangler with pnpm Source: https://developers.cloudflare.com/r2/get-started/cli Installs the Wrangler CLI as a development dependency in your project using pnpm. ```bash pnpm add -D wrangler ``` -------------------------------- ### Install Wrangler with yarn Source: https://developers.cloudflare.com/r2/get-started/cli Installs the Wrangler CLI as a development dependency in your project using yarn. ```bash yarn add -D wrangler ``` -------------------------------- ### Upload, Download, and List Objects with AWS CLI Source: https://developers.cloudflare.com/r2/get-started/cli Provides examples for uploading a file to R2, downloading a file from R2, and listing all objects within an R2 bucket using the AWS CLI with a custom endpoint. ```bash # Upload myfile.txt to my-bucket aws s3 cp myfile.txt s3://my-bucket/ --endpoint-url https://.r2.cloudflarestorage.com # Download myfile.txt from my-bucket to current directory aws s3 cp s3://my-bucket/myfile.txt ./ --endpoint-url https://.r2.cloudflarestorage.com # List all objects in my-bucket aws s3 ls s3://my-bucket/ --endpoint-url https://.r2.cloudflarestorage.com ``` -------------------------------- ### Install Wrangler with npm Source: https://developers.cloudflare.com/r2/get-started/cli Installs the Wrangler CLI as a development dependency in your project using npm. ```bash npm i -D wrangler ``` -------------------------------- ### List Buckets with AWS SDK for Java Source: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-java Demonstrates how to list all available buckets using the CloudflareR2Client. Ensure the client is properly configured before use. ```java CloudflareR2Client r2Client = new CloudflareR2Client(config); // List buckets System.out.println("Available buckets:"); r2Client.listBuckets().forEach(bucket -> System.out.println("* " + bucket.name()) ); ``` -------------------------------- ### Create, Upload Parts, and Complete Multipart Upload Source: https://developers.cloudflare.com/r2/objects/upload-objects This example demonstrates creating a multipart upload, uploading two parts (one large, one small), and then completing the upload. It includes error handling to abort the upload if any step fails. Use this pattern for uploading large files in a single Worker execution. ```javascript export default { async fetch(request, env) { const key = "large-file.bin"; // Create a new multipart upload const multipartUpload = await env.MY_BUCKET.createMultipartUpload(key); try { // In a real application, these would be actual data chunks. // Each part except the last must be at least 5 MiB. const firstChunk = new Uint8Array(5 * 1024 * 1024); // placeholder const secondChunk = new Uint8Array(1024); // placeholder const part1 = await multipartUpload.uploadPart(1, firstChunk); const part2 = await multipartUpload.uploadPart(2, secondChunk); // Complete the upload with all parts const object = await multipartUpload.complete([part1, part2]); return Response.json({ key: object.key, etag: object.httpEtag, }); } catch (err) { // Abort on failure so incomplete uploads do not count against storage await multipartUpload.abort(); return new Response(`Multipart upload failed: ${err}`, { status: 500 }); } }, }; ``` ```typescript export default { async fetch(request: Request, env: Env): Promise { const key = "large-file.bin"; // Create a new multipart upload const multipartUpload = await env.MY_BUCKET.createMultipartUpload(key); try { // In a real application, these would be actual data chunks. // Each part except the last must be at least 5 MiB. const firstChunk = new Uint8Array(5 * 1024 * 1024); // placeholder const secondChunk = new Uint8Array(1024); // placeholder const part1 = await multipartUpload.uploadPart(1, firstChunk); const part2 = await multipartUpload.uploadPart(2, secondChunk); // Complete the upload with all parts const object = await multipartUpload.complete([part1, part2]); return Response.json({ key: object.key, etag: object.httpEtag, }); } catch (err) { // Abort on failure so incomplete uploads do not count against storage await multipartUpload.abort(); return new Response(`Multipart upload failed: ${err}`, { status: 500 }); } }, } satisfies ExportedHandler; ``` -------------------------------- ### Add AWS SDK for Go v2 to Project Source: https://developers.cloudflare.com/r2/examples/authenticate-r2-auth-tokens Use `go get` to add the necessary AWS SDK for Go v2 packages to your project for R2 interaction. ```bash go get github.com/aws/aws-sdk-go-v2 go get github.com/aws/aws-sdk-go-v2/config go get github.com/aws/aws-sdk-go-v2/credentials go get github.com/aws/aws-sdk-go-v2/service/s3 ``` -------------------------------- ### Example R2 Access Policy Source: https://developers.cloudflare.com/r2/api/tokens An example JSON structure for an R2 access policy, defining permissions for resources. ```json [ { "id": "f267e341f3dd4697bd3b9f71dd96247f", "effect": "allow", "resources": { "com.cloudflare.edge.r2.bucket.4793d734c0b8e484dfc37ec392b5fa8a_default_my-bucket": "*", "com.cloudflare.edge.r2.bucket.4793d734c0b8e484dfc37ec392b5fa8a_eu_my-eu-bucket": "*" }, "permission_groups": [ { "id": "6a018a9f2fc74eb6b293b0c548f38b39", "name": "Workers R2 Storage Bucket Item Read" } ] } ] ``` -------------------------------- ### Install Wrangler CLI Source: https://developers.cloudflare.com/r2/get-started/cli/index.md Install the Wrangler CLI as a development dependency using npm, yarn, pnpm, or bun. ```bash npm i -D wrangler ``` ```bash yarn add -D wrangler ``` ```bash pnpm add -D wrangler ``` ```bash bun add -d wrangler ``` -------------------------------- ### Create Project Directory Source: https://developers.cloudflare.com/r2/data-catalog/get-started/index.md Create a new directory named 'r2-data-catalog-notebook' to store your marimo notebook and project files. ```bash mkdir r2-data-catalog-notebook ``` -------------------------------- ### List Objects with Options and Pagination Source: https://developers.cloudflare.com/r2/api/workers/workers-api-reference Demonstrates how to list objects from an R2 bucket using options like limit and include. It shows the correct way to handle pagination using the 'truncated' property and 'cursor' to retrieve all objects. ```javascript const options = { limit: 500, include: ["customMetadata"], }; const listed = await env.MY_BUCKET.list(options); let truncated = listed.truncated; let cursor = truncated ? listed.cursor : undefined; // ❌ - if your limit can't fit into a single response or your // bucket has less objects than the limit, it will get stuck here. while (listed.objects.length < options.limit) { // ... } // ✅ - use the truncated property to check if there are more // objects to be returned while (truncated) { const next = await env.MY_BUCKET.list({ ...options, cursor: cursor, }); listed.objects.push(...next.objects); truncated = next.truncated; cursor = next.cursor; } ```