如何在Golang中编码一个POST策略以实现基于浏览器的上传到Amazon S3?

huangapple go评论311阅读模式
英文:

How to encode a POST Policy in golang - browser based uploads to amazon S3?

问题

我正在尝试将图像文件上传到Amazon S3。设置如下:

Web服务器:golang
前端:用于测试的简单HTML表单

参考此文档:http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

我参考了上述文档中提供的示例,并尝试了以下代码:

package main

import "fmt"
import "encoding/base64"

func main() {
    bytePolicy := []byte(`{ 
        "expiration": "2013-08-06T12:00:00.000Z",
        "conditions": [
            {"bucket": "examplebucket"},
            ["starts-with", "$key", "user/user1/"],
            {"acl": "public-read"},
            {"success_action_redirect": "http://acl6.s3.amazonaws.com/successful_upload.html"},
            ["starts-with", "$Content-Type", "image/"],
            {"x-amz-meta-uuid": "14365123651274"},
            ["starts-with", "$x-amz-meta-tag", ""],
            {"x-amz-credential":"AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request"},
            {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
            {"x-amz-date": "20130806T000000Z"}
        ]
    }`)
    fmt.Println(base64.StdEncoding.EncodeToString(bytePolicy))
}

我得到的结果是:

eyAKICAgICAgICAgICAgICAgICAgICAgICAgImV4cGlyYXRpb24iOiAiMjAxMy0wOC0wNlQxMjowMDowMC4wMDBaIiwKICAgICAgICAgICAgICAgICAgICAgICAgImNvbmRpdGlvbnMiOiBbCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsiYnVja2V0IjogImV4YW1wbGVidWNrZXQifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci91c2VyMS8iXSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyJ4LWFtei1tZXRhLXV1aWQiOiAiMTQzNjUxMjM2NTEyNzQifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXSwKCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyJ4LWFtei1jcmVkZW50aWFsIjoiQUtJQUlPU0ZPRE5ON0VYQU1QTEUvMjAxMzA4MDYvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsieC1hbXotZGF0ZSI6ICIyMDEzMDgwNlQwMDAwMDBaIiB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgXQogICAgICAgICAgICAgICAgICAgIH0=

这是Amazon教程链接中的Base64编码策略:

eyAiZXhwaXJhdGlvbiI6ICIyMDEzLTA4LTA3VDEyOjAwOjAwLjAwMFoiLA0KICAiY29uZGl0aW9ucyI6IFsNCiAgICB7ImJ1Y2tldCI6ICJleGFtcGxlYnVja2V0In0sDQogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgInVzZXIvdXNlcjEvIl0sDQogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwNCiAgICB7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly9leGFtcGxlYnVja2V0LnMzLmFtYXpvbmF3cy5jb20vc3VjY2Vzc2Z1bF91cGxvYWQuaHRtbCJ9LA0KICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwNCiAgICB7IngtYW16LW1ldGEtdXVpZCI6ICIxNDM2NTEyMzY1MTI3NCJ9LA0KICAgIFsic3RhcnRzLXdpdGgiLCAiJHgtYW16LW1ldGEtdGFnIiwgIiJdLA0KDQogICAgeyJ4LWFtei1jcmVkZW50aWFsIjogIkFLSUFJT1NGT0ROTjdFWEFNUExFLzIwMTMwODA2L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QifSwNCiAgICB7IngtYW16LWFsZ29yaXRobSI6ICJBV1M0LUhNQUMtU0hBMjU2In0sDQogICAgeyJ4LWFtei1kYXRlIjogIjIwMTMwODA2VDAwMDAwMFoiIH0NCiAgXQ0KfQ==

为什么我的Base64编码策略与Amazon的不匹配?

英文:

I am trying to upload a image file to amazon s3. The setup is as follows:

Web server: golang<br/>Front-end: simple html form for testing

In reference with this document : http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

I referred to the example provided in the above document and tried this:<br/> http://play.golang.org/p/3zn5fSDasK

  package main

  import &quot;fmt&quot;
  import &quot;encoding/base64&quot;

  func main() {
         bytePolicy := []byte(`{ 
                    &quot;expiration&quot;: &quot;2013-08-06T12:00:00.000Z&quot;,
                    &quot;conditions&quot;: [
                             {&quot;bucket&quot;: &quot;examplebucket&quot;},
                             [&quot;starts-with&quot;, &quot;$key&quot;, &quot;user/user1/&quot;],
                             {&quot;acl&quot;: &quot;public-read&quot;},
                             {&quot;success_action_redirect&quot;: &quot;http://acl6.s3.amazonaws.com/successful_upload.html&quot;},
                             [&quot;starts-with&quot;, &quot;$Content-Type&quot;, &quot;image/&quot;],
                             {&quot;x-amz-meta-uuid&quot;: &quot;14365123651274&quot;},
                             [&quot;starts-with&quot;, &quot;$x-amz-meta-tag&quot;, &quot;&quot;],
                             {&quot;x-amz-credential&quot;:&quot;AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request&quot;},
                             {&quot;x-amz-algorithm&quot;: &quot;AWS4-HMAC-SHA256&quot;},
                             {&quot;x-amz-date&quot;: &quot;20130806T000000Z&quot; }
                      ]
                  }`)
        fmt.Println(base64.StdEncoding.EncodeToString(bytePolicy))
 }

The result I got with this<br/>

   eyAKICAgICAgICAgICAgICAgICAgICAgICAgImV4cGlyYXRpb24iOiAiMjAxMy0wOC0wNlQxMjowMDowMC4wMDBaIiwKICAgICAgICAgICAgICAgICAgICAgICAgImNvbmRpdGlvbnMiOiBbCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsiYnVja2V0IjogImV4YW1wbGVidWNrZXQifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci91c2VyMS8iXSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyJzdWNjZXNzX2FjdGlvbl9yZWRpcmVjdCI6ICJodHRwOi8vYWNsNi5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyJ4LWFtei1tZXRhLXV1aWQiOiAiMTQzNjUxMjM2NTEyNzQifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXSwKCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgeyJ4LWFtei1jcmVkZW50aWFsIjoiQUtJQUlPU0ZPRE5ON0VYQU1QTEUvMjAxMzA4MDYvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsieC1hbXotZGF0ZSI6ICIyMDEzMDgwNlQwMDAwMDBaIiB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgXQogICAgICAgICAgICAgICAgICAgIH0=

The base 64 encoded policy which is on link of amazon tutorial:<br/>

   eyAiZXhwaXJhdGlvbiI6ICIyMDEzLTA4LTA3VDEyOjAwOjAwLjAwMFoiLA0KICAiY29uZGl0aW9ucyI6IFsNCiAgICB7ImJ1Y2tldCI6ICJleGFtcGxlYnVja2V0In0sDQogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgInVzZXIvdXNlcjEvIl0sDQogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwNCiAgICB7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly9leGFtcGxlYnVja2V0LnMzLmFtYXpvbmF3cy5jb20vc3VjY2Vzc2Z1bF91cGxvYWQuaHRtbCJ9LA0KICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwNCiAgICB7IngtYW16LW1ldGEtdXVpZCI6ICIxNDM2NTEyMzY1MTI3NCJ9LA0KICAgIFsic3RhcnRzLXdpdGgiLCAiJHgtYW16LW1ldGEtdGFnIiwgIiJdLA0KDQogICAgeyJ4LWFtei1jcmVkZW50aWFsIjogIkFLSUFJT1NGT0ROTjdFWEFNUExFLzIwMTMwODA2L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QifSwNCiAgICB7IngtYW16LWFsZ29yaXRobSI6ICJBV1M0LUhNQUMtU0hBMjU2In0sDQogICAgeyJ4LWFtei1kYXRlIjogIjIwMTMwODA2VDAwMDAwMFoiIH0NCiAgXQ0KfQ==

Why my base64 encoded policy doesn't match with that of Amazon's?

答案1

得分: 3

只因为产生这些base64字符串的两个JSON源文本具有不同的缩进和不同的内容(它们不是逐字符相等的)。

解码这两个base64字符串,你会看到它们之间的差异。你可以使用一个程序(不一定是Go语言),或者简单地使用一个在线服务(比如这个)来完成解码。

你不必担心缩进,因为在JSON中它并不重要。但是当你使用Base64编码一个文本时,它会编码源文本中的所有字符,包括用于缩进的空格和制表符,所以不同的缩进会导致不同的Base64编码形式。

但是比较这两个解码后的JSON,还有其他的差异:

第一个:

"expiration": "2013-08-06T12:00:00.000Z"
"success_action_redirect": "http://acl6.s3.amazonaws.com/successful_upload.html"

第二个:

"expiration": "2013-08-07T12:00:00.000Z"
"success_action_redirect": "http://examplebucket.s3.amazonaws.com/successful_upload.html"

完整的解码后的JSON文本:

第一个:

{ 
    "expiration": "2013-08-06T12:00:00.000Z",
    "conditions": [
        {"bucket": "examplebucket"},
        ["starts-with", "$key", "user/user1/"],
        {"acl": "public-read"},
        {"success_action_redirect": "http://acl6.s3.amazonaws.com/successful_upload.html"},
        ["starts-with", "$Content-Type", "image/"],
        {"x-amz-meta-uuid": "14365123651274"},
        ["starts-with", "$x-amz-meta-tag", ""],
        {"x-amz-credential":"AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request"},
        {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
        {"x-amz-date": "20130806T000000Z"}
    ]
}

第二个:

{"expiration": "2013-08-07T12:00:00.000Z",
"conditions": [
    {"bucket": "examplebucket"},
    ["starts-with", "$key", "user/user1/"],
    {"acl": "public-read"},
    {"success_action_redirect": "http://examplebucket.s3.amazonaws.com/successful_upload.html"},
    ["starts-with", "$Content-Type", "image/"],
    {"x-amz-meta-uuid": "14365123651274"},
    ["starts-with", "$x-amz-meta-tag", ""],
    {"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "20130806T000000Z"}
]

}

英文:

Simply because the 2 JSON source texts which produce those base64 strings have different indentation and different content as seen below (they are not equal char-by-char).

Decode both of the base64 strings, and you will see the differences. You can do that either with a program (not necessarily Go), or simply use an online service like this one.

You shouldn't worry about indentation, it doesn't matter in JSON. But when you encode a text using Base64, that encodes all characters of the source including spaces and tabs used to indent, so different indentation will result in different Base64 encoded forms.

But comparing the 2 decoded JSON, there are also other differences:

First one:

&quot;expiration&quot;: &quot;2013-08-06T12:00:00.000Z&quot;
&quot;success_action_redirect&quot;: &quot;http://acl6.s3.amazonaws.com/successful_upload.html&quot;

2nd one:

&quot;expiration&quot;: &quot;2013-08-07T12:00:00.000Z&quot;
&quot;success_action_redirect&quot;: &quot;http://examplebucket.s3.amazonaws.com/successful_upload.html&quot;

The complete decoded JSON texts:

The first one:

{ 
                        &quot;expiration&quot;: &quot;2013-08-06T12:00:00.000Z&quot;,
                        &quot;conditions&quot;: [
                                 {&quot;bucket&quot;: &quot;examplebucket&quot;},
                                [&quot;starts-with&quot;, &quot;$key&quot;, &quot;user/user1/&quot;],
                                 {&quot;acl&quot;: &quot;public-read&quot;},
                                 {&quot;success_action_redirect&quot;: &quot;http://acl6.s3.amazonaws.com/successful_upload.html&quot;},
                                [&quot;starts-with&quot;, &quot;$Content-Type&quot;, &quot;image/&quot;],
                                {&quot;x-amz-meta-uuid&quot;: &quot;14365123651274&quot;},
                                [&quot;starts-with&quot;, &quot;$x-amz-meta-tag&quot;, &quot;&quot;],

                                {&quot;x-amz-credential&quot;:&quot;AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request&quot;},
                                {&quot;x-amz-algorithm&quot;: &quot;AWS4-HMAC-SHA256&quot;},
                               {&quot;x-amz-date&quot;: &quot;20130806T000000Z&quot; }
                          ]
                    }

And the 2nd one:

{ &quot;expiration&quot;: &quot;2013-08-07T12:00:00.000Z&quot;,
  &quot;conditions&quot;: [
    {&quot;bucket&quot;: &quot;examplebucket&quot;},
    [&quot;starts-with&quot;, &quot;$key&quot;, &quot;user/user1/&quot;],
    {&quot;acl&quot;: &quot;public-read&quot;},
    {&quot;success_action_redirect&quot;: &quot;http://examplebucket.s3.amazonaws.com/successful_upload.html&quot;},
    [&quot;starts-with&quot;, &quot;$Content-Type&quot;, &quot;image/&quot;],
    {&quot;x-amz-meta-uuid&quot;: &quot;14365123651274&quot;},
    [&quot;starts-with&quot;, &quot;$x-amz-meta-tag&quot;, &quot;&quot;],

    {&quot;x-amz-credential&quot;: &quot;AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request&quot;},
    {&quot;x-amz-algorithm&quot;: &quot;AWS4-HMAC-SHA256&quot;},
    {&quot;x-amz-date&quot;: &quot;20130806T000000Z&quot; }
  ]
}

huangapple
  • 本文由 发表于 2015年10月15日 17:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/33144375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定