2012-06-29 31 views
2

我想知道如何爲cloudfront創建一個簽名的URL。目前的工作解決方案是不安全的,我想切換系統以保護URL。如何使用Python創建簽名的雲端URL?

我一直在使用的Boto 2.5.2和Django的1.4

是否有關於如何使用boto.cloudfront.distribution.create_signed_url方法的工作示例試過嗎?或任何其他解決方案的工作?

我已經使用BOTO 2.5.2 API

def get_signed_url(): 
    import boto, time, pprint 
    from boto import cloudfront 
    from boto.cloudfront import distribution 
    AWS_ACCESS_KEY_ID = 'YOUR_AWS_ACCESS_KEY_ID' 
    AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET_ACCESS_KEY' 
    KEYPAIR_ID = 'YOUR_KEYPAIR_ID' 
    KEYPAIR_FILE = 'YOUR_FULL_PATH_TO_FILE.pem' 
    CF_DISTRIBUTION_ID = 'E1V7I3IOVHUU02' 
    my_connection = boto.cloudfront.CloudFrontConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) 
    distros = my_connection.get_all_streaming_distributions() 
    oai = my_connection.create_origin_access_identity('my_oai', 'An OAI for testing') 
    distribution_config = my_connection.get_streaming_distribution_config(CF_DISTRIBUTION_ID) 
    distribution_info = my_connection.get_streaming_distribution_info(CF_DISTRIBUTION_ID) 
    my_distro = boto.cloudfront.distribution.Distribution(connection=my_connection, config=distribution_config, domain_name=distribution_info.domain_name, id=CF_DISTRIBUTION_ID, last_modified_time=None, status='Active') 

    s3 = boto.connect_s3() 
    BUCKET_NAME = "YOUR_S3_BUCKET_NAME" 
    bucket = s3.get_bucket(BUCKET_NAME) 
    object_name = "FULL_URL_TO_MP4_ECLUDING_S3_URL_DOMAIN_NAME EG(my/path/video.mp4)" 
    key = bucket.get_key(object_name) 
    key.add_user_grant("READ", oai.s3_user_id) 

    SECS = 8000 
    OBJECT_URL = 'FULL_S3_URL_TO_FILE.mp4' 
    my_signed_url = my_distro.create_signed_url(OBJECT_URL, KEYPAIR_ID, expire_time=time.time() + SECS, valid_after_time=None, ip_address=None, policy_url=None, private_key_file=KEYPAIR_FILE, private_key_string=KEYPAIR_ID) 

似乎一切都正常,直到方法create_signed_url嘗試下面的代碼。它返回一個錯誤。

Exception Value: Only specify the private_key_file or the private_key_string not both 
+0

以供將來參考couldfront西寧URL與python3.4壞了,希望這樣可以節省別人一段時間,具體詳情這張票https://github.com/boto/boto/issues/2854?_pjax=%23js-repo -pjax容器 – Aameer

回答

3

略去private_key_string

my_signed_url = my_distro.create_signed_url(OBJECT_URL, KEYPAIR_ID, 
     expire_time=time.time() + SECS, private_key_file=KEYPAIR_FILE) 

這個參數用來傳遞私鑰文件的實際內容,作爲一個字符串。 the source中的意見解釋說只有private_key_fileprivate_key_string中的一個應該通過。

也可以省略所有這些都設置爲None的kwargs,因爲None是默認的。

+0

嗨克里斯, 謝謝您的建議。我曾嘗試這樣做,它會返回一個錯誤: Django的版本:\t 1.4 異常類型:\t RSAError 異常值:\t無從下手線 你知道什麼可能導致這個問題?多謝! – ipegasus

+0

沒關係。有用。該問題與不正確的雲端私人密鑰文件有關。我通過亞馬遜AWS安全證書頁面創建了一個新密鑰,它可以工作。感謝您的幫助! – ipegasus

相關問題