Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 not caching images

Tags:

php

amazon-s3

I'm using the Amazon S3 PHP Class to upload images, but the cache headers aren't being set. Here's the call I'm using.

$s3->putObjectFile(
    $image_location,
    "bucketname",
    $image_file_name,
    S3::ACL_PUBLIC_READ,
    array(
        "Cache-Control" => "max-age=315360000",
        "Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
    )
);

The header response I'm getting for the uploaded image is:

Date: Tue, 04 Oct 2011 04:21:09 GMT
x-amz-request-id: B6BAAAAD9B460160
Content-Length: 34319
x-amz-id-2: Oxxx1hIG2nNKfff3vgH/xx/dffF59O/7a1UWrKrgZlju2g/8WvTcBpccYToULbm
Last-Modified: Tue, 04 Oct 2011 04:19:20 GMT
Server: AmazonS3
ETag: "4846afffbc1a7284fff4a590d5acd6cd"
Content-Type: image/jpeg
Accept-Ranges: bytes
like image 524
Lamoni Avatar asked Feb 22 '26 04:02

Lamoni


1 Answers

I am not familiar with the Amazon S3 PHP Class but a quick look at the documentation reveals that the putObjectFile method is depreciated and you should use putObject instead.

<?php

    // PUT with custom headers:
    $put = S3::putObject(
        S3::inputFile($file),
        $bucket,
        $uri,
        S3::ACL_PUBLIC_READ,
        array(),
        array( // Custom $requestHeaders
            "Cache-Control" => "max-age=315360000",
            "Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
        )
    );
    var_dump($put);

?>

But why not consider using the official Amazon SDk for PHP?

You would use create_object to upload a file. The official docs have some good examples:

// Instantiate the class
$s3 = new AmazonS3();

$response = $s3->create_object('my-bucket', 'üpløåd/î\'vé nøw béén üpløådéd.txt', array(
    'fileUpload' => 'upload_me.txt',
    'acl' => AmazonS3::ACL_PUBLIC,
    'contentType' => 'text/plain',
    'storage' => AmazonS3::STORAGE_REDUCED,
    'headers' => array( // raw headers
        'Cache-Control' => 'max-age',
        'Content-Encoding' => 'gzip',
        'Content-Language' => 'en-US',
        'Expires' => 'Thu, 01 Dec 1994 16:00:00 GMT',
    ),
    'meta' => array(
        'word' => 'to your mother', // x-amz-meta-word
        'ice-ice-baby' => 'too cold, too cold' // x-amz-meta-ice-ice-baby
    ),
));

// Success?
var_dump($response->isOK());
like image 188
Geoff Appleford Avatar answered Feb 23 '26 16:02

Geoff Appleford