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" => 开发者_如何学Cgmdate("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
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());
Cache-Control
or Expires
headers are to be sent from server to client to instruct client on caching of the data. In your case, you have client sending those headers to server which has no meaning. I believe that you intention is to send headers to S3 and then you expect them to be provided by S3 when another client asks for the file. This is not supported, I believe.
However, S3 provides ETAg
and Last-Modified
headers that should be sufficient for most practical purposes as there is hardly any reason for your client to redownload the file unless it's updated in S3 (in this case ETag
and Last-Modified
will change).
S3::putObjectFile
does not accept Request Headers. The argument you are looking at is Meta Headers which is not quite the same thing.
S3::putObjectFile
is just a wrapper around S3::putObject
anyway so the following will work just fine
$s3->putObject(
S3::inputFile($image_location),
"bucketname",
$image_file_name,
S3::ACL_PUBLIC_READ,
array(), // meta headers
array( // request headers
"Cache-Control" => "max-age=315360000",
)
);
精彩评论