简体   繁体   中英

Boto3 generating different links locally and on elastic beanstalk with a django application

I am a bit confused right now: I have a Django Application that uploads files to s3 and generates download links in the admin panel, to retrieve the files after uploading. I use boto3 for signing upload and download links. It all works fine locally but as soon as I deploy it to elastic beanstalk the links to download are not working anymore and it seems it generates the links with an old singing version. The upload still works though.

I am generating the links like so:

 def downloadUrl(self):
    s3Client = boto3.client('s3')
    bucketName = getattr(settings, "AWS_STORAGE_BUCKET_NAME", None)
    fileurl = s3Client.generate_presigned_url('get_object', Params={'Bucket': bucketName, 'Key': self.url.split(bucketName + '/')[1]},
                                    ExpiresIn=100)
    return format_html("<a href='{url}'>{url}</a>", url=fileurl)

And the funny thing is it works locally and the generated links are different:

Local Link:

https://bucketname.s3.amazonaws.com/uploads/something/zips/2019/03/28/App%20Beschreibung.pdf.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJNND443OXASGOY2Q%2F201330328%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20190328T101414Z&X-Amz-Expires=100&X-Amz-SignedHeaders=host&X-Amz-Signature=4fe4d31dc1fd1f772217656654025b669513597c3f91f857fb722ff63a6c0194

Link Generated by Elastic Beanstalk Deployment:

https://bucketname.s3.amazonaws.com/uploads/something/zips/2019/03/28/App%20Beschreibung.pdf.zip?AWSAccessKeyId=ASIA2K3XQKDHVDBX3GWN&Signature=w%2FWRh6tf%2Ba7k91Odop3ly9gEJDw%3D&x-amz-security-token=AgoJb3JpZ2luX2VjEHIaDGV1LWNlbnRyYWwtMSJIMEYCIQD7hU0fX24bksIM3vlQxZjYeje2sDm%2FQeeFQqUt7MwDpQIhAIozM1i1%2FmGK2Xo0RXmR7UWAB2FRPdkQy3aBOisS3BzuKuADCCsQABoMNzEwNTMwODQ2OTI3IgxfJIvFGgD5WL6TwccqvQO1Bsri%2FTmpvaARkFA%2BOnhLRUv9FFfdFf6H8W4IY38OI6CWzvct%2BgmE14D06GHaCh65wKH%2BhR9ZT3V18sW9U4RM%2FpS64hESmC8SUerzpPv9RmuXgA8hQl%2BwRJaTFLGFRFrHdHFoeJAeLIKjK2B7lDjLzNALtvegnLrBYoBtT0Ga2a7CC7avWMevvQ0jAjuSMiEOS4FjXNwHInkb%2FCrX3EXk2Me9OaufICHGH0ZTN2WNAGtOPsY15YJyLf0WgDl7SxVLuAnA4nUzeMglsdaL9ZbUlsdleJtHTQefNqCXXsm4FIup%2Fe%2FNFkn9g7XDH9%2F9IXW%2FNZyBAtfrRoPmDwvv0KpeYPpRpkVKBa5TxQU1c6ei3AiHnIQZRdO5oEqW47qv9hM8%2Fw9U9VlDqyUy5BKbGnv7GTLCdh4%2FwcYEnlfgr8QdK%2BJzyQYdgpMYqw3hj%2FTDW8mJVI9AD3RpHnD0XwIL3BBH5fUgcNZm2vGLG5nOcav7u2YaeqdFiY95SkdL5C8CLv%2BRLYABXyAbeWPUtFzgTtdUFFlv5epAQO2cPOD%2BM3V%2BjhfTQa4WUSIRCwUpSQg3dahIvV75cbKdsdZ7bL1tRMIqz8uQFOrMBC7isGac1kJ0TqHMbiQ77QauekC9c1gy6wgnKu9enc2x4VvyhClhvTvmEFm5NW3CmU5LIXf9V4Df9ML7Mf3Nf2uhuAhQdTKoAIGPiVWz%2FB3Lh%2F58%2FLbRbIRNgWk6IDg88dzgevitkXSC0OokmpB3qbBj%2BGXjIHttHqdBKnjG22aud3wrMk6YxH8b0CfelmcN5vHykpMYgdfgNHvFKrea417ozh5CLfZ9PYQoug0iqsm9nqoU%3D&Expires=1553768535

as you can see the signing is different but I do not understand why. I also checked if there is maybe a different version running on EB but it is the same version as locally, I downloaded the zip and ran it locally and it worked.

I now found out what the problem was: I forgot to set the environment variables for S3 Access key and secret key to the boto library had no valid credentials to generate the links. The error message was very misleading though. I then set the variables in the elastic beanstalk console and it instantly worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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