在Laravel中压缩和下载Amazon S3 bucket文件和文件夹
有没有办法将AmazonS3存储桶中的文件和文件夹压缩并下载到Laravel中?我想将图片中的三个文件夹和一个文件压缩到一起并下载在Laravel中压缩和下载Amazon S3 bucket文件和文件夹,laravel,amazon-s3,zip,zipper,Laravel,Amazon S3,Zip,Zipper,有没有办法将AmazonS3存储桶中的文件和文件夹压缩并下载到Laravel中?我想将图片中的三个文件夹和一个文件压缩到一起并下载 在路由文件中有一个半生不熟的解决方案。希望能有帮助。 我把它放在routes/web.php中只是为了玩 <?php use Illuminate\Support\Facades\Storage; use League\Flysystem\Filesystem; use League\Flysystem\ZipArchive\Z
在路由文件中有一个半生不熟的解决方案。希望能有帮助。 我把它放在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');
});
在查看了一些解决方案后,我使用以下方法将zip直接流式传输到客户端:
您想要一次或作为laravel中的一项功能来压缩s3文件吗?作为laravel中的一项功能,文件正在压缩,但路径中的文件夹没有压缩。。。在这三个文件夹中,还有其他包含文件的文件夹…仅压缩文件,而不是压缩文件folders@TauseefMamun在我的解决方案中,我使用了files()。。。这里有一个递归版本allfiles()@TauseefMamun看看我对这个问题的答案(不是最上面的答案)@TarekAdam也有办法在S3中保存zip吗?@user1670816将public_path('archive.zip')
更改为app_path('archive.zip'),然后使用这两行Storage::disk('S3')->writeStream('archive2.zip',Storage::readStream('archive.zip');存储::磁盘('local')->删除('archive.zip')然后重定向到其他地方,因为重定向('archive.zip')将不存在。基本上,这是在本地创建一个zip并将其推送到s3。很抱歉,我无法在s3上构建zip,如果你发布一个新问题,也许有人会做得更好。仅供参考,您需要在project/config/filesystems.php中配置s3
<?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');
});
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');
}