简体   繁体   English

无法使用boto签署云端URL

[英]Can't sign cloudfront URLs using boto

I've written the following code to sign URLS using boto, however, I'm unable to access the signed URLS. 我已经编写了以下代码来使用boto对URL进行签名,但是,我无法访问已签名的URL。 Any help would be greatly appreciated. 任何帮助将不胜感激。

con = cloudfront.CloudFrontConnection(
    settings.AWS_USER_DETAILS.access_key_id,
    settings.AWS_USER_DETAILS.secret_key_id,
)

identities = con.get_all_origin_access_identity()

if len(identities) < 1:
    oai = con.create_origin_access_identity(
        'cv-oai',
        'OAI signing iOS video requests'
    )
else:
    oai = con.get_all_origin_access_identity()[0]

distro_summary = con.get_all_distributions()[0]
distro_info = con.get_distribution_info(distro_summary.id)

bucket = boto.connect_s3(
    settings.AWS_USER_DETAILS.access_key_id,
    settings.AWS_USER_DETAILS.secret_key_id,
    host='s3-eu-west-1.amazonaws.com'
).get_bucket(u'cv-video')

key_str = video.video_url.split('/')[-1]
key = bucket.get_key(key_str)

key.add_user_grant("READ", oai.s3_user_id)

distro = distro_summary.get_distribution()

# For local testing
if ip_addr == '127.0.0.1':
    ip_addr = None

private_key_string = open(settings.CF_PRIV_KEY_LOC).read()
print private_key_string

signed_url = distro.create_signed_url(
    "https://%s/%s" % (distro_info.domain_name, key_str),
    'APKAI3I3QZ7I73OIQIXA',
    expire_time=int(time.time()) + video.video_length + 5 * 60,
    #valid_after_time=None,
    #ip_address=ip_addr,
    #policy_url=None,
    private_key_string=private_key_string
)

Error: 错误:

Update: This is the error received when visiting the URLs generated by the above code. 更新:这是访问上述代码生成的URL时收到的错误。 I am unable to find documentation of the CloudFront errors to determine why this might be happening. 我无法找到CloudFront错误的文档来确定为什么会发生这种情况。

<Error>
<Code>InvalidKey</Code>
<Message>Unknown Key</Message>
</Error>

Example URL: 示例网址:

https://d1xvt07pr26drp.cloudfront.net/2iwjz3wn9atqlfw4.mov?Expires=1388705061&Signature=auPWW-X1LzZJkPxJ5YPCqQMatchbAb3BxCWTG5oMY78G6LrH0sZiQL6EKdCX-l3Fi9E475b11uFL~HnxfBUY9QqQH86478QU5BnpmR-U7uEikRs7kTDWOVj4Riv3PHUmjmzlBW8xU7-n9C0m2UZSXedPsYYFdPoWHH0VLlyKk2TzgYydLqu~jtq0iNdmz-C9TOgUCaICiMYi082AVc7bt6xTfVszA9BeAD4KLKnr42raFDLojbA78Q-7bLNA2CiStdT-8BblQOQ5IXUCxYdkw7ak0vp77vER1pCG9cEBkCHD~9dZccyQEGJApO~ax4D5wDtCpeQj3l0pW6kYNUlP8Q__&Key-Pair-Id=APKAI3I3QZ7I73OIQIXA https://d1xvt07pr26drp.cloudfront.net/2iwjz3wn9atqlfw4.mov?Expires=1388705061&Signature=auPWW-X1LzZJkPxJ5YPCqQMatchbAb3BxCWTG5oMY78G6LrH0sZiQL6EKdCX-l3Fi9E475b11uFL~HnxfBUY9QqQH86478QU5BnpmR-U7uEikRs7kTDWOVj4Riv3PHUmjmzlBW8xU7-n9C0m2UZSXedPsYYFdPoWHH0VLlyKk2TzgYydLqu~jtq0iNdmz-C9TOgUCaICiMYi082AVc7bt6xTfVszA9BeAD4KLKnr42raFDLojbA78Q-7bLNA2CiStdT-8BblQOQ5IXUCxYdkw7ak0vp77vER1pCG9cEBkCHD~9dZccyQEGJApO~ax4D5wDtCpeQj3l0pW6kYNUlP8Q__&Key-Pair-Id=APKAI3I3QZ7I73OIQIXA

The key pair is definitely active: 密钥对肯定是活跃的:

密钥对处于活动状态

There is only one Origin Access Identity and one distribution: 只有一个Origin Access Identity和一个发行版:

[<boto.cloudfront.identity.OriginAccessIdentitySummary object at 0x10f356110>]
[<boto.cloudfront.distribution.DistributionSummary object at 0x10f356090>]

I had the same issue. 我遇到过同样的问题。

I wondered if couldfront and s3 had not yet been connected logically to the "origin access identity." 我想知道canfront和s3是否还没有逻辑连接到“原始访问身份”。 This is the identity that cloudfront uses to pull content from S3, verifying signatures with the private key pair you specified. 这是cloudfront用于从S3提取内容的身份,使用您指定的私钥对验证签名。

Sure enough, that was the problem. 果然,这就是问题所在。 Disable public access to the underlying S3 bucket, then tell AWS to use the appropriate identity, then try your code again. 禁用对底层S3存储桶的公共访问,然后告知AWS使用适当的标识,然后再次尝试使用您的代码。

Here's how from the Web: 这是来自网络的方式:

From the CloudFront dashboard ( https://console.aws.amazon.com/cloudfront/home ), click on "Distribution" on the left to see all your distros. 在CloudFront仪表板( https://console.aws.amazon.com/cloudfront/home )中,单击左侧的“分发”以查看您的所有发行版。 Select the one you want, clicking the [i] icon for more information. 选择所需的一个,单击[i]图标以获取更多信息。 From there, click on the [Origins] tab and select the radio button next to your bucket. 从那里,单击[Origins]选项卡,然后选择存储桶旁边的单选按钮。 Click it, and an Edit button will appear. 单击它,将出现“编辑”按钮。 Click Edit. 单击编辑。 Choose "Restrict Bucket Access" in the new panel that appears. 在出现的新面板中选择“限制存储桶访问”。 The site will prompt you for which access identity to do. 该站点将提示您要执行的访问标识。 Choose the identity you created. 选择您创建的身份。 Click "Yes, Edit" to save your changes. 单击“是,编辑”以保存更改。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM