2016-04-20 95 views
1

我正在使用boto3的s3.transfer从s3下载几个4GB +文件。所有,但一个能下载,但失败的人给了以下错误:在http://boto3.readthedocs.org/en/latest/_modules/boto3/s3/transfer.html如何增加boto3.s3.transfer下载的令牌过期时间?

s3_client = session.client('s3') 
transfer = S3Transfer(s3_client) 
# Download s3://bucket/key to /tmp/myfile 
transfer.download_file('bucket', 'key', '/tmp/myfile') 

ERROR: An error occurred (ExpiredToken) when calling the GetObject operation: The provided token has expired. 

我现在用同样的方式是文件是否有增加的方式boto3中使用的签名url的过期时间?

如果它是相关的,我使用Cognito拿到证书,并与他们,会话

client = boto3.client('cognito-identity', AWS_REGION) 

    # credentials[] contains the IdentityId and Token I get from my server 
    # which I get using client.get_open_id_token_for_developer_identity 
    # with TokenDuration=86400 
    resp = client.get_credentials_for_identity(IdentityId=credentials['IdentityId'], 
               Logins={'cognito-identity.amazonaws.com': credentials['Token']}) 

    # The resp contains the actual temporary AWS secret/access codes and a session token, to be 
    # used with the rest of the AWS APIs 
    secretKey = resp['Credentials']['SecretKey'] 
    accessKey = resp['Credentials']['AccessKeyId'] 
    sessionToken = resp['Credentials']['SessionToken'] 

    session = Session(aws_access_key_id=accessKey, 
         aws_secret_access_key=secretKey, 
         aws_session_token=sessionToken, 
         region_name=AWS_REGION) 

    s3_client = session.client('s3') 
+0

您的代码如何接收访问密钥/密钥?我的猜测是你的问题是代码在一个附加了IAM角色的EC2实例上运行。它是否正确 ?或者你是否通过配置文件传递AK/SK? ENV变量? –

+0

忘记提及我使用cognito获取密钥,访问密钥和会话令牌。 – dkarchmer

+0

更新了OP,代码显示了我是如何获得会话和s3客户端的 – dkarchmer

回答

1

遇到没有连接到S3的问题已签署的网址,你所想象。

Cognito建立在称为安全令牌服务(STS)的IAM服务之上。该服务允许通过使用谷歌,Facebook,亚马逊,通过承担角色(IAM用户,EC2实例,Lambda函数等)或通过提供联合身份验证方案的Web身份令牌来生成临时凭证(访问密钥和密钥) 。

根据用例,这些凭证的范围是有限的(根据您定义的任何IAM角色),并且时间在15秒到几个小时之间。

您通过Cognito获取的凭据由STS生成。在低级别,STS API允许指定您希望这些凭据保持有效的时间(请参阅http://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRoleWithWebIdentity.html)。但是,我无法在Cognito API(https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html)中找到等效项。我希望在这一点上被证明是错误的。

为什么你看到错误?

根据您提供的元素,我的猜测是您的下载代码运行时间超过了您收到的临时凭证的寿命。首先下载作品,但后来没有。

如何解决它?

  • 在低层次,一个干净的解决方案是使用STS,而不是Cognito,但 将需要大量的工作和你将失去使用跨越登录提供商Cognito(稳定的用户ID的所有 好处, 多个登录提供者,未经身份验证的用户...)
  • 假设您有多个文件传输,在循环中的另一种解决方案是检查凭证过期时间,并在文件传输之间更新它们。检查resp['Credentials']['Expiration']的到期时间。您可以通过再次致电get_credentials_for_identity来续订Cognito提供的凭证。
  • 您也可以考虑通过AWS提供的50多个边缘位置之一下载您的文件。这是本周刚发布的一项新的S3功能,可大大加快上传或下载大文件的速度。有关更多详情,请参阅http://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html。有与该用法相关的价格标签,请参阅http://aws.amazon.com/s3/pricing/
+1

我忘记了认知正在产生临时令牌,但我现在可以看到问题出现在那里。有些东西还在继续,因为令牌应该持续24小时,但有了这个,我知道要调试什么 – dkarchmer