Search code examples
amazon-s3audio-streaming

How can I stream audio files (mp3) from Aws s3?


I have a simple javascript player, and I would like to stream audio files (mp3) from aws s3 sdk (Php). The files are private so I should also do the authentication.

If I banally insert the public link of the mp3 file into the src of the html object, the stream works.

Tnx


Solution

  • You don't need to stream through php backend. You can generate Presigned url with php code and send it to the front end. It will be accessible for the limited time.

    Sample PHP Code:

    //Creating a presigned request
    $s3Client = new Aws\S3\S3Client([
        'profile' => 'default',
        'region'  => 'us-east-1',
        'version' => '2006-03-01',
    ]);
    
    $cmd = $s3Client->getCommand('GetObject', [
        'Bucket' => 'my-bucket',
        'Key'    => 'testKey'
    ]);
    
    $request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
    $presignedUrl = (string) $request->getUri();
    

    Reference:

    https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-presigned-url.html

    Hope it helps.