8

I have tried to generate pre-signed URL with 7dsys expiration time. (It is saying maximum duration is 7days, AWS S3 pre signed URL without Expiry date)

# It is called and retruned in AWS Lambda
boto3.client('s3').generate_presigned_url(
    'get_object',
    Params={'Bucket': bucket, 'Key': object_key},
    ExpiresIn=(60*60*24*7)  # 7days
)

However, it seems not to retain the pre-signed URL for 7days but just several hours. The pre-signed URL just returns the XML format after that.

<Error>
  <Code>ExpiredToken</Code>
    <Message>The provided token has expired.</Message>
.
.
.
</Error>

It seems even to be different expiration time every time I try, sometimes 5 hours, sometime 12hours.

I don't know why.

SangminKim
  • 8,358
  • 14
  • 69
  • 125
  • 1
    Reading this goes some way to explaining the issue but I think for a 7 day expiry you need to generate the signed URL using IAM user permissions: https://medium.com/@uchimanajet7/point-to-note-when-using-aws-s3-pre-signed-url-aws-s3-6acf4ac1a3ba – berimbolo Aug 13 '20 at 08:08
  • @berimbolo 's reply should be the right answer `import boto3 from botocore.client import Config # Get the service client with sigv4 configured s3 = boto3.client('s3', config=Config(signature_version='s3v4')) # Generate the URL to get 'key-name' from 'bucket-name' # URL expires in 604800 seconds (seven days) url = s3.generate_presigned_url( ClientMethod='get_object', Params={ 'Bucket': 'bucket-name', 'Key': 'key-name' }, ExpiresIn=604800 ) print(url) ` – user3821178 Feb 18 '21 at 04:02
  • doesn't works for me – zushe Feb 01 '22 at 17:31

1 Answers1

2
import boto3 
from botocore.client 
import Config  
# Get the service client with sigv4 configured 
s3 = boto3.client('s3', config=Config(signature_version='s3v4'))  
# Generate the URL to get 'key-name' from 'bucket-name' 
# URL expires in 604800 seconds (seven days) 
url = s3.generate_presigned_url(ClientMethod='get_object',Params={
                                                   'Bucket':'bucket-name',
                                                   'Key': 'key-name'
                                                    },ExpiresIn=604800)  
print(url)
Landerson
  • 37
  • 9