我正在检查压缩S3流内容的好的解决方案,我遇到了PHP,它已经被PHP使用过,但是我猜在核心ZipStream-PHP类ZipArchive和它的函数ZipArchive::addFromString的帮助下,我们可以实现同样的效果。
我的问题是,对于从S3或任何其他云服务压缩流内容,PHP API是比ZipArchive更好的解决方案吗?
发布于 2017-06-23 19:14:33
根据我的经验,最好的解决方案是使用aws-sdk-php通过启用了registerStreamWrapper()的s3client访问S3上的对象。然后使用fopen从S3流式传输对象,并将该流直接提供给ZipStream的addFileFromStream()函数,然后让ZipStream从那里获取它。没有ZipArchive,没有巨大的内存开销,没有在服务器上创建压缩文件,也没有在web服务器上复制来自S3的文件,以便随后用于流式压缩。
所以:
//...
$s3Client->registerStreamWrapper(); //required
//test files on s3
$s3keys = array(
"ziptestfolder/file1.txt",
"ziptestfolder/file2.txt"
);
// 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';
$s3path = "s3://" . $bucket . "/" . $key;
//addFileFromStream
if ($streamRead = fopen($s3path, 'r')) {
$zip->addFileFromStream($fileName, $streamRead);
} else {
die('Could not open stream for reading');
}
}
$zip->finish();如果您在Symfony控制器操作中使用ZipStream,也请参阅此答案:https://stackoverflow.com/a/44706446/136151
https://stackoverflow.com/questions/39317392
复制相似问题