pv4, err := storage.GenerateSignedPostPolicyV4("my-bucket", "my-object.txt", &storage.PostPolicyV4Options{
GoogleAccessID: "my-access-id",
PrivateKey: []byte("my-private-key"),
// The upload expires in 2hours.
Expires: time.Now().Add(2 * time.Hour),
Fields: &storage.PolicyV4Fields{
StatusCodeOnSuccess: 200,
RedirectToURLOnSuccess: "https://example.org/",
// It MUST only be a text file.
ContentType: "text/plain",
},
// The conditions that the uploaded file will be expected to conform to.
Conditions: []storage.PostPolicyV4Condition{
// Make the file a maximum of 10mB.
storage.ConditionContentLengthRange(0, 10<<20),
},
})
3条答案
按热度按时间yqkkidmi1#
您正在查找
storage.GenerateSignedPostPolicyV4
,它允许您通过PostPolicyV4Options
www.example.com设置策略文档https://cloud.google.com/storage/docs/authentication/signatures#policy-documentzi8p0yeb2#
策略文档允许您更好地控制您在Cloud Storage上上传的对象,通过此功能,您可以控制每次上传必须满足的一系列条件。
由于你需要控制你需要上传的对象的大小,我建议你看一下这里的例子:
正如您所看到的,字段“content-length-range”,0,1000000]不允许文档大于1 Mb。
8gsdolmq3#
你必须使用
GenerateSignedPostPolicyV4
。这将从bucket、object和opts生成一个PostPolicyV4值。该策略允许您添加文件限制作为条件。你可以点击这里找到更多的条件。
之后,您可以通过发送普通HTTP请求,使用生成的post策略上传文件。
示例代码可以在这里找到