在 Laravel 中压缩和下载 Amazon S3 存储桶文件和文件夹 [英] Zipping and downloading Amazon S3 bucket files and folders in Laravel

查看:26
本文介绍了在 Laravel 中压缩和下载 Amazon S3 存储桶文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Laravel 中压缩和下载 Amazon S3 存储桶中的文件和文件夹?我想把图片中的三个文件夹和一个文件压缩在一起下载

Is there a way to zip and download files and folders which are in Amazon S3 bucket, together in Laravel? I Want to zip the three folders and one file in the picture together and download it

推荐答案

这是一个路由文件中的半成品解决方案.希望能帮助到你.https://flysystem.thephpleague.com/docs/adapter/zip-archive/

Here's a half baked solution in a route file. Hope it helps. https://flysystem.thephpleague.com/docs/adapter/zip-archive/

    composer require league/flysystem-ziparchive

我把它放在 routes/web.php 中只是为了玩.

I put this in routes/web.php just to play with.

<?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');

    });

您肯定想做一些与将它放在公共目录中不同的事情.也许将其作为下载直接流式传输或将其保存在更好的地方.随时发表评论/问题,我们可以讨论.

You'll definitely want to do something different than just plopping it in the public dir. Maybe stream it out straight out as a download or save it somewhere better. Feel free to post comment/questions and we can discuss.

这篇关于在 Laravel 中压缩和下载 Amazon S3 存储桶文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆