简体   繁体   中英

Update a static webpage in S3 bucket with Python/boto3

I tried to find another article with my question but I failed.

My end goal is to upload an html file into an S3 bucket that is configured to host a static webpage. I have tried with:

s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                  aws_secret_access_key=SECRET_KEY)

s3.upload_file(local_file, bucket, s3_file) 

and

with open("index.html", "rb") as f:
   s3.upload_fileobj(f, bucket, "index.html")

With both approaches, I successfully upload the html file. Nonetheless, when I access the URL instead of displaying the html file, it downloads it.

I can verify that the S3 bucket is configured to host a webpage and to display the file with the same name like the one I upload using Python. When I upload the file manually from my folder, it works as expected. When I push the file using Python instead of displaying it, it downloads it as mentioned above.

Has anyone experienced this before? Is the 2 x functions I tried not appropriate for the work I need to do? Do you think there is any misconfiguration on the S3 bucket?

Thanks in advance,

Based on the comments, the problem was incorrect/lack of text/html content type for the objects uploaded. The solution was to add it.

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