- Newest
- Most votes
- Most comments
Hello,
please try this solution it will be helpful for you and resolve the issue.
To upload PNG, JPG, and JPEG files to an S3 bucket with the correct Content-Type using Terraform,
-
first configure your AWS provider with the appropriate region.
-
Then create an S3 bucket and ensure its name is globally unique.
-
Next, upload your image files to the bucket, specifying the correct Content-Type for each file: image/png for PNG files and image/jpeg for JPG and JPEG files. Additionally, set the access control list (ACL) to public-read if you want the objects to be publicly accessible.
-
This setup ensures that your files are served with the correct content type, allowing them to be properly handled and displayed when accessed.
please look at Terraform document Link & AWS Document link you will get more information.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object
https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingMetadata.html
https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html
AWS S3, the Content-Type is a metadata field that specifies the media type of the object being stored. It is essential for browsers and applications to correctly handle and display the stored objects. Here are some common Content-Type values for different file extensions:
For HTML files: text/html
For PNG images: image/png
For JPEG images: image/jpeg
For JSON files: application/json
For JavaScript files: application/javascript
For CSS files: text/css
For plain text files: text/plain
For XML files: application/xml
For your specific case, if you are uploading .png, .jpg, and .jpeg files, you should use the following Content-Type values:
For .png files: image/png
For .jpg and .jpeg files: image/jpeg
Here is an example of how to specify the Content-Type for these file types in Terraform when uploading objects to an S3 bucket:
resource "aws_s3_bucket" "example_bucket" {
bucket = "example-bucket"
}
resource "aws_s3_bucket_object" "example_png" {
bucket = aws_s3_bucket.example_bucket.bucket
key = "example.png"
source = "path/to/local/example.png"
content_type = "image/png"
}
resource "aws_s3_bucket_object" "example_jpg" {
bucket = aws_s3_bucket.example_bucket.bucket
key = "example.jpg"
source = "path/to/local/example.jpg"
content_type = "image/jpeg"
}
resource "aws_s3_bucket_object" "example_jpeg" {
bucket = aws_s3_bucket.example_bucket.bucket
key = "example.jpeg"
source = "path/to/local/example.jpeg"
content_type = "image/jpeg"
}
aws_s3_bucket resource creates the S3 bucket.
aws_s3_bucket_object resources upload the files to the S3 bucket and specify the Content-Type for each file based on its extension.
Relevant content
- asked 6 months ago
- asked 3 years ago
- asked 2 years ago

Thank you very much