Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store PHPExcel files in Amazon S3

I have an application that creates a lot of Excel files, which I would like to store in Amazon S3 for later use.

$objPHPExcel = PHPExcel_IOFactory::load('template.xlsx');
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('SomeVal');
// ...
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('filename.xlsx');

This is the code I usually use to save the files, but I dont't want to store the files on my application server, so I am going to store these in an S3 Bucket.

Here is the code I usually use to upload a file to S3:

$s3 = S3Client::factory(array(
    'key' => AWS_KEY,
    'secret' => AWS_SECRET
));
$result = $s3->putObject(array(
    'Bucket' => AWS_BUCKET,
    'Key' => 'somefileinmybucket.jpg',
    'SourceFile' => $filepath,
    'ACL' => 'private'
));

From my (limited) previous experience with these tools to me the only solution seems to be:

  1. Save the Excel document to a temporary location on my app server.
  2. Upload the file to S3 from the temporary location.
  3. Delete my temporary file.

My question is this: Is there a better way, where I wouldn't have to save it to the disk of my application server in between?

When this process has been initiated, it will be between 50 and 100 excel files that are generated, and should be uploaded to S3, so I'd like to streamline the process as much as possible.

Edit: The question that this could have been a duplicate only deals with S3, not PHPExcel. And it does not discuss PHP, which is what this question deals with.

like image 916
lshas Avatar asked Mar 17 '23 03:03

lshas


1 Answers

There is no shortcut method built into PHPExcel itself; but as PHPExcel allows you to write to any output stream supported by PHP, there's nothing to prevent you from using

$objWriter->save('s3://myS3bucket/filename.xlsx');

via the AWS S3 stream wrapper.

Rather than using

$s3->putObject(...);

to store objects

you can register the stream wrapper using

$s3->registerStreamWrapper();

and then most standard PHP file operations (such as fopen(), fwrite(), file_put_contents(), etc) can be used directly from your script, and PHPExcel can use this to write the data directly to the s3 bucket/file defined by that stream reference in the save

Please note that I haven't tried this myself, but see no reason why it shouldn't work in exactly the same way that using PHP's own built-in streams like php://output work

Edit by asker: I tried it, and at first I got this error thrown:

Could not close zip file s3://mybucket/test.xlsx

To fix it I had to do some adjustments in `PHPExcel/Writer/Excel2007.php``

if (strtolower($pFilename) == 'php://output' ||
    strtolower($pFilename) == 'php://stdout')
{

Switch this line out with this:

if (strtolower($pFilename) == 'php://output' || 
    strtolower($pFilename) == 'php://stdout' ||
    strstr($pFilename,'s3://') )
{

In my case it was in line 196. After this, it worked just fine.

like image 128
Mark Baker Avatar answered Mar 24 '23 10:03

Mark Baker