@@ -26,6 +26,19 @@ export default {
2626 return new Response ( `Seeded ${ SEED_DATA . length } KV entries` ) ;
2727 }
2828
29+ // R2 routes
30+ case "/r2/seed" : {
31+ await Promise . all (
32+ R2_SEED_DATA . map ( ( { key, content, contentType, customMetadata } ) =>
33+ env . BUCKET . put ( key , content , {
34+ httpMetadata : { contentType } ,
35+ customMetadata,
36+ } )
37+ )
38+ ) ;
39+ return new Response ( `Seeded ${ R2_SEED_DATA . length } R2 objects` ) ;
40+ }
41+
2942 // D1 database route
3043 case "/d1" : {
3144 await env . DB . exec ( `
@@ -202,3 +215,134 @@ const SEED_DATA: [string, string][] = [
202215 [ "number-float" , "3.14159" ] ,
203216 [ "number-negative" , "-273.15" ] ,
204217] ;
218+
219+ interface R2SeedItem {
220+ key : string ;
221+ content : string | ArrayBuffer ;
222+ contentType : string ;
223+ customMetadata ?: Record < string , string > ;
224+ }
225+
226+ const R2_SEED_DATA : R2SeedItem [ ] = [
227+ // Root level files
228+ {
229+ key : "readme.txt" ,
230+ content : "Welcome to the R2 bucket! This is a sample readme file." ,
231+ contentType : "text/plain" ,
232+ } ,
233+ {
234+ key : "config.json" ,
235+ content : JSON . stringify (
236+ { version : "1.0.0" , environment : "development" } ,
237+ null ,
238+ 2
239+ ) ,
240+ contentType : "application/json" ,
241+ customMetadata : { author : "admin" , created : "2025-01-15" } ,
242+ } ,
243+
244+ // Images folder
245+ {
246+ key : "images/logo.svg" ,
247+ content :
248+ '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" fill="orange"/></svg>' ,
249+ contentType : "image/svg+xml" ,
250+ } ,
251+ {
252+ key : "images/banner.svg" ,
253+ content :
254+ '<svg xmlns="http://www.w3.org/2000/svg" width="300" height="100"><rect width="300" height="100" fill="blue"/></svg>' ,
255+ contentType : "image/svg+xml" ,
256+ } ,
257+ {
258+ key : "images/icons/home.svg" ,
259+ content :
260+ '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M12 3L2 12h3v9h6v-6h2v6h6v-9h3L12 3z"/></svg>' ,
261+ contentType : "image/svg+xml" ,
262+ } ,
263+ {
264+ key : "images/icons/settings.svg" ,
265+ content :
266+ '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"><circle cx="12" cy="12" r="3"/></svg>' ,
267+ contentType : "image/svg+xml" ,
268+ } ,
269+
270+ // Documents folder
271+ {
272+ key : "documents/report.txt" ,
273+ content :
274+ "Annual Report 2024\n\nThis is a sample annual report with important business metrics." ,
275+ contentType : "text/plain" ,
276+ customMetadata : { department : "finance" , year : "2024" } ,
277+ } ,
278+ {
279+ key : "documents/notes.md" ,
280+ content :
281+ "# Meeting Notes\n\n## Action Items\n- Review budget\n- Update roadmap\n- Schedule follow-up" ,
282+ contentType : "text/markdown" ,
283+ } ,
284+ {
285+ key : "documents/data.csv" ,
286+ content : "id,name,value\n1,Alpha,100\n2,Beta,200\n3,Gamma,300" ,
287+ contentType : "text/csv" ,
288+ } ,
289+
290+ // Data folder with nested structure
291+ {
292+ key : "data/users.json" ,
293+ content : JSON . stringify (
294+ [
295+ { id : 1 , name : "Alice" , role : "admin" } ,
296+ { id : 2 , name : "Bob" , role : "user" } ,
297+ ] ,
298+ null ,
299+ 2
300+ ) ,
301+ contentType : "application/json" ,
302+ } ,
303+ {
304+ key : "data/backup/2024/january.json" ,
305+ content : JSON . stringify ( { month : "January" , records : 150 } ) ,
306+ contentType : "application/json" ,
307+ customMetadata : { backup : "true" , period : "2024-01" } ,
308+ } ,
309+ {
310+ key : "data/backup/2024/february.json" ,
311+ content : JSON . stringify ( { month : "February" , records : 175 } ) ,
312+ contentType : "application/json" ,
313+ customMetadata : { backup : "true" , period : "2024-02" } ,
314+ } ,
315+
316+ // Logs folder
317+ {
318+ key : "logs/access.log" ,
319+ content :
320+ "2025-01-15 10:00:00 GET /api/users 200\n2025-01-15 10:01:00 POST /api/login 200\n2025-01-15 10:02:00 GET /api/products 200" ,
321+ contentType : "text/plain" ,
322+ } ,
323+ {
324+ key : "logs/error.log" ,
325+ content :
326+ "2025-01-15 09:30:00 ERROR Connection timeout\n2025-01-15 09:45:00 ERROR Invalid token" ,
327+ contentType : "text/plain" ,
328+ } ,
329+
330+ // Assets with various content types
331+ {
332+ key : "assets/styles.css" ,
333+ content :
334+ "body { font-family: sans-serif; }\n.container { max-width: 1200px; margin: 0 auto; }" ,
335+ contentType : "text/css" ,
336+ } ,
337+ {
338+ key : "assets/script.js" ,
339+ content : 'console.log("Hello from R2!");\nfunction init() { return true; }' ,
340+ contentType : "application/javascript" ,
341+ } ,
342+ {
343+ key : "assets/template.html" ,
344+ content :
345+ "<!DOCTYPE html>\n<html><head><title>Template</title></head><body><h1>Hello</h1></body></html>" ,
346+ contentType : "text/html" ,
347+ } ,
348+ ] ;
0 commit comments