如何将文件从 Amazon S3 流式传输到 Zip 中
How to Stream files into a Zip from Amazon S3
我正在使用 the PHP Flysystem package to stream content from my Amazon S3 bucket. In particular, I'm using $filesystem->readStream
。
我的问题
当我流式传输文件时,它以 myzip.zip 结束并且大小正确,但是当解压缩它时,它变成 myzip.zip.cpgz.这是我的原型:
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');
我做错了什么?
附带问题
当我像这样流式传输文件时,是否:
- 完全下载到我服务器的内存中,然后传输到客户端,或者
- 它是否以块的形式保存在缓冲区中,然后传输到客户端?
基本上,如果我有几十 GB 的数据正在流式传输,我的服务器是否负担过重?
您目前正在将 directory/file.jpg
的原始内容转储为 zip(jpg 不是 zip)。您需要使用这些内容创建一个 zip 文件。
而不是
echo $s3->readStream('directory/file.jpg');
尝试使用 Zip extension 替换以下内容:
// 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);
使用 tmpfile
和 stream_copy_to_stream
,它将分块下载到磁盘上的临时文件而不是 RAM
我正在使用 the PHP Flysystem package to stream content from my Amazon S3 bucket. In particular, I'm using $filesystem->readStream
。
我的问题
当我流式传输文件时,它以 myzip.zip 结束并且大小正确,但是当解压缩它时,它变成 myzip.zip.cpgz.这是我的原型:
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');
我做错了什么?
附带问题
当我像这样流式传输文件时,是否:
- 完全下载到我服务器的内存中,然后传输到客户端,或者
- 它是否以块的形式保存在缓冲区中,然后传输到客户端?
基本上,如果我有几十 GB 的数据正在流式传输,我的服务器是否负担过重?
您目前正在将 directory/file.jpg
的原始内容转储为 zip(jpg 不是 zip)。您需要使用这些内容创建一个 zip 文件。
而不是
echo $s3->readStream('directory/file.jpg');
尝试使用 Zip extension 替换以下内容:
// 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);
使用 tmpfile
和 stream_copy_to_stream
,它将分块下载到磁盘上的临时文件而不是 RAM