Laravel 5 Flysystem - download file from remote disk
Asked Answered
W

2

9

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

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?

Watcher answered 25/3, 2015 at 21:34 Comment(0)
W
28

Frank here from Flysystem.

The preferred way to do this would be to use the readStream output in combination with 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 $fs is the League\Flysystem\Filesystem instance. I believe there is a method to retrieve this instance in the filesystem class Laravel provides.

Wilder answered 20/4, 2015 at 11:48 Comment(5)
Hi Frank, thanks for this - would it be possible to update with an example known to work in Laravel? I'm a big lost with the Filesystem instance as it relates to LaravelWatcher
I've added it in the first line.Wilder
I can confirm that this works in Lumen, don't see any reason why it wouldn't in Laravel 5Wigwag
This example doesn't appear to work in Laravel 5.2. I get Call to undefined method Illuminate\Http\Response::stream(). Looking for a fix. EDIT: use response()->stream(...) with the same parameters. D'ohTicker
@DanielBuckmaster or use streamDownloadSteato
H
-4

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

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

Something like this should work:

return response()->download(Storage::disk('rackspace')->get('file-library/' . $file->filename));
Hendiadys answered 25/3, 2015 at 21:40 Comment(5)
is_file() expects parameter 1 to be a valid path, string given. Download's first parameter is a path (string)Watcher
@Watcher You may have to pull it down into a temporary file, I guess. Or just link the user straight to Rackspace's storage.Hendiadys
@ceeyaoz seems weird that they wouldn't have thought of this. I'd like to avoid having to mess with temporary files (the idea was to keep all files of this type off of my local file system) and was hoping to initiate a download without the user leaving the current page.Watcher
@Watcher Whenever I've needed to do this sort of thing I've just redirected the user to a AWS S3 signed URL for the download. No reason to involve the web server at all in the download that way. More scalable, faster, and easier.Hendiadys
@Watcher I don't use Rackspace, but they appear to have similar systems in place. docs.rackspace.com/files/api/v1/cf-devguide/content/…Hendiadys

© 2022 - 2024 — McMap. All rights reserved.