I am new to AWS S3 Service and working on a project like Dropbox. Now I want to store all the images in S3 with PHP and currently, I am using S3 presigned URL to PUT Image. Now I want to show the Image to the users. I found 2 ways to do this:
I want something like permanent URLs to view image and but without showing the path of the image as it is in S3 Bucket. For example, currently the URL has the structure like this :
domain.com///?
but I want something like this:
domain.com/ some code is like a pointer to get the image from s3 bucket's directory. Please give me a hint to do so.
Thanks in advance
You can use this script as a proxy to fetch the image from S3 using php curl.
$bucket = 'bucketname';
$filename = 'somefile.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://s3-<region>.amazonaws.com/' . $bucket . '/' . $filename);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
header("Content-Length: " . strlen($data));
echo $data;
Combine with .htaccess RewriteRule
for example, then your s3 bucket url is hidden and can simply fetch resource such as this url http://example.com/resources/images/somefile.jpg
RewriteRule ^resources/images/([^/]+)$ /proxy.php?filename=$1 [QSA,L]