すべてのプロダクト
Search
ドキュメントセンター

CDN:URL署名の例

最終更新日:Feb 22, 2024

このトピックでは、Pythonのデモを使用して、a型署名、B型署名、およびC型署名の実装方法を示します。

サンプルコード:

URL署名の種類の詳細については、次のトピックを参照してください。

例:

説明
  • Pythonには、Python 2とPython 3の2つのメジャーバージョンがあります。 Python 3は、Python 2と下位互換性がありません。 したがって、Python 2およびPython 3のサンプルコードが提供されます。

  • URLに漢字が含まれている場合は、URL署名のコードを実行する前に、UrlEncode() 関数を使用してURLをエンコードします。

  • Python 2はASCIIエンコーディングを使用し、Python 3はUTF-8エンコーディングを使用します。 ハッシュを渡すには、UTF-8エンコーディングを使用する必要があります。 そのため、Python 3のサンプルコードでは、UTF-8エンコーディングがhashlib.md5() 関数に追加されます。

Python3

import re
インポート時間
hashlibのインポート
datetimeのインポート
def md5sum(src):
    m = hashlib.md5()
    m.update(src.encode(encoding='utf-8')) # UTF-8のエンコード操作を追加します。
    return m.hexdigest()
    # タイプA署名
def a_auth(uri、key、exp):
    p = re.com pile("^(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:
        "% s % s % s?auth_key=% s" % (スキーム、ホスト、パス、args、auth_key) を返します。
    # タイプB署名
def b_auth(uri、key、exp):
    p = re.com pile("^(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 = ""
    # convert unix timestamp to "YYmmDDHHMM" format
    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)
    # タイプC署名
def c_auth(uri、key、exp):
    p = re.com pile("^(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)
    # 次のコードブロックは、uri、key、およびexpパラメーターの値を示しています。def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar" # original uri
    key = "<input private key>"                         # private key of authorization
    exp = int(time.time()) + 1*3600# 有効期限: 現在の時刻から1時間後
    # "1*3600" は、署名サーバーが署名済みURLに割り当てるTTL値を指定します。 ビジネス要件に基づいて値を指定できます。 単位は秒です。 署名サーバーによって割り当てられるTTL値は、Alibaba Cloud CDNによって割り当てられるTTL値とは無関係です。 
    # 署名付きURLの有効期間=署名サーバーで生成されたUNIXタイムスタンプ + 署名サーバーによって割り当てられたTTL + Alibaba Cloud CDNによって割り当てられたTTL。
    # タイプAの署名の場合、署名サーバーで生成されたUNIXタイムスタンプが1444435200、署名サーバーによって割り当てられたTTL値が3600、Alibaba Cloud CDNによって割り当てられたTTL値が1800の場合、URLの有効期間は1444440600 (1444435200 + 3600 + 1800) です。
    # 次の例は、タイプAの署名を実装する方法を示しています。
    authuri = a_auth(uri, key, exp)                     # auth type: a_auth / b_auth / c_auth
    プリント ("URL : % s\nAUTH: % s" %(uri, authuri))
if __name__ == "__main__":
    main() 

Python2

import re
インポート時間
hashlibのインポート
datetimeのインポート
def md5sum(src):
    m = hashlib.md5()
    m.update(src)
    return m.hexdigest()
    # タイプA署名
def a_auth(uri、key、exp):
    p = re.com pile("^(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:
        "% s % s % s?auth_key=% s" % (スキーム、ホスト、パス、args、auth_key) を返します。
    # タイプB署名
def b_auth(uri、key、exp):
    p = re.com pile("^(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 = ""
    # convert unix timestamp to "YYmmDDHHMM" format
    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)
    # タイプC署名
def c_auth(uri、key、exp):
    p = re.com pile("^(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)
    # 次のコードブロックは、uri、key、およびexpパラメーターの値を示しています。def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar" # original uri
    key = "<input private key>"                         # private key of authorization
    exp = int(time.time()) + 1*3600# 有効期限: 現在の時刻から1時間後
    # "1*3600" は、署名サーバーが署名済みURLに割り当てるTTL値を指定します。 ビジネス要件に基づいて値を指定できます。 単位は秒です。 署名サーバーによって割り当てられるTTL値は、Alibaba Cloud CDNによって割り当てられるTTL値とは無関係です。 
    # 署名付きURLの有効期間=署名サーバーで生成されたUNIXタイムスタンプ + 署名サーバーによって割り当てられたTTL + Alibaba Cloud CDNによって割り当てられたTTL。
    # タイプAの署名の場合、署名サーバーで生成されたUNIXタイムスタンプが1444435200、署名サーバーによって割り当てられたTTL値が3600、Alibaba Cloud CDNによって割り当てられたTTL値が1800の場合、URLの有効期間は1444440600 (1444435200 + 3600 + 1800) です。
    # 次の例は、タイプAの署名を実装する方法を示しています。
    authuri = a_auth(uri, key, exp)                     # auth type: a_auth / b_auth / c_auth
    プリント ("URL : % s\nAUTH: % s" %(uri, authuri))
if __name__ == "__main__":
    main()