4

I'm storing videos to Amazon S3 via my Laravel app. That works great. But I can't "stream" them.

This is the URL for example: https://website.com/video/342.qt?api_token=a5a18c9f-f5f6-5d66-85e3-aaaaaaa, what should return this movie from S3 called '212.DdsqoK1PlL.qt'

It returns this output when calling the URL: Output of Video URL That's the video, but I was expecting it to run directly in the browser, like this video does: https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4

The route calls this function, retrieving the non-public file from the S3-disk:

public function document(Document $document)
{
    return Storage::disk('s3')->get($document->path);
}

The only difference between the example URL that works and mine is that the example is MP4 and mine .QT, but I tried MP4 also and go the same output in the browser; so no autoplaying video.

I guess the movie that plays directly is streaming the video?..

My website is running on Ubuntu and installed also sudo apt-get install vlc.

user1469734
  • 851
  • 14
  • 50
  • 81
  • Instead of getting the file here, use your s3 file url and make a redirect. – Eduardo Stuart Sep 22 '17 at 11:52
  • @EduardoStuart where to get that URL? Files are listed non-public. – user1469734 Sep 22 '17 at 11:57
  • You can create an authenticated url (temporary) OR make your file public. https://laravel.com/docs/5.5/filesystem#storing-files (visibility section) OR $url = Storage::temporaryUrl( 'file1.jpg', Carbon::now()->addMinutes(5) ); – Eduardo Stuart Sep 22 '17 at 12:00

2 Answers2

11

I personally am opposed to the idea of redirecting to an S3 URL. I mask all my URLs through a Laravel php wrapper server-side. This is the code I use to do so if anyone else encounters similar issues. This code is written for streaming a video from S3 with Laravel 5.6 (and includes HTTP_RANGE support so it works on iOS too).

I use the class below, placed at App/Http/Responses. To use this class, create a method that does this (this is like a getFile method):

$filestream = new \App\Http\Responses\S3FileStream('file_path_and_name_within_bucket', 'disk_bucket_name', 'output_file_name_when_downloaded');
return $filestream->output();

With any luck, you should be streaming in no time (without revealing an S3 URL)!

S3FileStream.php: <?php

namespace Http\Responses;

use Exception;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;

class S3FileStream
{
    /**
     * @var \League\Flysystem\AwsS3v3\AwsS3Adapter
     */
    private $adapter;

    /**
     * Name of adapter
     *
     * @var string
     */
    private $adapterName;

    /**
     * Storage disk
     *
     * @var FilesystemAdapter
     */
    private $disk;

    /**
     * @var int file end byte
     */
    private $end;

    /**
     * @var string
     */
    private $filePath;

    /**
     * Human-known filename
     *
     * @var string|null
     */
    private $humanName;

    /**
     * @var bool storing if request is a range (or a full file)
     */
    private $isRange = false;

    /**
     * @var int|null length of bytes requested
     */
    private $length = null;

    /**
     * @var array
     */
    private $returnHeaders = [];

    /**
     * @var int file size
     */
    private $size;

    /**
     * @var int start byte
     */
    private $start;

    /**
     * S3FileStream constructor.
     * @param string $filePath
     * @param string $adapter
     * @param string $humanName
     */
    public function __construct(string $filePath, string $adapter = 's3', ?string $humanName = null)
    {
        $this->filePath    = $filePath;
        $this->adapterName = $adapter;
        $this->disk        = Storage::disk($this->adapterName);
        $this->adapter     = $this->disk->getAdapter();
        $this->humanName   = $humanName;
        //Set to zero until setHeadersAndStream is called
        $this->start = 0;
        $this->size  = 0;
        $this->end   = 0;
    }

    /**
     * Output file to client.
     */
    public function output()
    {
        return $this->setHeadersAndStream();
    }

    /**
     * Output headers to client.
     * @return Response|StreamedResponse
     */
    protected function setHeadersAndStream()
    {
        if (!$this->disk->exists($this->filePath)) {
            report(new Exception('S3 File Not Found in S3FileStream - ' . $this->adapterName . ' - ' . $this->disk->path($this->filePath)));
            return response('File Not Found', 404);
        }

        $this->start   = 0;
        $this->size    = $this->disk->size($this->filePath);
        $this->end     = $this->size - 1;
        $this->length  = $this->size;
        $this->isRange = false;

        //Set headers
        $this->returnHeaders = [
            'Last-Modified'       => $this->disk->lastModified($this->filePath),
            'Accept-Ranges'       => 'bytes',
            'Content-Type'        => $this->disk->mimeType($this->filePath),
            'Content-Disposition' => 'inline; filename=' . ($this->humanName ?? basename($this->filePath) . '.' . Arr::last(explode('.', $this->filePath))),
            'Content-Length'      => $this->length,
        ];

        //Handle ranges here
        if (!is_null(request()->server('HTTP_RANGE'))) {
            $cStart = $this->start;
            $cEnd   = $this->end;

            $range = Str::after(request()->server('HTTP_RANGE'), '=');
            if (strpos($range, ',') !== false) {
                return response('416 Requested Range Not Satisfiable', 416, [
                    'Content-Range' => 'bytes */' . $this->size,
                ]);
            }
            if (substr($range, 0, 1) == '-') {
                $cStart = $this->size - intval(substr($range, 1)) - 1;
            } else {
                $range  = explode('-', $range);
                $cStart = intval($range[0]);

                $cEnd = (isset($range[1]) && is_numeric($range[1])) ? intval($range[1]) : $cEnd;
            }

            $cEnd = min($cEnd, $this->size - 1);
            if ($cStart > $cEnd || $cStart > $this->size - 1) {
                return response('416 Requested Range Not Satisfiable', 416, [
                    'Content-Range' => 'bytes */' . $this->size,
                ]);
            }

            $this->start                           = intval($cStart);
            $this->end                             = intval($cEnd);
            $this->length                          = min($this->end - $this->start + 1, $this->size);
            $this->returnHeaders['Content-Length'] = $this->length;
            $this->returnHeaders['Content-Range']  = 'bytes ' . $this->start . '-' . $this->end . '/' . $this->size;
            $this->isRange                         = true;
        }

        return $this->stream();
    }

    /**
     * Stream file to client.
     * @throws Exception
     * @return StreamedResponse
     */
    protected function stream(): StreamedResponse
    {
        $this->adapter->getClient()->registerStreamWrapper();
        // Create a stream context to allow seeking
        $context = stream_context_create([
            's3' => [
                'seekable' => true,
            ],
        ]);
        // Open a stream in read-only mode
        if (!($stream = fopen("s3://{$this->adapter->getBucket()}/{$this->filePath}", 'rb', false, $context))) {
            throw new Exception('Could not open stream for reading export [' . $this->filePath . ']');
        }
        if (isset($this->start) && $this->start > 0) {
            fseek($stream, $this->start, SEEK_SET);
        }

        $remainingBytes = $this->length ?? $this->size;
        $chunkSize      = 100;

        $video = response()->stream(
            function () use ($stream, $remainingBytes, $chunkSize) {
                while (!feof($stream) && $remainingBytes > 0) {
                    $toGrab = min($chunkSize, $remainingBytes);
                    echo fread($stream, $toGrab);
                    $remainingBytes -= $toGrab;
                    flush();
                }
                fclose($stream);
            },
            ($this->isRange ? 206 : 200),
            $this->returnHeaders
        );

        return $video;
    }
}
Jmorko
  • 382
  • 3
  • 16
  • For my use case I changed the `Content-Disposition`-Header to `inline` instead of attachment to play the video directly in the browser instead of downloading it. – rambii Nov 23 '18 at 09:51
  • It should be noted that this also worked for me with Digital Ocean Spaces, which use the same API as S3 – Matthew Daly Mar 03 '20 at 10:16
  • We originally used this to solve the exact same issue, but have found it’s recently stopped working for iOS/Safari. It does continue to work for other browsers though (i.e. I can see that chrome gets a 206 response when I access the file there. Has anybody else found the same thing? Unsure where the cause of this issue is right now – Joe Dec 11 '20 at 20:23
  • 1
    Updated the answer with the latest code I use, @Joe! Please let me know if it works – Jmorko Dec 12 '20 at 22:24
  • @Jmorko — it turned out that the issue was a server configuration where our servers were returning a Connection header over an HTTP/2 connection, which Safari apparently just rejects inline with the HTTP/2 standard (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection). Your update looks like it has some nice improvements though, so I may well borrow that for the human name and other improvements you've made, thank you! – Joe Dec 14 '20 at 13:09
  • @Jmorko - Have you made this work on Laravel 9? It seems that it fails. I made a question about it but no one could answer. https://stackoverflow.com/questions/72433129/laravel-9-aws-s3-retreiving-a-video-to-stream – Scorekaj22 May 30 '22 at 17:25
0

Like I said on the comments section. I think you should use S3 url (temporary, or public).

You have some options here:

  1. Use laravel temporary url;
  2. Set your file as public + get url;

For more information: https://laravel.com/docs/5.5/filesystem#storing-files

To set your file visibility as public:

Storage::setVisibility('file.jpg', 'public')

Temporary URL:

$url = Storage::temporaryUrl(
    'file1.jpg', Carbon::now()->addMinutes(5)
);

If your file is public, you can use:

Storage::url('file1.jpg');
Eduardo Stuart
  • 2,869
  • 18
  • 22