RobinH
CDCloudflare Developers
•Created by RobinH on 5/9/2024 in #workers-help
Newbie Assistance with Worker Code
Hello -
I'm fairly new to Cloudflare and I'm making my first attempt at using a Worker. The goal is to have the Worker add a SAS token, from an Azure storage account, anytime a specific URL is processed in CF. For example, if the URL is sub.mydomain.com, add the SAS TOKEN (stored as a variable) so that the URL becomes sub.mydomain.com?<SASTOKEN>.
I found a blog post where someone explained how to do this: https://www.eliostruyf.com/cloudflare-serve-private-azure-storage-blobs/
Using the code provided:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
return fetch(
So, I'm stuck and not sure what I'm missing or doing wrong. If someone could offer some pointers or direction, it would be greatly appreciated. Thanks for your time, Robin
${request.url}${SASTOKEN}
);
}
Along with creating a variable called SASTOKEN, in the Worker settings of Cloudflare, I was getting an error that SASTOKEN was unknown. I posted on the Cloudflare community and it was pointed out that I wasn't sending the 'env parameter to your handleRequest function'. I was pointed towards this doc: https://developers.cloudflare.com/workers/configuration/environment-variables/#:~:text=Refer%20to%20the%20following%20example.
I added the 'export default' block and updated my 'return fetch' line to env.SASTOKEN (instead of just SASTOKEN) as suggested but, I'm still having an issue. First, upon doing that, the code editor is showing that in the env.SASTOKEN, 'env' is not valid/found. Then, if I try to save the code with those changes included, I get an error "unexpected token at 'export'".So, I'm stuck and not sure what I'm missing or doing wrong. If someone could offer some pointers or direction, it would be greatly appreciated. Thanks for your time, Robin
3 replies