Dynamically override content-type response header in AWS CloudFront - amazon-s3

I'm using AWS S3 and CloudFront to distribute content.
My files stored in S3 have binary/octet-stream type but in fact they can be images or documents.
When generating S3 URLs, I can override the response content type with a parameter like this: https://my-bucket.s3.eu-central-1.amazonaws.com/foo/bar?[...]&response-content-type=image/jpeg
Is there a way to do it with CloudFront (with the same S3 bucket as origin)?
I tried adding the same parameter -- it's ignored.
I can create a response headers policy and add a custom header overriding origin, but as far as I can see, it'll be the same for all files whereas I need to control it with a request parameter.

Related

AWS s3 SignatureDoesNotMatch error during get Request through Cloudfront

I have two cloudfront and one s3 bucket and in both cloudfront i have added s3 bucket as a origin. (i am using origin access identity to serve s3 content)
I added same behavior in both cloudfront.
My problem is
I am able to access get s3 using only one cloudfront and its throwing error SignatureDoesNotMatch with other cloudfront.
For example:
https://cloudront1url/images/a.jpg is working but
https://cloudfront2url/images/a.jpg is not working.
Error that i am getting is click here
I got the issue. in behavior i was using "Cache Based on Selected Request Headers" (whitelist option) for s3 origin. I was white listing "host" header. when i choose option "none" in "Cache Based on Selected Request Headers" issue gets resolved.
In my case it was Origin Request Policy in Cloudfront being set to forward all headers which turns out takes your request headers and calculates signature while s3 calculates signature from specific set of headers.
Correct way to use OAI is with CORS-S3Origin request policy or cherry pick selected headers yourself.
I got a hint from this article. I had to edit the behavior, use “Legacy cache settings” and select “All” for “Query strings” (select default “None” for “Headers”, and select default “None” for “Cookies”). After that, the SignatureDoesNotMatch error was gone.
Here is the screenshot of the CloudFront behavior.

Modify S3 bucket response?

I want to serve an HTML page from an S3 bucket, which I'm able to do w/o issue. I'd like to then have the S3 bucket response (the static webpage) include the request headers - can I do this?

Use S3 pre-signed url to upload object with cache-control header on responses

It seems unclear form the docs, but here is my goal:
Create a presigned url to upload a file to s3
Pass that url to the browser
The browser uploads a file selected by the user to s3 using the pre-signed url
When the object on s3 is requested, the "Cache-Control: max-age=604800" header is on the response.
I would LIKE to not have to rely on the client to do anything special to make this happen. Meaning, some signal to S3 that it should set the cache control header to that value would have to be present in the pre-signed url, but I can't tell from the docs or 50 google searches how that is accomplished.
Any illumination into how this can be accomplished would be great!

AWS CloudFront Leverage browser caching not working

I am trying to set following Origin Custom Headers
Header Name: Cache-Control
Value: max-age=31536000
But it is giving com.amazonaws.services.cloudfront.model.InvalidArgumentException: The parameter HeaderName : Cache-Control is not allowed. (Service: AmazonCloudFront; Status Code: 400; Error Code: InvalidArgument; error.
I tried multiple ways along with setting the Minimum TTL, Default TTL, and Maximum TTL, but no help.
I assume you are trying to get good ratings on gtmetrix page score by leveraging browser caching! If you are serving content from S3 through cloudfront, then you need to add the following headers to objects in S3 while uploading files to S3.
Expires: {some future date}
Bonus: You do not need to specify this header for every object individually. You can upload a bunch of files together on S3, click next, and then on the screen that asks S3 storage class, scroll down and add these headers. And don't forget to click save!

Cloudfront Won't Set Expiration Header from S3 Origin

I am using an S3 bucket to store a bunch of product images for a large web site. These images are being served through Cloudfront with the S3 bucket as the origin. I have noticed that Cloudfront does not put an expiration header on the image even though I have set the distribution behavior to customize the cache headers and set a long min, max, and default TTL in Cloudfront.
I understand that I can put an expiration on the S3 object, however this is going to be quite impractical as I have millions of images. I was hoping that cloudfront would do me the honors of adding this header for me, but it does not.
So my question is the only way to get this expiration header to apply it every S3 object, or perhaps I am missing something in Cloudfront that will do it for me?
CloudFront's TTL configuration only controls the amount of time CloudFront keeps the object in the cache.
It doesn't add any headers.
So, yes, you'll need to set these on the objects in S3.
Note that Cache-Control: is usually considered a better choice than Expires:.
A alternative to avoid updating the onjects is to configure a proxy server in EC2 in the same region as the bucket, and let the server add the headers as the responses pass through it.
Request: CloudFront >> Proxy >> S3
Response: S3 >> Proxy >> CloudFront
...for what it's worth.