通过亚马逊s3 php类强制下载文件

My application is using tpyo/amazon-s3-php-class to put and get objects to/from Amazon S3 storage.

Files are not available publicly and I want my users to be able to download private files through my application.

amazon-s3-php-class offers two options to get the file:

  1. Save an object to file: S3::getObject($bucketName, $uploadName, $saveName)
  2. Save an object to a resource of any type: S3::getObject($bucketName, $uploadName, fopen('savefile.txt', 'wb'))

But both methods save a file to my web server. How can I force file download to a client's computer?

I believe you could use the second option, passing the output buffer as the filename argument for fopen (after setting the correct headers):

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
//etc etc

S3::getObject($bucketName, $uploadName, fopen('php://output', 'wb'));