How to stream an large file from S3 to a laravel view

TJ Sherrill

I have this mostly working but having a tough time finalizing it.

For now I have a simple route:

Route::get('file/{id}/', 'FileController@fileStream')->name('file');

this route connects to an action in the FileController:

public function fileStream($id){

    $audio = \App\Audio::where('id', $id)->first();

    $client = S3Client::factory([
        'credentials' => [
            'key'    => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
        ],
        'region' => env('S3REGION'),
        'version' => 'latest',
    ]);


    // Register the stream wrapper from an S3Client object
    $client->registerStreamWrapper();

    if ($stream = fopen('s3://[bucket_name]/'. $audio->audio_url, 'r')) {
        while (!feof($stream)) {
            echo fread($stream, 1024);
        }
        fclose($stream);
    }    
}

This works to the browser: if I go to a url: /file/1 it looks up the right file, and in a clean browser window I get:

enter image description here

And then in my view I am trying to output the audio like:

   <audio>
      <source src="{{ url('file', ['id' => $section->id]) }}" type="{{ $section->audio_mime_type}}"></audio>
   </audio>

But no player is getting output to the screen.

TIA

Hieu Le

You should use Laravel Streamed response

return response()->streamDownload(function () use ($audio) {
    if ($stream = fopen('s3://[bucket_name]/'. $audio->audio_url, 'r')) {
        while (!feof($stream)) {
            echo fread($stream, 1024);
            flush();
        }
        fclose($stream);
    }    
}, 'file-name.ext');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read large JSON file from Amazon S3 using Boto3

From Dev

How can I safely upload a large file from the browser to a bucket on Amazon S3?

From Dev

How to link a large file from S3 to a CakePHP 2.x request?

From Dev

Go: How to send a File from AWS S3 via GIN-Router as binary stream to the browser?

From Dev

How do I upload a file to S3 from GuzzleHttp\Psr7\Stream?

From Dev

How would I write JSON to a S3 file from an input stream without loading it all into memory?

From Dev

Upload a file stream to S3 without a file and from memory

From Dev

Sax parsing a large file from S3

From Dev

Alternative to loading large file from s3

From Dev

How can I read .txt file from S3 bucket using python and view the contents?

From Dev

How to upload very large file to S3?

From Dev

How to download a large S3 file with AWS Lambda (javascript)

From Dev

How to read file from s3?

From Dev

How to upload image file to Stripe from Amazon S3 using Laravel 5.5 and Intervention Image

From Dev

Stream a large file from http service

From Dev

How to Stream files into a Zip from AWS S3

From Dev

How to read data from s3 bucket to Kinesis Stream

From Java

Spring WebClient: How to stream large byte[] to file?

From Dev

Download file from cloud disk (S3) with Laravel 5.1

From Dev

Retrieve tags from a file in AWS S3 with Laravel

From Dev

How to view images from S3 bucket to my angular

From Dev

File.Stream for a file on S3?

From Dev

Unable to restore SQL Server bak file from S3, says file too large

From Dev

How to intercept a new file on S3 using Laravel Queues?

From Dev

Laravel How to get size of a file stored in AWS S3

From Dev

How do I stream a large file from api to api without using disk and running out of memory?

From Dev

Stream a large file from URL straight into a gzip file

From Dev

Laravel S3 retreiving a Video to Stream

From Dev

Stream large string to S3 using boto3

Related Related

  1. 1

    How to read large JSON file from Amazon S3 using Boto3

  2. 2

    How can I safely upload a large file from the browser to a bucket on Amazon S3?

  3. 3

    How to link a large file from S3 to a CakePHP 2.x request?

  4. 4

    Go: How to send a File from AWS S3 via GIN-Router as binary stream to the browser?

  5. 5

    How do I upload a file to S3 from GuzzleHttp\Psr7\Stream?

  6. 6

    How would I write JSON to a S3 file from an input stream without loading it all into memory?

  7. 7

    Upload a file stream to S3 without a file and from memory

  8. 8

    Sax parsing a large file from S3

  9. 9

    Alternative to loading large file from s3

  10. 10

    How can I read .txt file from S3 bucket using python and view the contents?

  11. 11

    How to upload very large file to S3?

  12. 12

    How to download a large S3 file with AWS Lambda (javascript)

  13. 13

    How to read file from s3?

  14. 14

    How to upload image file to Stripe from Amazon S3 using Laravel 5.5 and Intervention Image

  15. 15

    Stream a large file from http service

  16. 16

    How to Stream files into a Zip from AWS S3

  17. 17

    How to read data from s3 bucket to Kinesis Stream

  18. 18

    Spring WebClient: How to stream large byte[] to file?

  19. 19

    Download file from cloud disk (S3) with Laravel 5.1

  20. 20

    Retrieve tags from a file in AWS S3 with Laravel

  21. 21

    How to view images from S3 bucket to my angular

  22. 22

    File.Stream for a file on S3?

  23. 23

    Unable to restore SQL Server bak file from S3, says file too large

  24. 24

    How to intercept a new file on S3 using Laravel Queues?

  25. 25

    Laravel How to get size of a file stored in AWS S3

  26. 26

    How do I stream a large file from api to api without using disk and running out of memory?

  27. 27

    Stream a large file from URL straight into a gzip file

  28. 28

    Laravel S3 retreiving a Video to Stream

  29. 29

    Stream large string to S3 using boto3

HotTag

Archive