Tüm Ürünler
Search
Belge Merkezi

CDN:URL imzalama örnekleri

Son Güncelleme:Oct 01, 2026

Alibaba Cloud CDN URL kimlik doğrulaması için sunucu tarafında URL imzalamayı uygulamak üzere bu Python örneklerini kullanın. Her imzalama türü farklı bir imzalı URL formatı üretir; CDN URL kimlik doğrulama yapılandırmanızla eşleşen türü seçin.

Bir imzalama türü seçin

İmzalama türü İmzalı URL formatı Kullanım durumu
Type A http://example.com/path?auth_key=<exp>-<rand>-<uid>-<hash> Kimlik doğrulama anahtarını sorgu parametresi olarak ekler. URL'lerinizde zaten sorgu dizeleri varsa kullanın.
Type B http://example.com/<YYYYmmddHHMM>/<hash>/path Zaman damgasını ve karma değerini yola yerleştirir. Sorgu dizesinin temiz kalmasını istiyorsanız tercih edilir.
Type C http://example.com/<hash>/<hexexp>/path Karma değerini ve onaltılık süre sonunu yola yerleştirir. Kompakt, yol tabanlı kimlik doğrulama için uygundur.

Her bir imzalama türünün algoritma ayrıntıları için bkz. Type A imzalama, Type B imzalama ve Type C imzalama.

Bu örnekler, imzalama sunucunuzda imzalı URL'ler oluşturur. İmzalı URL'leri oluşturduktan sonra bunları doğrulamak için Alibaba Cloud CDN konsolunda URL kimlik doğrulamayı yapılandırın. Bkz. Sonraki adımlar .

Parametreler

Parametre Açıklama Varsayılan Örnek
uri İmzalanacak orijinal URL — http://example.aliyundoc.com/ping?foo=bar
key CDN URL kimlik doğrulama ayarlarınızda yapılandırılan özel anahtar — <your-private-key>
exp UNIX zaman damgası olarak ifade edilen süre sonu. Geçerli zamana istenen TTL değerini ekleyerek ayarlayın. — int(time.time()) + 1 * 3600
rand Type A imzalama için imzalanan dizeye dahil edilen rastgele bir dize. "0" "0"
uid Type A imzalama için imzalanan dizeye dahil edilen kullanıcı tanımlayıcısı. "0" "0"

Kullanım notları

  • Python 3, Python 2 ile geriye dönük uyumlu olmadığından hem Python 2 hem de Python 3 örnekleri sağlanmıştır.

  • Python 2 ASCII kodlamasını kullanır. Python 3 ise UTF-8 kodlamasını kullanır. Python 3 örnekleri, doğru karma hesaplaması sağlamak amacıyla encoding='utf-8' parametresini hashlib.md5() işlevine iletir.

  • Bir URL Çince karakterler içeriyorsa, imzalama kodunu çalıştırmadan önce UrlEncode() ile URL kodlaması uygulayın.

  • İmzalama sunucunuz tarafından atanan TTL ile Alibaba Cloud CDN tarafından atanan TTL bağımsızdır. İmzalı bir URL'nin etkili geçerlilik süresi şu şekildedir: UNIX zaman damgası (imzalama sunucusu) + TTL (imzalama sunucusu) + TTL (Alibaba Cloud CDN). Örnek: UNIX zaman damgası 1444435200 + imzalama sunucusu TTL 3600 + CDN TTL 1800 = geçerlilik süresi 1444440600 tarihinde sona erer.

Type A imzalama

Type A imzalama, auth_key parametresini bir sorgu parametresi olarak ekler. İmzalanan dize şu şekildedir: <path>-<exp>-<rand>-<uid>-<key>.

İmzalı URL formatı:

http://example.aliyundoc.com/ping?foo=bar&auth_key=<exp>-<rand>-<uid>-<hash>

Python 3

import re
import time
import hashlib
import datetime

def md5sum(src):
    m = hashlib.md5()
    m.update(src.encode(encoding='utf-8'))  # UTF-8 encoding required for Python 3
    return m.hexdigest()

def a_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    rand = "0"      # "0" by default, other value is ok
    uid = "0"       # "0" by default, other value is ok
    sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)
    hashvalue = md5sum(sstring)
    auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)
    if args:
        return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)
    else:
        return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)

def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"   # original URI
    key = "<input private key>"                          # private key for URL authentication
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current time
    # "1 * 3600" is the TTL assigned by the signing server. Adjust based on your requirements. Unit: seconds.
    # The signing server TTL is independent of the Alibaba Cloud CDN TTL.
    # Validity period = UNIX timestamp (signing server) + TTL (signing server) + TTL (Alibaba Cloud CDN).
    # Example: timestamp 1444435200 + signing server TTL 3600 + CDN TTL 1800 = 1444440600.
    authuri = a_auth(uri, key, exp)
    print("URL : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":
    main()

Python 2

import re
import time
import hashlib
import datetime

def md5sum(src):
    m = hashlib.md5()
    m.update(src)
    return m.hexdigest()

def a_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    rand = "0"      # "0" by default, other value is ok
    uid = "0"       # "0" by default, other value is ok
    sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)
    hashvalue = md5sum(sstring)
    auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)
    if args:
        return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)
    else:
        return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)

def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"   # original URI
    key = "<input private key>"                          # private key for URL authentication
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current time
    authuri = a_auth(uri, key, exp)
    print("URL : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":
    main()

Type B imzalama

Type B imzalama, süre sonunu strftime('%Y%m%d%H%M') kullanarak YYYYmmddHHMM formatına dönüştürür ve zaman damgası ile karma değerini URL yoluna yerleştirir. İmzalanan dize şu şekildedir: key + nexp + path.

İmzalı URL formatı:

http://example.aliyundoc.com/<YYYYmmddHHMM>/<hash>/ping?foo=bar

Python 3

import re
import time
import hashlib
import datetime

def md5sum(src):
    m = hashlib.md5()
    m.update(src.encode(encoding='utf-8'))  # UTF-8 encoding required for Python 3
    return m.hexdigest()

def b_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    nexp = datetime.datetime.fromtimestamp(exp).strftime('%Y%m%d%H%M')
    sstring = key + nexp + path
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, nexp, hashvalue, path, args)

def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"   # original URI
    key = "<input private key>"                          # private key for URL authentication
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current time
    authuri = b_auth(uri, key, exp)
    print("URL : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":
    main()

Python 2

import re
import time
import hashlib
import datetime

def md5sum(src):
    m = hashlib.md5()
    m.update(src)
    return m.hexdigest()

def b_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    nexp = datetime.datetime.fromtimestamp(exp).strftime('%Y%m%d%H%M')
    sstring = key + nexp + path
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, nexp, hashvalue, path, args)

def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"   # original URI
    key = "<input private key>"                          # private key for URL authentication
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current time
    authuri = b_auth(uri, key, exp)
    print("URL : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":
    main()

Type C imzalama

Type C imzalama, süre sonunu "%x" % exp kullanarak onaltılık biçime dönüştürür ve karma değeri ile onaltılık süre sonunu URL yoluna yerleştirir. İmzalanan dize şu şekildedir: key + path + hexexp.

İmzalı URL formatı:

http://example.aliyundoc.com/<hash>/<hexexp>/ping?foo=bar

Python 3

import re
import time
import hashlib
import datetime

def md5sum(src):
    m = hashlib.md5()
    m.update(src.encode(encoding='utf-8'))  # UTF-8 encoding required for Python 3
    return m.hexdigest()

def c_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    hexexp = "%x" %exp
    sstring = key + path + hexexp
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, hashvalue, hexexp, path, args)

def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"   # original URI
    key = "<input private key>"                          # private key for URL authentication
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current time
    authuri = c_auth(uri, key, exp)
    print("URL : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":
    main()

Python 2

import re
import time
import hashlib
import datetime

def md5sum(src):
    m = hashlib.md5()
    m.update(src)
    return m.hexdigest()

def c_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    hexexp = "%x" %exp
    sstring = key + path + hexexp
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, hashvalue, hexexp, path, args)

def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"   # original URI
    key = "<input private key>"                          # private key for URL authentication
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current time
    authuri = c_auth(uri, key, exp)
    print("URL : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":
    main()

Sonraki adımlar

İmzalama sunucunuzda imzalı URL'ler oluşturduktan sonra, bunları doğrulamak için Alibaba Cloud CDN konsolunda URL kimlik doğrulamayı yapılandırın: