【发布时间】:2019-07-18 05:56:10
【问题描述】:
【问题讨论】:
-
你想要它一次或作为 laravel 中的一项功能来压缩 s3 文件?
-
作为 laravel 中的一个功能
标签: laravel amazon-s3 zip zipper
【问题讨论】:
标签: laravel amazon-s3 zip zipper
这是路由文件中的一个半生不熟的解决方案。希望能帮助到你。 https://flysystem.thephpleague.com/docs/adapter/zip-archive/
composer require league/flysystem-ziparchive
我把它放在 routes/web.php 中只是为了玩。
<?php
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
Route::get('zip', function(){
// see laravel's config/filesystem.php for the source disk
$source_disk = 's3';
$source_path = '';
$file_names = Storage::disk($source_disk)->files($source_path);
$zip = new Filesystem(new ZipArchiveAdapter(public_path('archive.zip')));
foreach($file_names as $file_name){
$file_content = Storage::disk($source_disk)->get($file_name);
$zip->put($file_name, $file_content);
}
$zip->getAdapter()->getArchive()->close();
return redirect('archive.zip');
});
您肯定会想做一些不同的事情,而不仅仅是将它放在公共目录中。也许直接将其作为下载流出来或将其保存在更好的地方。随时发表评论/问题,我们可以讨论。
【讨论】:
public_path('archive.zip') 更改为app_path('archive.zip') 然后使用这两行Storage::disk('s3')->writeStream('archive2.zip', Storage::readStream('archive.zip')); Storage::disk('local')->delete('archive.zip'); 然后重定向到其他地方,因为redirect('archive.zip') 将不存在。基本上这是在本地创建一个 zip 并将其推到 s3。抱歉,我无法在 s3 上构建 zip,也许如果您发布一个新问题,有人会做得更好。仅供参考,您需要在 project/config/filesystems.php 中配置 s3
在查看了一些解决方案后,我通过使用https://github.com/maennchen/ZipStream-PHP 将 zip 直接流式传输到客户端,然后按照以下方式进行了操作:
if ($uploads) {
return response()->streamDownload(function() use ($uploads) {
$opt = new ArchiveOptions();
$opt->setContentType('application/octet-stream');
$zip = new ZipStream("uploads.zip", $opt);
foreach ($uploads as $upload) {
try {
$file = Storage::readStream($upload->path);
$zip->addFileFromStream($upload->filename, $file);
}
catch (Exception $e) {
\Log::error("unable to read the file at storage path: $upload->path and output to zip stream. Exception is " . $e->getMessage());
}
}
$zip->finish();
}, 'uploads.zip');
}
【讨论】: