存储桶上的S3列出对象请求以获取其他密钥(> 1000)

时间:2011-03-21 20:51:34

标签: php amazon-s3

默认情况下,S3 List Objects(获取存储桶)请求会返回1000个键。根据S3文档,如果我想列出更多对象,那么我必须将标记查询字符串与我的请求一起传递,并将标记集的值设置为上一个对象列表中的最后一个键。

由于某种原因,这对我不起作用......我似乎错过了一些明显的东西,我正在试图找出它。

这就是REST请求的样子:

GET ?marker=LAST-KEY HTTP/1.1
Host: quotes.s3.amazonaws.com
Date: Wed, 01 Mar  2009 12:00:00 GMT
Authorization: AWS ACCESS_KEY_ID:SIGNATURE

我的PHP代码看起来像这样:

...
// this is where I'm getting the value of the last key
$marker=$arrObjects["Contents"][999]["Key"];
...
//this is where I'm signing my request
$canonicalizedResources = "/".$bucketName."/".(($marker=="")?"":"?marker=".rawurlencode($marker));
// $contentMD5 and $contentType are both empty strings ""
$stringToSign = utf8_encode("GET"."\n".$contentMD5."\n".$contentType."\n".$timestamp."\n".$canonicalizedAmzHeaders.$canonicalizedResources);
$signature = base64_encode(hash_hmac("sha1",$stringToSign,$customerSecretKey,true));
...

如果我在一个数量小于1000的存储桶中列出对象,则此代码可以正常工作。但每当我将标记传递给请求时,我都会收到“签名不匹配”错误。

任何人都知道我哪里出错了?

1 个答案:

答案 0 :(得分:-1)

要跟进@ Skyler的评论,使用SDK,您的代码可能如下所示(减去授权位):

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

$response = $s3->list_objects('my-bucket');

AWS docs中的更多内容。