Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 get images via Proxy - PHP

I have a bucket at S3 where i have uploaded the images. Now i am fetching the images using AWS - SDK. Now i want to bypass the image via Proxy

$client = new Aws\S3\S3Client([
            'version'     => 'latest',
            'region'      => 'us-east-1',
            'debug'       => TRUE, // enable debug info
            'stats'       => TRUE, // enable stats
            '@http'  => [
            'proxy' => 'http://192.168.16.1:10'
            ],
            'credentials' => [
                'key'    => base64_decode(KEY),
                'secret' => base64_decode(SECRET)
            ]
        ]);

Here is my bucket settings and when i did wireshark it still showing AWS ip address in request.

Can anybody tell me how to bypass S3 images with proxy.

like image 461
Hitu Bansal Avatar asked Jan 03 '18 03:01

Hitu Bansal


People also ask

How do I get pictures from my S3 bucket to my browser?

Sign in to Amazon Web Services and go to your S3 Management Console. 2. Click on the name of the S3 bucket from the list. If it's still in its default access state, it should say “Buckets and objects not public” next to it.

How do I download an image file to Amazon S3 through API gateway?

Upload an image file to S3 by invoking your APIAppend the bucket name and file name of the object to your API's invoke URL. Then, make a PUT HTTP request using a client of your choice. For example, the Postman application. For more information, see Invoking a REST API in Amazon API Gateway.

Can you access S3 objects from the Internet?

If you don't want to use an S3 Endpoint to access an S3 bucket, you can access it using the internet gateway. By default, S3 access goes through the S3 endpoint of your connected Amazon VPC. You must enable access to S3 over the internet before you can use it.

Can we host PHP website on S3 bucket?

Using an Amazon S3 bucket as a static web host with AWS SDK for PHP Version 3. You can host a static website on Amazon S3. To learn more, see Hosting a Static Website on Amazon S3.


1 Answers

If I understand you correctly you want to set up a reserve proxy for your images E.G your system downloads the images send to the browser so it can render them, and you don't want users to know your path / you don't want public read access on your bucket.

If that is the case you can use the following code to download the file via

$result = $client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $keyname
));

header("Content-Type: {$result['ContentType']}");
echo $result['Body'];

this could then be set up on a specific URL with the key as a parameter or if your bucket is correctly secured you could just use the key name via GET E.G URL image.php?key=some/key/on/aws.jpg and use $keyname = $_GET['key'] inside your file.

If you're using a MySQL table to use a lookup it would be $id = $_GET['id']; and create a function that protects against SQL Injection and returns the key column then use that for your $keyname an example table would be $keyname and that can be set by a mapping database table E.G

CREATE TABLE `proxy_map`(
   `id` INT(11) NOT NULL PRIMARY KEY,
   `key` TEXT NOT NULL
)

If you want to limit it so only this specific site can use it you can use a referrer check

$url = parse_url($_SERVER['HTTP_REFERER'] , PHP_URL_HOST);
if($url !== $_SERVER[HTTP_HOST]){ // assuming that the images are only loaded on the same site as this php script
    http_response_code(404);
    echo "<h1>File Not Found</h1><p>Sorry the file you were looking for could not be found</p>";
}

If you wish to allow the images from a set of sites E.G you have a subdomain setup you can use.

$url = parse_url($_SERVER['HTTP_REFERER'] , PHP_URL_HOST);
$allowedDomains = array(
    $_SERVER[HTTP_HOST],
    "www.example.com"
);
if(!in_array($url, $allowdDomains))
like image 53
Barkermn01 Avatar answered Oct 02 '22 23:10

Barkermn01