Created
August 30, 2021 10:09
-
-
Save bazzooka/3853c429ebbf2438f88454ff2a62c273 to your computer and use it in GitHub Desktop.
Authorize Cloudflare to access Backblaze private buckets
This file contains 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
addEventListener("fetch", (event) => { | |
event.respondWith( | |
handleRequest(event.request).catch( | |
(err) => new Response(err.stack, { status: 500 }) | |
) | |
); | |
}); | |
async function handleRequest(request) { | |
const { pathname } = new URL(request.url); | |
if (pathname.startsWith("/postlife")) { | |
const response = await fetch('https://f002.backblazeb2.com/file'+ pathname, { | |
headers: { | |
Authorization: B2_DOWNLOAD_TOKEN | |
} | |
}) | |
return response; | |
} | |
return new Response("Unauthorized", {status: 401}); | |
} |
This file contains 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
/** | |
* Environment variables to declare for this worker: | |
* BB_API_TOKEN: Backblaze API token | |
* = (The application key id and application key are combined into a string in the format base64("applicationKeyId:applicationKey"). | |
* BB_BUCKET_ID: Backblaze bucketId | |
* CLOUDFLARE_TOKEN: CLOUDFLARE API TOKEN WITH EDIT PERMISSION ON WORKER | |
*/ | |
addEventListener("scheduled", (event) => { | |
event.waitUntil( | |
handleSchedule(event.scheduledTime) | |
) | |
}); | |
async function authorizeAccount() { | |
const response = await fetch('https://api.backblazeb2.com/b2api/v2/b2_authorize_account', { | |
headers: { | |
Authorization: 'Basic ' + BB_API_TOKEN | |
} | |
}) | |
return await response.json(); | |
} | |
async function getDownloadAuthorization(dlApiUrl, authorizationToken) { | |
const response = await fetch( dlApiUrl +'/b2api/v2/b2_get_download_authorization', { | |
method: 'POST', | |
headers: { | |
Authorization: authorizationToken, | |
}, | |
body: JSON.stringify({ | |
"bucketId": BB_BUCKET_ID, | |
"fileNamePrefix": "", | |
"validDurationInSeconds": 7*24*60*60 | |
}) | |
}) | |
return await response.json(); | |
} | |
async function deleteBBSecret() { | |
const response = await fetch('https://api.cloudflare.com/client/v4/accounts/'+CLOUDFLARE_ID+'/workers/scripts/backblaze/secrets/B2_DOWNLOAD_TOKEN', { | |
method: 'DELETE', | |
headers: { | |
Authorization: 'Bearer ' + CLOUDFLARE_TOKEN | |
} | |
}); | |
console.log(await response.json()) | |
} | |
async function updateBBSecret(bbToken) { | |
const response = await fetch('https://api.cloudflare.com/client/v4/accounts/'+CLOUDFLARE_ID+'/workers/scripts/backblaze/secrets', { | |
method: 'PUT', | |
headers: { | |
Authorization: 'Bearer ' + CLOUDFLARE_TOKEN | |
}, | |
body: JSON.stringify({ | |
"type": "secret_text", | |
"text": bbToken, | |
"name": "B2_DOWNLOAD_TOKEN" | |
}) | |
}); | |
console.log(await response.json()) | |
} | |
async function handleSchedule(request) { | |
const bbAuthorizeAccount = await authorizeAccount(); | |
const {authorizationToken} = await getDownloadAuthorization(bbAuthorizeAccount.apiUrl, bbAuthorizeAccount.authorizationToken); | |
// await deleteBBSecret(); | |
await updateBBSecret(authorizationToken); | |
return new Response("OK", {status: 200}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment