Created
August 30, 2026 18:55
-
-
Save waynegraham/39159d9d94cce0d7bc7d5f3a6fb47d72 to your computer and use it in GitHub Desktop.
Cloudflare image worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const OCI_REGION = "me-riyadh-1"; | |
| const OCI_NAMESPACE = "YOUR_NAMESPACE"; | |
| const OCI_BUCKET = "YOUR_BUCKET"; | |
| export default { | |
| async fetch(request) { | |
| const url = new URL(request.url); | |
| if (request.method !== "GET" && request.method !== "HEAD") { | |
| return new Response("Method not allowed", { | |
| status: 405, | |
| headers: { | |
| Allow: "GET, HEAD", | |
| }, | |
| }); | |
| } | |
| const objectName = decodeURIComponent( | |
| url.pathname.replace(/^\/+/, "") | |
| ); | |
| if (!objectName) { | |
| return new Response("Not found", { | |
| status: 404, | |
| }); | |
| } | |
| const encodedObjectName = objectName | |
| .split("/") | |
| .map(segment => encodeURIComponent(segment)) | |
| .join("/"); | |
| const originUrl = new URL( | |
| `https://objectstorage.${OCI_REGION}.oraclecloud.com` | |
| ); | |
| originUrl.pathname = | |
| `/n/${OCI_NAMESPACE}` + | |
| `/b/${OCI_BUCKET}` + | |
| `/o/${encodedObjectName}`; | |
| const originResponse = await fetch(originUrl.toString(), { | |
| method: request.method, | |
| cf: { | |
| cacheEverything: true, | |
| cacheTtlByStatus: { | |
| "200-299": 604800, | |
| "404": 60, | |
| "500-599": 0, | |
| }, | |
| }, | |
| }); | |
| const response = new Response( | |
| originResponse.body, | |
| originResponse | |
| ); | |
| // Don't leak OCI-specific headers. | |
| response.headers.delete("opc-request-id"); | |
| if (originResponse.ok) { | |
| response.headers.set( | |
| "Cache-Control", | |
| "public, max-age=86400, s-maxage=604800, stale-while-revalidate=86400" | |
| ); | |
| } | |
| return response; | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment