Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I display protected Amazon S3 images on my secure site using PHP?
    text
    copied!<p>I am trying to move images for my site from my host to Amazon S3 cloud hosting. These images are of client work sites and cannot be publicly available. I would like them to be displayed on my site preferably by using the PHP SDK available from Amazon.</p> <p>So far I have been able to script for the conversion so that I look up records in my database, grab the file path, name it appropriately, and send it to Amazon.</p> <pre><code> //upload to s3 $s3-&gt;create_object($bucket, $folder.$file_name_new, array( 'fileUpload' =&gt; $file_temp, 'acl' =&gt; AmazonS3::ACL_PRIVATE, //access denied, grantee only own //'acl' =&gt; AmazonS3::ACL_PUBLIC, //image displayed //'acl' =&gt; AmazonS3::ACL_OPEN, //image displayed, grantee everyone has open permission //'acl' =&gt; AmazonS3::ACL_AUTH_READ, //image not displayed, grantee auth users has open permissions //'acl' =&gt; AmazonS3::ACL_OWNER_READ, //image not displayed, grantee only ryan //'acl' =&gt; AmazonS3::ACL_OWNER_FULL_CONTROL, //image not displayed, grantee only ryan 'storage' =&gt; AmazonS3::STORAGE_REDUCED ) ); </code></pre> <p>Before I copy everything over, I have created a simple form to do test upload and display of the image. If I upload an image using ACL_PRIVATE, I can either grab the public url and I will not have access, or I can grab the public url with a temporary key and can display the image.</p> <pre><code>&lt;?php //display the image link $temp_link = $s3-&gt;get_object_url($bucket, $folder.$file_name_new, '1 minute'); ?&gt; &lt;a href='&lt;?php echo $temp_link; ?&gt;'&gt;&lt;?php echo $temp_link; ?&gt;&lt;/a&gt;&lt;br /&gt; &lt;img src='&lt;?php echo $temp_link; ?&gt;' alt='finding image' /&gt;&lt;br /&gt; </code></pre> <p>Using this method, how will my caching work? I'm guessing every time I refresh the page, or modify one of my records, I will be pulling that image again, increasing my get requests.</p> <p>I have also considered using bucket policies to only allow image retrieval from certain referrers. Do I understand correctly that Amazon is supposed to only fetch requests from pages or domains I specify?</p> <p>I referenced: <a href="https://forums.aws.amazon.com/thread.jspa?messageID=188183&amp;#188183" rel="noreferrer">https://forums.aws.amazon.com/thread.jspa?messageID=188183&amp;#188183</a> to set that up, but then am confused as to which security I need on my objects. It seemed like if I made them Private they still would not display, unless I used the temp link like mentioned previously. If I made them public, I could navigate to them directly, regardless of referrer.</p> <p>Am I way off what I'm trying to do here? Is this not really supported by S3, or am I missing something simple? I have gone through the SDK documentation and lots of searching and feel like this should be a little more clearly documented so hopefully any input here can help others in this situation. I've read others who name the file with a unique ID, creating security through obscurity, but that won't cut it in my situation, and probably not best practice for anyone trying to be secure.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload