I'm trying to play Instagram Video assets. The challenge is the videos are expirable. They expire every N mins.
I'm brainstorming a solution where I set up my CDN (Cloudfront/Cloudflare) which forwards the incoming requests to the original server (Instagram in this case), caches the video at CDN, and then keeps serving it without the need to request Instagram again. I don't want to download the videos and keep them in my bucket. Is it even possible?
====Update
After @Ross Suggestion, I'd a look at CloudFront functions and was able to redirect the incoming requests to another URL, basis on some condition. Following is the code.
function handler(event) {
var request = event.request;
var headers = request.headers;
if request.uri == '/assets/1.jpg'{
var newurl = 'https://instagram.com/media/1.jpg'
var response = {
statusCode: 302,
statusDescription: 'Found',
headers:
{ "location": { "value": newurl } }
}
return response;
}
return request
}
However, this redirects it to the newURL. What I'm looking for is not redirect, but the following
when the request is made to my server cdn, ie
mydomain.com/assets/1.jpg, the file1.jpgshould be served from the instagram server, whose value is thenewURLin the above code snippet. This should be done without changing my domain URL (in the address bar) to instagram.The following requests to
mydomain.com/assets/1.jpgshould be directly served from the cache, and should not be routed again to instagram.