-
-
Save longsays/0527603c1d6b883b73e7249e053b83da to your computer and use it in GitHub Desktop.
修改Backblaze桶地址和访问令牌
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)); | |
}); | |
addEventListener("scheduled", (event) => { | |
event.waitUntil(updateRule()); | |
}); | |
const getB2Token = async () => { | |
const res = await fetch( | |
"https://api.backblazeb2.com/b2api/v2/b2_authorize_account", | |
{ | |
headers: { | |
Authorization: "Basic " + btoa(B2KeyID + ":" + B2AppKey), | |
}, | |
} | |
); | |
const data = await res.json(); | |
return data.authorizationToken; | |
}; | |
const updateRule = async () => { | |
const b2Token = await getB2Token(); | |
const res = await fetch( | |
`https://api.cloudflare.com/client/v4/zones/${CfZoneID}/rulesets/${CfRulesetID}/rules/${CfRuleID}`, | |
{ | |
method: "PATCH", | |
headers: { | |
"Authorization": `Bearer ${CfAuthKey}` | |
}, | |
body: | |
`{ | |
"description": "修改Backblaze桶地址和访问令牌", | |
"action": "rewrite", | |
"expression": "(http.host eq \\\"${CfHostname}\\\" and not starts_with(http.request.uri.path, \\\"/file/${B2BucketName}\\\"))", | |
"action_parameters": { | |
"uri": { | |
"path": { | |
"expression": "concat(\\\"/file/${B2BucketName}\\\", http.request.uri.path)" | |
}, | |
"query": { | |
"value": "Authorization=${b2Token}" | |
} | |
} | |
} | |
}`, | |
} | |
); | |
const data = await res.text(); | |
console.log(data); | |
return data; | |
}; | |
async function handleRequest(request) { | |
const data = await updateRule(); | |
return new Response(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment