【问题标题】:S3 link with longer expiration过期时间较长的 S3 链接
【发布时间】:2016-09-20 21:16:54
【问题描述】:

我正在使用 java sdk 为客户端生成预签名链接。我们有新的要求,允许链接至少保持活动 30 天。当我将到期时间设置为更长时,我收到以下错误:

通过 SigV4 算法预签名的请求最多有效 7天

我需要确定解决此问题的方法,因为客户端无法接受对链接的更新(例如,如果我只是每周自动生成更新)。有没有解决的办法?我可以通过一组给定的只读凭据吗?

【问题讨论】:

    标签: java amazon-web-services amazon-s3 aws-sdk


    【解决方案1】:

    请参阅此详细信息answer,了解有关天数限制的说明。

    为客户端生成只读凭据将无法正常工作,因为客户端必须使用这些凭据来创建自己的预签名 URL(与您现在所做的没有什么不同 - 它仍然会在最多7 天)或使用 AWS SDK 直接下载文件,无需预签名 URL。

    使用 SigV4 并拥有超过 7 天的恒定链接可以通过中间层(如 REST 端点)完成,该中间层的 URL 不会更改并在请求时提供文件。

    【讨论】:

    • 您好,您能否提供更多关于如何解决带有中间层的签名 URL 过期问题的详细信息?非常感谢!
    【解决方案2】:

    遗憾的是,使用 S3 预签名 URL 不能超过 7 天。

    一种可能的解决方案是使用 CloudFront 签名的 url,这些对 url 的有效期没有“限制”。 S3 存储桶仍将保持私有状态。

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html

    Java 示例:

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CFPrivateDistJavaDevelopment.html

    【讨论】:

    【解决方案3】:
    import logging
    import boto3
    from botocore.exceptions import ClientError
    from botocore.client import Config
    
    # python > 3 should be installed
    # pip install boto3
    # s3v4
    # (Default) Signature Version 4
    # v4 algorithm starts with X-Amz-Algorithm
    #
    # s3
    # (Deprecated) Signature Version 2,  this only works in some regions new regions not supported
    # if you have to generate signed url that has > 7 days expiry then use version 2 if your region supports it. below code illustration of this
    
    s3_signature ={
        'v4':'s3v4',
        'v2':'s3'
    }
    
    def create_presigned_url(bucket_name, bucket_key, expiration=3600):
        """Generate a presigned URL to share an S3 object
    
        :param bucket_name: string
        :param bucket_key: string
        :param expiration: Time in seconds for the presigned URL to remain valid
        :return: Presigned URL as string. If error, returns None.
        """
    
        # Generate a presigned URL for the S3 object
        s3_client = boto3.client('s3',
                                 aws_access_key_id='your_access_key_here',
                                 aws_secret_access_key='your_secret_key_here',
                                 config=Config(signature_version=s3_signature['v2']),
                                 region_name='us-east-1'
                                 )
        try:
            response = s3_client.generate_presigned_url('get_object',
                                                        Params={'Bucket': bucket_name,
                                                                'Key': bucket_key},
                                                        ExpiresIn=expiration)
        except ClientError as e:
            logging.error(e)
            return None
    
        # The response contains the presigned URL
        return response
    
    weeks = 8
    
    seven_days_as_seconds = 604800
    
    signed_url = create_presigned_url('your_bucket_here', 'your_key/file_name.xls', (seven_days_as_seconds*weeks))
    
    print(signed_url)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多