3

I am converting some legacy code that serves an image file from disk:

$file_path = 'dir/image.jpg';
$readfile($file_path);

Rather than read a file from disk, I want to get a file from AWS S3.
So I use the PHP SDK to call GetObject(). This returns a blob:

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#getobject

Type: blob (string|resource|Psr\Http\Message\StreamInterface)

I would like to send that blob directly into readfile():

$result = $s3->getObject([
  'Bucket'      =>$_ENV['S3_BUCKET']
  ,'Key'        =>$file_path
]);
readfile($result['Body']); 

but it doesn't seem to be working. I can only think to save the file to disk, then load it using readfile(). But it would be nice to just pass the blob around in memory rather than writing it to disk temporarily.

paperduck
  • 1,175
  • 1
  • 16
  • 26
  • 1
    I got it working by using echo instead of readfile. Just echo the blob. The data gets put on the output stream all the same, apparently. – paperduck Jun 30 '20 at 03:54

0 Answers0