【问题标题】:can't access the Amazon s3 data by query string authentication无法通过查询字符串身份验证访问 Amazon s3 数据
【发布时间】:2011-03-01 02:35:51
【问题描述】:


我下面的代码用于访问 amazon s3 以获取 .zip 文件。 我的请求网址是这样的: http://xxx.s3.amazonaws.com/Pack1A.zip?AWSAccessKeyId=AKIAIEMQY4BEQUOCUP7Q&Expires=1298945115&Signature=sxoXZ4y7osXjn IycQynGbE9%2Bb5E%3D

代码片段:

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 禁止的错误。谁能告诉我为什么?

【问题讨论】:

    标签: c++ amazon-web-services amazon-s3 authorization


    【解决方案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
    ?>
    

    【讨论】:

      【解决方案2】:

      确保所有 ACL 设置正确。

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

      还有一个 AWS PHP SDK。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-14
        • 1970-01-01
        • 2014-02-17
        • 2018-06-16
        • 1970-01-01
        • 2017-08-31
        • 1970-01-01
        • 2010-11-21
        相关资源
        最近更新 更多