php - 检查文件是否存在于 Amazon s3 签名 URL 上

标签 php curl amazon-web-services amazon-s3

我为 Amazon s3 创建了一个签名的 $URL,它可以在浏览器中完美打开。

http://testbucket.com.s3.amazonaws.com/100-game-play-intro-1.m4v?AWSAccessKeyId=AKIAJUAjhkhkjhMO73BF5Q&Expires=1378465934&Signature=ttmsAUDgJjCXepwEXvl8JdFu%2F60%3D

**此示例中更改了存储桶名称和访问 key

然而,我正在尝试使用下面的函数来检查(使用 curl)文件是否存在。它使 CURL 连接失败。如果我将上面的 $URL 替换为 s3 之外的图像的 url,则此代码可以完美运行。

我知道该文件存在于 amazon 中,但无法弄清楚为什么如果使用上述签名的 url,此代码会失败

有什么想法吗?

谢谢

这是我的代码。

function remoteFileExists($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    //don't fetch the actual file, only get header to check if file exists
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, true);

    $result = curl_exec($ch);
    curl_close($ch);



    if ($result !== false) {

        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  

        if ($statusCode == 200) {
            $ret = true;   
        } else {
            $ret = false;
        }

    } else {
        $ret='connection failed';
    }

    return $ret;

}

最佳答案

使用 CURLOPT_NOBODY 时, libcurl 发送 HTTP HEAD 请求,而不是 GET 请求。

...the string to be signed is formed by appending the REST verb, content-md5 value, content-type value, expires parameter value, canonicalized x-amz headers (see recipe below), and the resource; all separated by newlines.

http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html

“REST 动词”——例如,GETHEAD——必须在您生成的签名和发出的请求之间保持一致,因此签名对 GET 有效将对 HEAD 无效,反之亦然。

您需要签署 HEAD 请求而不是 GET 请求才能以这种方式验证文件。

关于php - 检查文件是否存在于 Amazon s3 签名 URL 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18635216/

相关文章:

bash - 如何使用 curl 通过 bash 脚本将表情符号发送到 Telegram 机器人?

RCurl:在 Rgui 中显示进度表

python - pycurl 失败但 curl(来自 bash)在 ubuntu 中工作

javascript - 在 angular2 中使用 http 访问 Amazon s3

php - 为什么我的输入法不起作用?

javascript - 如何通过许多重复出现的图像来加快页面加载时间?

amazon-web-services - 如何在部署时使用 Elastic Beanstalk 指定敏感的环境变量

javascript - Alexa Smarthome-无需 AWS 的技能

php - 循环中的查询是否可能给出不同的结果?

php - 将 'utf-8' 参数添加到 htmlspecialchars() 的出现 - 它可以破坏任何东西吗?