Home > other >  AWS S3 downloading the HTML file instead of serving it
AWS S3 downloading the HTML file instead of serving it

Time:11-09

I'm trying to serve an HTML page after generating it with my code,

so every time I run my code, it will generate a new index.html file. And after that, I have to create a URL for this page like this:

file_name = 'index.html'
fig.write_html('/tmp/'   file_name, auto_play=False)

s3_client = boto3.client('s3')
response = s3_client.upload_file('/tmp/'   file_name, 
 Writing_Bucket_Name, file_name, 
 ExtraArgs={'ACL':'public-read', 'ContentType':'text/html'})



# generate presigned url
print('URL START ')
url = s3_client.generate_presigned_url('get_object',
                                       Params={'Bucket': Writing_Bucket_Name, 
                                                        'Key': 'index.html'},
                                                         ExpiresIn=86400)

The problem is: when I go to this URL, it will download the HTML file instead of serving it

CodePudding user response:

You have to specify which type of response you want to get, in your case :- you have to return an HTML file so that means you need text/html Response

You can do that by adding "ResponseContentType": "text/html" to your Params like this:

url = s3_client.generate_presigned_url('get_object',
                                       Params={                    
                                              'Bucket': Writing_Bucket_Name, 
                                              "ResponseContentType": "text/html", 
                                              'Key': 'index.html'         
                                              },
                                       ExpiresIn=86400)
  • Related