Laravel 5 Flysystem - 从远程磁盘下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29266974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:13:19  来源:igfitidea点击:

Laravel 5 Flysystem - download file from remote disk

phplaravellaravel-5flysystem

提问by NightMICU

I am storing files for a site on Rackspace using Flysystem. Uploading is no problem, having trouble figuring out how to start a download for a file - this is what I have tried

我正在使用 Flysystem 为 Rackspace 上的站点存储文件。上传没问题,无法弄清楚如何开始下载文件 - 这是我尝试过的

Storage::disk('rackspace');
return response()->download('file-library/' . $file->filename);

The result is that the file could not be found. Is adding Storage::disk()sufficient for making Laravel look in this location rather than locally? What is the best way to accomplish this?

结果是找不到文件。添加是否Storage::disk()足以使 Laravel 看起来在这个位置而不是本地?实现这一目标的最佳方法是什么?

回答by Frank de Jonge

Frank here from Flysystem.

弗兰克来自 Flysystem。

The preferred way to do this would be to use the readStream output in combination with Response::stream.

执行此操作的首选方法是将 readStream 输出与 Response::stream 结合使用。

<?php

$fs = Storage::disk('diskname')->getDriver();
$stream = $fs->readStream($file);

return Response::stream(function() use($stream) {
    fpassthru($stream);
}, 200, [
    "Content-Type" => $fs->getMimetype($file),
    "Content-Length" => $fs->getSize($file),
    "Content-disposition" => "attachment; filename=\"" . basename($file) . "\"",
]);

The $fsis the League\Flysystem\Filesysteminstance. I believe there is a method to retrieve this instance in the filesystem class Laravel provides.

$fsLeague\Flysystem\Filesystem实例。我相信有一种方法可以在 Laravel 提供的文件系统类中检索此实例。

回答by ceejayoz

Is adding Storage::disk()sufficient for making Laravel look in this location rather than locally?

添加是否Storage::disk()足以使 Laravel 看起来在这个位置而不是本地?

No, that wouldn't affect response()->download()calls.

不,这不会影响response()->download()通话。

Something like this should work:

这样的事情应该工作:

return response()->download(Storage::disk('rackspace')->get('file-library/' . $file->filename));