[英]How to Stream files into a Zip from AWS S3
I'm using the PHP Flysystem package to stream content from my AWS S3 bucket. 我正在使用PHP Flysystem包来传输来自我的AWS S3存储桶的内容。 In particular, I'm using
$filesystem->readStream
. 特别是,我正在使用
$filesystem->readStream
。
My Question 我的问题
When I stream a file, it ends up in myzip.zip and the size is correct, but when unzip it, it become myzip.zip.cpgz . 当我流式传输文件时,它最终会出现在myzip.zip中并且大小正确,但是当解压缩它时,它会变成myzip.zip.cpgz 。 Here is my prototype:
这是我的原型:
header('Pragma: no-cache');
header('Content-Description: File Download');
header('Content-disposition: attachment; filename="myZip.zip"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
$s3 = Storage::disk('s3'); // Laravel Syntax
echo $s3->readStream('directory/file.jpg');
What am I doing wrong? 我究竟做错了什么?
Side Question 边问题
When I stream a file like this, does it: 当我像这样流式传输文件时,它是这样的:
Basically, is my server being burdened if I have have dozens of GB's of data being streamed? 基本上,如果我有数十GB的数据被流式传输,我的服务器是否负担沉重?
You are currently dumping the raw contents of the directory/file.jpg
as the zip (which a jpg is not a zip) . 您当前正在转储
directory/file.jpg
的原始内容作为zip(jpg不是zip)。 You need to create a zip file with those contents. 您需要创建包含这些内容的zip文件。
Instead of 代替
echo $s3->readStream('directory/file.jpg');
Try the following in its place using the Zip extension : 使用Zip扩展名尝试以下内容:
// use a temporary file to store the Zip file
$zipFile = tmpfile();
$zipPath = stream_get_meta_data($zipFile)['uri'];
$jpgFile = tmpfile();
$jpgPath = stream_get_meta_data($jpgFile)['uri'];
// Download the file to disk
stream_copy_to_stream($s3->readStream('directory/file.jpg'), $jpgFile);
// Create the zip file with the file and its contents
$zip = new ZipArchive();
$zip->open($zipPath);
$zip->addFile($jpgPath, 'file.jpg');
$zip->close();
// export the contents of the zip
readfile($zipPath);
Using tmpfile
and stream_copy_to_stream
, it will download it in chunks to a temporary file on disk and not into RAM 使用
tmpfile
和stream_copy_to_stream
,它将以块的形式下载到磁盘上的临时文件而不是RAM中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.