在 Laravel 中压缩和下载 Amazon S3 存储桶文件和文件夹
Zipping and downloading Amazon S3 bucket files and folders in Laravel
有没有办法将 Amazon S3 存储桶中的文件和文件夹一起压缩和下载到 Laravel 中?我想把图片中的三个文件夹一个文件一起压缩下载
这是路由文件中的半生不熟的解决方案。希望能帮助到你。
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 目录中。也许直接将其作为下载流出或将其保存在更好的地方。随时postcomment/questions,我们可以讨论。
在查看了一些解决方案后,我通过使用 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');
}
有没有办法将 Amazon S3 存储桶中的文件和文件夹一起压缩和下载到 Laravel 中?我想把图片中的三个文件夹一个文件一起压缩下载
这是路由文件中的半生不熟的解决方案。希望能帮助到你。 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 目录中。也许直接将其作为下载流出或将其保存在更好的地方。随时postcomment/questions,我们可以讨论。
在查看了一些解决方案后,我通过使用 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');
}