Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final mySecurityKey = 'e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed';
final url = 'https://test.b-cdn.net/sample-pdf-with-images.pdf';
// Extract the URL components.
final urlObj = CkUrl();
urlObj.parseUrl(url);
var urlScheme = 'https';
if (!urlObj.ssl) {
urlScheme = 'http';
}
final urlHost = urlObj.host;
final urlPath = urlObj.path;
// Calculate an expiration time 1 hour from the current date/time.
final expTime = CkDateTime();
expTime.setFromCurrentSystemTime();
expTime.addSeconds(3600);
final expires = expTime.getAsUnixTimeStr(false);
print('Expires = $expires');
// Create the string to hash
final sbToHash = CkStringBuilder();
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.
final token = sbToHash.getHash('sha256', 'base64Url', 'utf-8');
final sbSignedUrl = CkStringBuilder();
sbSignedUrl.append(urlScheme);
sbSignedUrl.append('://');
sbSignedUrl.append(urlHost);
sbSignedUrl.append(urlPath);
sbSignedUrl.append('?token=');
sbSignedUrl.append(token);
sbSignedUrl.append('&expires=');
sbSignedUrl.append(expires);
final signedUrl = sbSignedUrl.getAsString();
print('Signed URL: $signedUrl');
// Use the signed URL to download the file.
final http = CkHttp();
try {
http.download(signedUrl, 'c:/aaworkarea/sample.pdf');
print('Success.');
} on ChilkatException catch (e) {
print(e.lastErrorText);
}
}