Question

Get private file on DigitalOcean Spaces

I am using Digital Ocean Spaces to host some video classes. Files are private because I don’t want to anyone directly access them.

I found this answer with the code I need to stream videos to only authenticated users, but it only works with local files –– so to get a private file from DO Spaces I need to first auth with their SDK, but I can’t do that.

They use the same parameters of Amazon S3, but I couldn’t create a connection since Amazon SDK doesn’t a allow changing the host (in this case, should be nyc3.digitaloceanspaces.com).

<?php

define('AWS_KEY', ' _DigitalOcean Spaces Key_ ');
define('AWS_SECRET_KEY', ' _DigitalOcean Spaces SecretKey_ ');

define('AWS_CANONICAL_ID', ' __?__ ');
define('AWS_CANONICAL_NAME', ' __?__ ');

$HOST = 'https://nyc3.digitaloceanspaces.com';

require 'aws/aws-autoloader.php';

$Connection = new AmazonS3(array(
    'key' => AWS_KEY,
    'secret' => AWS_SECRET_KEY,
    'canonical_id' => AWS_CANONICAL_ID,
    'canonical_name' => AWS_CANONICAL_NAME,
));
$Connection->set_hostname($HOST);
$Connection->allow_hostname_override(false);

$Connection->enable_path_style();

$file = "videos/" . $_GET['video'] . ".mp4";

$Connection->get_object('myBucket', $file);

$fakefilename = "./show-video/" . $_GET['video'] . ".mp4";

//          +++++++++++++  Streaming support for Apple iPhone +++++++++++
$fp = @fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size;           // Content length
$start = 0;               // Start byte
$end = $size - 1;       // End byte
header('Content-Type: video/mp4');

//      +++++++++++ Detect if user OS is iPad, iPhone, iPod ++++++++++++++++++++
$iPod = stripos($_SERVER['HTTP_USER_AGENT'], "iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'], "iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'], "iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'], "Android");
$webOS = stripos($_SERVER['HTTP_USER_AGENT'], "webOS");
//      +++++++++++ End of Detect if user OS is iPad, iPhone, iPod +++++++++++++
if (!($iPad OR $iPhone OR $iPod)) {
    header('Content-Disposition: attachment; filename="' . basename($fakefilename) . '"');
}

header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {

    $c_start = $start;
    $c_end = $end;

    list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
    if (strpos($range, ',') !== false) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    if ($range == '-') {
        $c_start = $size - substr($range, 1);
    } else {
        $range = explode('-', $range);
        $c_start = $range[0];
        $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
    }
    $c_end = ($c_end > $end) ? $end : $c_end;
    if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    $start = $c_start;
    $end = $c_end;
    $length = $end - $start + 1;
    fseek($fp, $start);
    header('HTTP/1.1 206 Partial Content');
}
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: " . $length);
$buffer = 1024 * 8;
while (!feof($fp) && ($p = ftell($fp)) <= $end) {
    if ($p + $buffer > $end) {
        $buffer = $end - $p + 1;
    }
    set_time_limit(0);
    echo fread($fp, $buffer);
    flush();
}

fclose($fp);

exit();
//          +++++++ End of Streaming support for Apple iPhone +++++++++++

?>

Can anyone help me? I am totally lost right now.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hey there

So I took a look at the S3 client we are using in our application, turns out if you dive deeper into the source code you can specific an endpoint, so sure: you can keep using your sdk.

Only difference however is your not using the same version as us, we’re using

"aws/aws-sdk-php": "3.36.25"

So, eventually once we migrate to Spaces, our construct will look like this

new Aws\S3\S3Client(
            [
                'region'      => $region,
                'version'     => $version,
                'credentials' => $credentials,
                'endpoint' => 'nyc3.digitaloceanspaces.com'
            ]
        );

We trust this will work because the underlying AWS S3 construct contains the following

$this->handlerList = new HandlerList();
        $resolver = new ClientResolver(static::getArguments());
        $config = $resolver->resolve($args, $this->handlerList);
        $this->api = $config['api'];
        $this->signatureProvider = $config['signature_provider'];
        $this->endpoint = new Uri($config['endpoint']);
        $this->credentialProvider = $config['credentials'];
        $this->region = isset($config['region']) ? $config['region'] : null;
        $this->config = $config['config'];
        $this->defaultRequestOptions = $config['http'];
        $this->addSignatureMiddleware();
        $this->addInvocationId();

Let me know if this helps.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.