Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local mySecurityKey = "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed"
local url = "https://test.b-cdn.net/sample-pdf-with-images.pdf"
-- Extract the URL components.
local urlObj = chilkat.Url.new()
urlObj:parse_url(url)
local url_scheme = "https"
if not urlObj.ssl then
url_scheme = "http"
end
local url_host = urlObj.host
local url_path = urlObj.path
-- Calculate an expiration time 1 hour from the current date/time.
local expTime = chilkat.DateTime.new()
expTime:set_from_current_system_time()
expTime:add_seconds(3600)
local expires = expTime:get_as_unix_time_str(false)
print("Expires = " .. expires)
-- Create the string to hash
local sbToHash = chilkat.StringBuilder.new()
sbToHash:append(mySecurityKey)
sbToHash:append(url_path)
sbToHash:append(expires)
-- Base64Url encoding is the same as base64, except "-" is used instead of "+",
-- "_" is used instead of "/", and no "=" padding is added.
local token = sbToHash:get_hash("sha256", "base64Url", "utf-8")
local sbSignedUrl = chilkat.StringBuilder.new()
sbSignedUrl:append(url_scheme)
sbSignedUrl:append("://")
sbSignedUrl:append(url_host)
sbSignedUrl:append(url_path)
sbSignedUrl:append("?token=")
sbSignedUrl:append(token)
sbSignedUrl:append("&expires=")
sbSignedUrl:append(expires)
local signedUrl = sbSignedUrl:get_as_string()
print("Signed URL: " .. signedUrl)
-- Use the signed URL to download the file.
local http = chilkat.Http.new()
local success = http:download(signedUrl, "c:/aaworkarea/sample.pdf")
if not success then
print(http.last_error_text)
else
print("Success.")
end