Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Laravel中压缩和下载Amazon S3 bucket文件和文件夹_Laravel_Amazon S3_Zip_Zipper - Fatal编程技术网

在Laravel中压缩和下载Amazon S3 bucket文件和文件夹

在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

有没有办法将AmazonS3存储桶中的文件和文件夹压缩并下载到Laravel中?我想将图片中的三个文件夹和一个文件压缩到一起并下载


在路由文件中有一个半生不熟的解决方案。希望能有帮助。

我把它放在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')<?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');
}