使用php从存储在S3上的文件动态创建zip文件

时间:2016-07-08 08:17:02

标签: php amazon-s3 zipfile

我有一个Laravel网络应用,用户可以在其中上传文件。这些文件可能很敏感,虽然它们存储在S3上,但只能通过我的网络服务器访问(流下载)。上传后,用户可能希望下载这些文件的选择。

以前,当用户下载一系列文件时,我的网络服务器会从S3下载文件,在本地压缩,然后将压缩文件发送到客户端。但是,由于文件大小一旦投入生产,服务器响应会经常超时。

作为一种替代方法,我想通过ZipStream动态压缩文件,但我没有太多运气。该zip文件最终会导致文件损坏,或者自身损坏并且非常小。

如果可以将S3上的文件的流资源传递给ZipStream,解决超时问题的最佳方法是什么?

我尝试了几种方法,我最近的两种方法如下:

// First method using fopen
// Results in tiny corrupt zip files
if (!($fp = fopen("s3://{$bucket}/{$key}", 'r')))
{
    die('Could not open stream for reading');
}

$zip->addFileFromPath($file->orginal_filename, "s3://{$bucket}/{$key}");
fclose($fp);


// Second method tried get download the file from s3 before sipping
// Results in a reasonable sized zip file that is corrupt
$contents = file_get_contents("s3://{$bucket}/{$key}");

$zip->addFile($file->orginal_filename, $contents); 

每个都位于遍历每个文件的循环中。在循环之后我调用$ zip-> finish()。

注意我没有得到任何php错误只是损坏文件。

2 个答案:

答案 0 :(得分:4)

最后,解决方案是使用已签名的S3网址和curl来为s3 bucket steam zip php所示的ZipStream提供文件流。从上述来源编辑的结果代码如下:

public function downloadZip()
{
    // ...

    $s3 = Storage::disk('s3');
    $client = $s3->getDriver()->getAdapter()->getClient();
    $client->registerStreamWrapper();
    $expiry = "+10 minutes";

    // Create a new zipstream object
    $zip = new ZipStream($zipName . '.zip');

    foreach($files as $file)
    {
        $filename = $file->original_filename;

        // We need to use a command to get a request for the S3 object
        //  and then we can get the presigned URL.
        $command = $client->getCommand('GetObject', [
            'Bucket' => config('filesystems.disks.s3.bucket'),
            'Key' => $file->path()
        ]);

        $signedUrl = $request = $client->createPresignedRequest($command, $expiry)->getUri();

        // We want to fetch the file to a file pointer so we create it here
        //  and create a curl request and store the response into the file
        //  pointer.
        // After we've fetched the file we add the file to the zip file using
        //  the file pointer and then we close the curl request and the file
        //  pointer.
        // Closing the file pointer removes the file.
        $fp = tmpfile();
        $ch = curl_init($signedUrl);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($ch);
        curl_close($ch);
        $zip->addFileFromStream($filename, $fp);
        fclose($fp);
    }

    $zip->finish();
}

请注意,这需要在服务器上安装并运行curl和php-curl。

答案 1 :(得分:1)

我遇到了与@cubiclewar相同的问题,并进行了一些调查。我发现最新的解决方案不需要卷曲,并且可以在 maennchen / ZipStream-PHP / 库的Wiki上看到它。

https://github.com/maennchen/ZipStream-PHP/wiki/Symfony-example

use ZipStream;

//...

/**
 * @Route("/zipstream", name="zipstream")
 */
public function zipStreamAction()
{
    //sample test file on s3
    $s3keys = array(
      "ziptestfolder/file1.txt"
    );

    $s3Client = $this->get('app.amazon.s3'); //s3client service
    $s3Client->registerStreamWrapper(); //required

    //using StreamedResponse to wrap ZipStream functionality for files on AWS s3.
    $response = new StreamedResponse(function() use($s3keys, $s3Client)
    {

        // Define suitable options for ZipStream Archive.
        $opt = array(
                'comment' => 'test zip file.',
                'content_type' => 'application/octet-stream'
              );

        //initialise zipstream with output zip filename and options.
        $zip = new ZipStream\ZipStream('test.zip', $opt);

        //loop keys - useful for multiple files
        foreach ($s3keys as $key) {
            // Get the file name in S3 key so we can save it to the zip
            //file using the same name.
            $fileName = basename($key);

            //concatenate s3path.
            $bucket = 'bucketname'; //replace with your bucket name or get from parameters file.
            $s3path = "s3://" . $bucket . "/" . $key;

            //addFileFromStream
            if ($streamRead = fopen($s3path, 'r')) {
              $zip->addFileFromStream($fileName, $streamRead);
            } else {
              die('Could not open stream for reading');
            }
        }

        $zip->finish();

    });

    return $response;
}