Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkUrl.pb"
IncludeFile "CkDateTime.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    mySecurityKey.s = "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed"

    url.s = "https://test.b-cdn.net/sample-pdf-with-images.pdf"

    ; Extract the URL components.
    urlObj.i = CkUrl::ckCreate()
    If urlObj.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkUrl::ckParseUrl(urlObj,url)

    url_scheme.s = "https"
    If CkUrl::ckSsl(urlObj) = 0
        url_scheme = "http"
    EndIf

    url_host.s = CkUrl::ckHost(urlObj)
    url_path.s = CkUrl::ckPath(urlObj)

    ; Calculate an expiration time 1 hour from the current date/time.
    expTime.i = CkDateTime::ckCreate()
    If expTime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckSetFromCurrentSystemTime(expTime)
    CkDateTime::ckAddSeconds(expTime,3600)
    expires.s = CkDateTime::ckGetAsUnixTimeStr(expTime,0)

    Debug "Expires = " + expires

    ; Create the string to hash
    sbToHash.i = CkStringBuilder::ckCreate()
    If sbToHash.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbToHash,mySecurityKey)
    CkStringBuilder::ckAppend(sbToHash,url_path)
    CkStringBuilder::ckAppend(sbToHash,expires)

    ; Base64Url encoding is the same as base64, except "-" is used instead of "+",
    ; "_" is used instead of "/", and no "=" padding is added.
    token.s = CkStringBuilder::ckGetHash(sbToHash,"sha256","base64Url","utf-8")

    sbSignedUrl.i = CkStringBuilder::ckCreate()
    If sbSignedUrl.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbSignedUrl,url_scheme)
    CkStringBuilder::ckAppend(sbSignedUrl,"://")
    CkStringBuilder::ckAppend(sbSignedUrl,url_host)
    CkStringBuilder::ckAppend(sbSignedUrl,url_path)
    CkStringBuilder::ckAppend(sbSignedUrl,"?token=")
    CkStringBuilder::ckAppend(sbSignedUrl,token)
    CkStringBuilder::ckAppend(sbSignedUrl,"&expires=")
    CkStringBuilder::ckAppend(sbSignedUrl,expires)

    signedUrl.s = CkStringBuilder::ckGetAsString(sbSignedUrl)
    Debug "Signed URL: " + signedUrl

    ; Use the signed URL to download the file.
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckDownload(http,signedUrl,"c:/aaworkarea/sample.pdf")
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
    Else
        Debug "Success."
    EndIf



    CkUrl::ckDispose(urlObj)
    CkDateTime::ckDispose(expTime)
    CkStringBuilder::ckDispose(sbToHash)
    CkStringBuilder::ckDispose(sbSignedUrl)
    CkHttp::ckDispose(http)


    ProcedureReturn
EndProcedure