How to generate presigned URL DO Spaces in Laravel

January 28, 2018 1.2k views
DigitalOcean

Hi!

really excited to try digital ocean spaces!
I'm currently using Laravel and aws s3 for streaming and download video on my site
i'm using aws-sdk-php-laravel right now for generating presigned url for my videos (not the phpleague flysystem)

my question is, how can do this with digitalocean spaces, or rather
how can i easily move to do spaces using the same sdk?

1 Answer

Spaces was designed to be inter-operable with the S3 API (for more details see here). This includes the ability to presign URLs. The only difference when porting from a code base originally targeting S3 will be configuring the correct endpoint (e.g. $REGION.digitaloceanspaces.com).

Here's a quick example with vanilla PHP:

<?php

// Included aws/aws-sdk-php via Composer's autoloader
// Installed with: composer.phar require aws/aws-sdk-php
require 'vendor/autoload.php';
use Aws\S3\S3Client;

// Configure a client using Spaces
$client = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'nyc3',
        'endpoint' => 'https://nyc3.digitaloceanspaces.com',
        'credentials' => [
                'key'    => 'MY_SPACES_KEY',
                'secret' => 'MY_SPACES_SECRET,
            ],
]);

$cmd = $client->getCommand('GetObject', [
    'Bucket' => 'examplebucket',
    'Key'    => 'file.ext'
]);

$request = $client->createPresignedRequest($cmd, '+5 minutes');
$presignedUrl = (string) $request->getUri();

echo $presignedUrl;
Have another answer? Share your knowledge.