<?php
include("./lib/config.php");
require 'vendor/autoload.php';

$s3Client = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'endpoint' => $S3Server,
        'use_path_style_endpoint' => true,
        'credentials' => [
                'key'    => $S3AccessKey,
                'secret' => $S3SecretKey,
            ],
]);

// Get a command object from the client
$command = $s3Client->getCommand('GetObject', [
    'Bucket' => $S3BucketName,
    'Key'    => $_GET["filename"]
]);

// Create a pre-signed URL for a request with duration of 10 miniutes
$presignedRequest = $s3Client->createPresignedRequest($command, '60 minutes');

// Get the actual presigned-url
$downloadURL =  (string)  $presignedRequest->getUri();

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $downloadURL);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);

$content = curl_exec ($curl);
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
header('Content-Type: ' . $contentType);
echo($content);