将PHPExcel文件存储在Amazon S3中 [英] Store PHPExcel files in Amazon S3

查看:157
本文介绍了将PHPExcel文件存储在Amazon S3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以创建许多Excel文件的应用程序,我希望将其存储在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');

这是我通常用于保存文件的代码,但是我不想将文件存储在我的应用程序服务器上,因此我要将它们存储在S3存储桶中.

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.

这是我通常用于将文件上传到S3的代码:

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. 将Excel文档保存到我的应用服务器上的临时位置.
  2. 从临时位置将文件上传到S3.
  3. 删除我的临时文件.

我的问题是:有没有更好的方法,我之间不必将其保存到应用程序服务器的磁盘中?

启动此过程后,将生成50到100个excel文件,这些文件应上传到S3,所以我想尽可能地简化该过程.

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.

该问题可能是重复的,仅适用于S3,而不适用于PHPExcel.而且它没有讨论PHP,这就是这个问题要解决的问题.

推荐答案

PHPExcel本身没有内置快捷方式;但是由于PHPExcel允许您写入PHP支持的任何输出流,所以没有什么可以阻止您使用

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');

通过AWS S3流包装器.

via the AWS S3 stream wrapper.

而不是使用

$s3->putObject(...);

存储对象

您可以使用来注册流包装器

you can register the stream wrapper using

$s3->registerStreamWrapper();

,然后可以从脚本中直接使用大多数标准的PHP文件操作(例如fopen()fwrite()file_put_contents()等),PHPExcel可以使用此操作将数据直接写入s3存储桶/该流引用在保存中定义的文件

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

请注意,我自己还没有尝试过,但是没有理由不认为它不能与使用PHP自己的内置流(如php://output work)完全相同的方式

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:

无法关闭zip文件s3://mybucket/test.xlsx

要解决此问题,我必须在`PHPExcel/Writer/Excel2007.php``中进行一些调整

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

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

将此行切换为此:

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

在我的情况下,它位于第196行. 在此之后,它运行良好.

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

这篇关于将PHPExcel文件存储在Amazon S3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆