PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
string ls_MySecurityKey
string ls_Url
oleobject loo_UrlObj
string ls_Url_scheme
string ls_Url_host
string ls_Url_path
oleobject loo_ExpTime
string ls_Expires
oleobject loo_SbToHash
string ls_Token
oleobject loo_SbSignedUrl
string ls_SignedUrl
oleobject loo_Http
li_Success = 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
ls_MySecurityKey = "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed"
ls_Url = "https://test.b-cdn.net/sample-pdf-with-images.pdf"
// Extract the URL components.
loo_UrlObj = create oleobject
li_rc = loo_UrlObj.ConnectToNewObject("Chilkat.Url")
if li_rc < 0 then
destroy loo_UrlObj
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_UrlObj.ParseUrl(ls_Url)
ls_Url_scheme = "https"
if loo_UrlObj.Ssl = 0 then
ls_Url_scheme = "http"
end if
ls_Url_host = loo_UrlObj.Host
ls_Url_path = loo_UrlObj.Path
// Calculate an expiration time 1 hour from the current date/time.
loo_ExpTime = create oleobject
li_rc = loo_ExpTime.ConnectToNewObject("Chilkat.CkDateTime")
loo_ExpTime.SetFromCurrentSystemTime()
loo_ExpTime.AddSeconds(3600)
ls_Expires = loo_ExpTime.GetAsUnixTimeStr(0)
Write-Debug "Expires = " + ls_Expires
// Create the string to hash
loo_SbToHash = create oleobject
li_rc = loo_SbToHash.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbToHash.Append(ls_MySecurityKey)
loo_SbToHash.Append(ls_Url_path)
loo_SbToHash.Append(ls_Expires)
// Base64Url encoding is the same as base64, except "-" is used instead of "+",
// "_" is used instead of "/", and no "=" padding is added.
ls_Token = loo_SbToHash.GetHash("sha256","base64Url","utf-8")
loo_SbSignedUrl = create oleobject
li_rc = loo_SbSignedUrl.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbSignedUrl.Append(ls_Url_scheme)
loo_SbSignedUrl.Append("://")
loo_SbSignedUrl.Append(ls_Url_host)
loo_SbSignedUrl.Append(ls_Url_path)
loo_SbSignedUrl.Append("?token=")
loo_SbSignedUrl.Append(ls_Token)
loo_SbSignedUrl.Append("&expires=")
loo_SbSignedUrl.Append(ls_Expires)
ls_SignedUrl = loo_SbSignedUrl.GetAsString()
Write-Debug "Signed URL: " + ls_SignedUrl
// Use the signed URL to download the file.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
li_Success = loo_Http.Download(ls_SignedUrl,"c:/aaworkarea/sample.pdf")
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
else
Write-Debug "Success."
end if
destroy loo_UrlObj
destroy loo_ExpTime
destroy loo_SbToHash
destroy loo_SbSignedUrl
destroy loo_Http