2016-03-05 82 views
1

我在page的底部發現了類似於我所需要的代碼。如何使用boto和python3使CloudFront中的對象失效?

#!/usr/bin/env python3 
from boto.cloudfront import CloudFrontConnection 

aws_access_key   = 'BJDJLSMQRWDSC4UPLS6S' # Not real 
aws_secret_access_key = '8xRnWKxRR/93TeQv3pwzMNR222nwe5kjhYweCAij' # Not real 
aws_cf_distribution_id = 'foobar35' 

objects = [ '/index.html' ] 
conn = CloudFrontConnection(aws_access_key, aws_secret_access_key) 
print(conn.create_invalidation_request(aws_cf_distribution_id, objects)) 

當我運行它,我得到以下錯誤:

$ ./invalidate.py 
Traceback (most recent call last): 
    File "./invalidate.py", line 14, in <module> 
    print(conn.create_invalidation_request(aws_cf_distribution_id, objects)) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/boto/cloudfront/__init__.py", line 263, in create_invalidation_request 
    raise CloudFrontServerError(response.status, response.reason, body) 
boto.cloudfront.exception.CloudFrontServerError: CloudFrontServerError: 404 Not Found 
<?xml version="1.0"?> 
<ErrorResponse xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/"> 
    <Error> 
    <Type>Sender</Type> 
    <Code>NoSuchDistribution</Code> 
    <Message>The specified distribution does not exist.</Message> 
    </Error> 
    <RequestId>343d3d5b-e269-11e5-bc96-eb9a228cf3e7</RequestId> 
</ErrorResponse> 

我認爲問題是,在那裏駐留/index.html我還沒有確定的S3桶。我有大約20個存儲桶,每個存儲桶都以URL的域名部分命名。我嘗試了各種排列,但無濟於事。

  • S3://www.example.com.s3.amazonaws.com/index.html
  • S3://www.example.com/index.html
  • /www.example.com /index.html
  • www.example.com/index.html
  • /index.html

有人能告訴我如何得到這個工作?

回答

0

弗拉基米爾Mukhin的答案是有幫助的:他是正確的,我的分配ID是不正確的,我需要創建一個分配。但是,我不知道如何獲取我的分配ID。我通過查看awscli文檔找到它。然而,答案不僅僅是RTFM,因爲我需要的信息不容易找到。下面是最後幫我

aws cloudfront list-distributions 

隨着這些信息的答案,我能找到在Python和Ruby的相似之處,和Perl(爪)API來獲取正確的分配ID不炮擊了命令行。希望這可以幫助某人。

4

實際誤差小於:

<Message>The specified distribution does not exist.</Message>

根據你的代碼,您指定「foobar35」作爲您的分配ID - 這是不正確的。

在嘗試使對象失效之前,您需要創建一個分配。創建它之後,您將收到一個分配標識,該分配標識應作爲參數傳遞給create_invalidation_request方法。

欲瞭解更多信息,請參閱:Creating or Updating a Web Distribution Using the CloudFront Console

+0

謝謝你,弗拉德。你是對的,我不明白關於分配ID的部分。但是,對於我的角色而言,我還沒有訪問AWS Web Console的權限。你能告訴我如何使用Boto3創建一個發行版並獲得它的ID嗎? – rcrews

+1

您需要使用'create_distribution()'方法。有關如何使用[這裏]的詳細說明(https://boto3.readthedocs.org/en/latest/reference/services/cloudfront.html#CloudFront.Client.create_distribution) –

相關問題