Symfony.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. Usage with Symfony
  2. ===============
  3. Overview for using ZipStream in Symfony
  4. --------
  5. Using ZipStream in Symfony requires use of Symfony's ``StreamedResponse`` when
  6. used in controller actions.
  7. Wrap your call to the relevant ``ZipStream`` stream method (i.e. ``addFile``,
  8. ``addFileFromPath``, ``addFileFromStream``) in Symfony's ``StreamedResponse``
  9. function passing in any required arguments for your use case.
  10. Using Symfony's ``StreamedResponse`` will allow Symfony to stream output from
  11. ZipStream correctly to users' browsers and avoid a corrupted final zip landing
  12. on the users' end.
  13. Example for using ``ZipStream`` in a controller action to zip stream files
  14. stored in an AWS S3 bucket by key:
  15. .. code-block:: php
  16. use Symfony\Component\HttpFoundation\StreamedResponse;
  17. use Aws\S3\S3Client;
  18. use ZipStream;
  19. //...
  20. /**
  21. * @Route("/zipstream", name="zipstream")
  22. */
  23. public function zipStreamAction()
  24. {
  25. //sample test file on s3
  26. $s3keys = array(
  27. "ziptestfolder/file1.txt"
  28. );
  29. $s3Client = $this->get('app.amazon.s3'); //s3client service
  30. $s3Client->registerStreamWrapper(); //required
  31. //using StreamedResponse to wrap ZipStream functionality for files on AWS s3.
  32. $response = new StreamedResponse(function() use($s3keys, $s3Client)
  33. {
  34. // Define suitable options for ZipStream Archive.
  35. $options = new \ZipStream\Option\Archive();
  36. $options->setContentType('application/octet-stream');
  37. // this is needed to prevent issues with truncated zip files
  38. $options->setZeroHeader(true);
  39. $options->setComment('test zip file.');
  40. //initialise zipstream with output zip filename and options.
  41. $zip = new ZipStream\ZipStream('test.zip', $options);
  42. //loop keys - useful for multiple files
  43. foreach ($s3keys as $key) {
  44. // Get the file name in S3 key so we can save it to the zip
  45. //file using the same name.
  46. $fileName = basename($key);
  47. //concatenate s3path.
  48. $bucket = 'bucketname'; //replace with your bucket name or get from parameters file.
  49. $s3path = "s3://" . $bucket . "/" . $key;
  50. //addFileFromStream
  51. if ($streamRead = fopen($s3path, 'r')) {
  52. $zip->addFileFromStream($fileName, $streamRead);
  53. } else {
  54. die('Could not open stream for reading');
  55. }
  56. }
  57. $zip->finish();
  58. });
  59. return $response;
  60. }
  61. In the above example, files on AWS S3 are being streamed from S3 to the Symfon
  62. application via ``fopen`` call when the s3Client has ``registerStreamWrapper``
  63. applied. This stream is then passed to ``ZipStream`` via the
  64. ``addFileFromStream`` function, which ZipStream then streams as a zip to the
  65. client browser via Symfony's ``StreamedResponse``. No Zip is created server
  66. side, which makes this approach a more efficient solution for streaming zips to
  67. the client browser especially for larger files.
  68. For the above use case you will need to have installed
  69. `aws/aws-sdk-php-symfony <https://github.com/aws/aws-sdk-php-symfony>`_ to
  70. support accessing S3 objects in your Symfony web application. This is not
  71. required for locally stored files on you server you intend to stream via
  72. ``ZipStream``.
  73. See official Symfony documentation for details on
  74. `Symfony's StreamedResponse <https://symfony.com/doc/current/components/http_foundation.html#streaming-a-response>`_
  75. ``Symfony\Component\HttpFoundation\StreamedResponse``.
  76. Note from `S3 documentation <https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-stream-wrapper.html>`_:
  77. Streams opened in "r" mode only allow data to be read from the stream, and
  78. are not seekable by default. This is so that data can be downloaded from
  79. Amazon S3 in a truly streaming manner, where previously read bytes do not
  80. need to be buffered into memory. If you need a stream to be seekable, you
  81. can pass seekable into the stream context options of a function.
  82. Make sure to configure your S3 context correctly!
  83. Uploading a file
  84. --------
  85. You need to add correct permissions
  86. (see `#120 <https://github.com/maennchen/ZipStream-PHP/issues/120>`_)
  87. **example code**
  88. .. code-block:: php
  89. $path = "s3://{$adapter->getBucket()}/{$this->getArchivePath()}";
  90. // the important bit
  91. $outputContext = stream_context_create([
  92. 's3' => ['ACL' => 'public-read'],
  93. ]);
  94. fopen($path, 'w', null, $outputContext);