2011-03-01 57 views
0


我的代码如下用于访问亚马逊s3以获取.zip文件。 和我的请求的URL是这样的: http://xxx.s3.amazonaws.com/Pack1A.zip?AWSAccessKeyId=AKIAIEMQY4BEQUOCUP7Q&Expires=1298945115&Signature=sxoXZ4y7osXjn IycQynGbE9%2Bb5E%3D无法通过查询字符串身份验证访问Amazon s3数据

代码段:

time_t rawtime; 
time(&rawtime); 

gHttpDownloader->SetRequestHeader("Authorization","AWS AKIAIEMQY4BEQUOCUP7Q:aN6bjwDkeZXIHDrqk3MHlj4shl0%3D"); 
gHttpDownloader->SetRequestHeader("Authorization", buf); 
gHttpDownloader->SetRequestHeader("Cache-Control", "max-age=0"); 
gHttpDownloader->SetRequestHeader("Accept","application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*;q=0.5"); 
gHttpDownloader->SetRequestHeader("Accept-Encoding", "gzip,deflate,sdch"); 
gHttpDownloader->SetRequestHeader("Accept-Language", "en-GB"); 
gHttpDownloader->SetRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3"); 
gHttpDownloader->Post(gURL, NULL,0,GotHeaders, NULL); 

............ //GotHeaders is a callback function that reads input from the response. 

它总是打印的403禁止错误。有人能告诉我为什么吗?

回答

0

确保所有ACL均已正确设置。

示例URL使用Query String Authentication,但代码示例显示正在设置Authorization。你只需要设置一个或另一个。请参阅docs

还有一个AWS PHP SDK

+0

感谢。尽管我使用c plus plus。 – GGLouis 2011-04-21 02:59:18

1

我遇到了类似的问题,即从Web浏览器请求预先签名的URL是成功的,但直接从PHP导致失败。在做了一些挖掘之后,我了解到要模拟Web浏览器,您需要传递一个空值为Content-Type的标头。

AWS SDK for PHP提供了documentation下面的示例:

<?php 
$s3 = new AmazonS3(); 

// Generate the URL 
$url = $s3->get_object_url('my-bucket', 'folder/file.txt', '5 minutes'); 

// Try to fetch the URL so we can get the status code 
$http = new CFRequest($url); 
$http->add_header('Content-Type', ''); # Simulate a web browser 
$http->send_request(true); 

echo $http->get_response_code(); 
#=> 200 
?> 
相关问题