0

我正在创建以下表单,以便将 POST 对象与 Google Storage 一起使用,以允许用户将文档上传到我的存储桶。提交表单时出现以下错误。我确定我创建的策略是错误的,但我不确定如何正确执行,我尝试了几种方法。Google 的文档在此处解释了该政策。谢谢您的帮助!

    <Error> 

<Code>InvalidPolicyDocument</Code> − <Message> The content of the form does not meet the conditions specified in the policy document. </Message> 
− <Details> Invalid value for conditions: {"acl":"private","failure_action_redirect":"http:\/\/www.example.com\/failure_instructions.html"} </Details> 
</Error>


class Policy {
    public $expiration = "2010-12-31T11:11:11Z";
    public $conditions = array("acl" => "private",
                        "failure_action_redirect" => "http://www.example.com/failure_instructions.html");
}
$policy = new Policy();
$policy = json_encode($policy);

$policy_utf = utf8_encode($policy);
$policy_base64 = base64_encode($policy_utf);
$policy_sig = base64_encode(hash_hmac('sha1', $policy_base64, $secret, TRUE));
?>

<form action="http://<example_bucket>.commondatastorage.googleapis.com" method="post" enctype="multipart/form-data">
            <input type="hidden" name="key" value="test_documenttttt">
            <input type="hidden" name="GoogleAccessId" value="<?php echo $key; ?>">
            <input type="hidden" name="policy" value="<?php echo $policy_base64; ?>">
            <input type="hidden" name="signature" value="<?php echo $policy_sig; ?>">
            <input type="file" name="file">
            <input type="submit" value="Upload!">
</form>
4

1 回答 1

1

解决了!

它就像教程一样简单——我研究得太多了。只需$policy = <the code>对其进行设置和 UTF-8 编码。我的问题是我会设置 $policy = <the example code>,但我会 json_encode() 那个。它已经JSON编码,所以 JSON 编码搞砸了。读得太多了是我的错!

$policy = '{ "expiration": "2010-12-31T11:11:11Z",'.
                        '"conditions": ['.
                        '["starts-with", "$key", "" ],'.
                '{"acl": "private" }'.
                        ']'.
                        '}'; 
于 2010-12-29T17:10:19.183 回答