How do I display protected Amazon S3 images on my secure site using PHP?

前端 未结 4 2016
太阳男子
太阳男子 2021-01-30 17:39

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 d

4条回答
  •  醉梦人生
    2021-01-30 18:19

    You can download the contents from S3 (in a PHP script), then serve them using the correct headers.

    As a rough example, say you had the following in image.php:

    $s3 = new AmazonS3();
    $response = $s3->get_object($bucket, $image_name);
    if (!$response->isOK()) {
        throw new Exception('Error downloading file from S3');
    }
    header("Content-Type: image/jpeg");
    header("Content-Length: " . strlen($response->body));
    die($response->body);
    

    Then in your HTML code, you can do

    
    

提交回复
热议问题