Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading asset to S3 isn't working

I need to upload a css file and a js file to S3 and use them as static resources. If I upload them via web from S3 page, it works. But if I upload via a python script, it uploads the files, but I can't get the css seems not working at all.

Here is my python code,

s3 = boto3.resource('s3')
s3.meta.client.upload_file('sample.css', 'mybucket', 'sample_dir/sample.css', {'ACL': 'public-read'})
like image 419
Reza Avatar asked Jan 07 '23 08:01

Reza


2 Answers

The notable condition here is that files uploaded through the console are correctly used by the browser, but files uploaded through the API are not.

The AWS/S3 console, by default, automatically sets the Content-Type: for many uploaded file types, (for CSS, this should probably be text/css)... but the API requires it be set by your code.

But, you don't appear to be setting it, so the browser may be refusing to use the css, even if it downloads successfully, because the Content-Type: response header contains an unexpected value.

(The browser dev tools/console should show an error or warning to confirm this).

like image 170
Michael - sqlbot Avatar answered Jan 16 '23 21:01

Michael - sqlbot


Mentioning file type solved this issue.

s3 = boto3.resource('s3')
s3.meta.client.upload_file('sample.css', 'mybucket', 'sample_dir/sample.css', {'ACL': 'public-read','public-read','ContentType': 'text/css'})
like image 24
Reza Avatar answered Jan 16 '23 20:01

Reza