React Native
React Native
Bunny Sign URL and then Download using Signed URL
See more Bunny CDN Examples
Shows how to sign a URL for BunnyCDN Token Authentication and then use it to download.Chilkat React Native Downloads
import { CkDateTime, Http, StringBuilder, Url } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const mySecurityKey = 'e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed';
const url = 'https://test.b-cdn.net/sample-pdf-with-images.pdf';
// Extract the URL components.
const urlObj = new Url();
urlObj.parseUrl(url);
let urlScheme = 'https';
if (!urlObj.ssl) {
urlScheme = 'http';
}
const urlHost = urlObj.host;
const urlPath = urlObj.path;
// Calculate an expiration time 1 hour from the current date/time.
const expTime = new CkDateTime();
expTime.setFromCurrentSystemTime();
expTime.addSeconds(3600);
const expires = expTime.getAsUnixTimeStr(false);
console.log(`Expires = ${expires}`);
// Create the string to hash
const sbToHash = new StringBuilder();
sbToHash.append(mySecurityKey);
sbToHash.append(urlPath);
sbToHash.append(expires);
// Base64Url encoding is the same as base64, except "-" is used instead of "+",
// "_" is used instead of "/", and no "=" padding is added.
const token = sbToHash.getHash('sha256', 'base64Url', 'utf-8');
const sbSignedUrl = new StringBuilder();
sbSignedUrl.append(urlScheme);
sbSignedUrl.append('://');
sbSignedUrl.append(urlHost);
sbSignedUrl.append(urlPath);
sbSignedUrl.append('?token=');
sbSignedUrl.append(token);
sbSignedUrl.append('&expires=');
sbSignedUrl.append(expires);
const signedUrl = sbSignedUrl.getAsString();
console.log(`Signed URL: ${signedUrl}`);
// Use the signed URL to download the file.
const http = new Http();
try {
await http.downloadAsync(signedUrl, 'c:/aaworkarea/sample.pdf');
console.log('Success.');
} catch {
console.log(http.lastErrorText);
}
}