valdeir3194
CDCloudflare Developers
•Created by Oscar on 9/18/2023 in #pages-help
Unable to serve private CF images
const imageUrl = "https://imagedelivery.net/0F1Nj_HMA0zNbUzFz7cUlw/658e1c6b-3efb-457c-8340-a15f48389401/public";
generateSignedUrl(imageUrl)
.then(signedUrl => {
console.log("URL: ", signedUrl);
// Use o signedUrl conforme necessário, por exemplo, em um link de imagem
})
.catch(error => {
console.error("URL error: ", error);
});
8 replies
CDCloudflare Developers
•Created by Oscar on 9/18/2023 in #pages-help
Unable to serve private CF images
const KEY = process.env.NEXT_PUBLIC_TOKEN_CLOUDFLARE;
const EXPIRATION = 60 * 60 * 24;
const bufferToHex = (buffer: ArrayBuffer): string =>
Array.from(new Uint8Array(buffer)).map(b => b.toString(16).padStart(2, '0')).join('');
async function generateSignedUrl(imageUrl: string): Promise<string> {
const encoder = new TextEncoder();
const secretKeyData = encoder.encode(KEY);
const key = await crypto.subtle.importKey( 'raw', secretKeyData, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'] ); const expiry = Math.floor(Date.now() / 1000) + EXPIRATION;
const url = new URL(imageUrl); url.searchParams.set('exp', expiry.toString()); const stringToSign = url.pathname; const mac = await crypto.subtle.sign('HMAC', key, encoder.encode(stringToSign)); const sig = bufferToHex(mac); url.searchParams.set('sig', sig); return url.toString(); } export { generateSignedUrl };
const key = await crypto.subtle.importKey( 'raw', secretKeyData, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'] ); const expiry = Math.floor(Date.now() / 1000) + EXPIRATION;
const url = new URL(imageUrl); url.searchParams.set('exp', expiry.toString()); const stringToSign = url.pathname; const mac = await crypto.subtle.sign('HMAC', key, encoder.encode(stringToSign)); const sig = bufferToHex(mac); url.searchParams.set('sig', sig); return url.toString(); } export { generateSignedUrl };
8 replies
CDCloudflare Developers
•Created by Oscar on 9/18/2023 in #pages-help
Unable to serve private CF images
Hello, did you find a solution to this problem, I'm facing this too and so far I haven't found any solution, I use the same function as in the example informing the private url of the image, but I get the same error.
8 replies