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:
- Save the Excel document to a temporary location on my app server.
- Upload the file to S3 from the temporary location.
- 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.
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
via the AWS S3 stream wrapper.
Rather than using
to store objects
you can register the stream wrapper using
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 savePlease 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
workEdit by asker: I tried it, and at first I got this error thrown:
To fix it I had to do some adjustments in `PHPExcel/Writer/Excel2007.php``
Switch this line out with this:
In my case it was in line 196. After this, it worked just fine.