Source File
post_policy_v4.go
Belonging Package
cloud.google.com/go/storage
package storage
import (
)
type PostPolicyV4Condition interface {
isEmpty() bool
json.Marshaler
}
type startsWith struct {
key, value string
}
func ( *startsWith) () ([]byte, error) {
return json.Marshal([]string{"starts-with", .key, .value})
}
func ( *startsWith) () bool {
return .value == ""
}
func (, string) PostPolicyV4Condition {
return &startsWith{, }
}
type contentLengthRangeCondition struct {
start, end uint64
}
func ( *contentLengthRangeCondition) () ([]byte, error) {
return json.Marshal([]interface{}{"content-length-range", .start, .end})
}
func ( *contentLengthRangeCondition) () bool {
return .start == 0 && .end == 0
}
type singleValueCondition struct {
name, value string
}
func ( *singleValueCondition) () ([]byte, error) {
return json.Marshal(map[string]string{.name: .value})
}
func ( *singleValueCondition) () bool {
return .value == ""
}
func (, uint64) PostPolicyV4Condition {
return &contentLengthRangeCondition{, }
}
func ( string) PostPolicyV4Condition {
return &singleValueCondition{"success_action_redirect", }
}
func ( int) PostPolicyV4Condition {
:= &singleValueCondition{name: "success_action_status"}
if > 0 {
.value = fmt.Sprintf("%d", )
}
return
}
func (, string, *PostPolicyV4Options) (*PostPolicyV4, error) {
if == "" {
return nil, errors.New("storage: bucket must be non-empty")
}
if == "" {
return nil, errors.New("storage: object must be non-empty")
}
:= utcNow()
if := validatePostPolicyV4Options(, ); != nil {
return nil,
}
var func( []byte) ([]byte, error)
switch {
case .SignBytes != nil:
= .SignBytes
case len(.PrivateKey) != 0:
, := parseKey(.PrivateKey)
if != nil {
return nil,
}
= func( []byte) ([]byte, error) {
return rsa.SignPKCS1v15(rand.Reader, , crypto.SHA256, )
}
default:
return nil, errors.New("storage: exactly one of PrivateKey or SignedBytes must be set")
}
var PolicyV4Fields
if .Fields != nil {
= *.Fields
}
if := validateMetadata(.Metadata); != nil {
return nil,
}
:= make([]PostPolicyV4Condition, len(.Conditions))
copy(, .Conditions)
= append(,
conditionRedirectToURLOnSuccess(.RedirectToURLOnSuccess),
conditionStatusCodeOnSuccess(.StatusCodeOnSuccess),
&singleValueCondition{"acl", .ACL},
&singleValueCondition{"cache-control", .CacheControl},
)
:= .Format(yearMonthDay)
:= map[string]string{
"key": ,
"x-goog-date": .Format(iso8601),
"x-goog-credential": .GoogleAccessID + "/" + + "/auto/storage/goog4_request",
"x-goog-algorithm": "GOOG4-RSA-SHA256",
"success_action_redirect": .RedirectToURLOnSuccess,
"acl": .ACL,
}
for , := range .Metadata {
= append(, &singleValueCondition{, })
[] =
}
= append(,
&singleValueCondition{"bucket", },
&singleValueCondition{"key", },
&singleValueCondition{"x-goog-date", .Format(iso8601)},
&singleValueCondition{
name: "x-goog-credential",
value: .GoogleAccessID + "/" + + "/auto/storage/goog4_request",
},
&singleValueCondition{"x-goog-algorithm", "GOOG4-RSA-SHA256"},
)
:= make([]PostPolicyV4Condition, 0, len(.Conditions))
for , := range {
if == nil || !.isEmpty() {
= append(, )
}
}
, := json.Marshal(map[string]interface{}{
"conditions": ,
"expiration": .Expires.Format(time.RFC3339),
})
if != nil {
return nil, fmt.Errorf("storage: PostPolicyV4 JSON serialization failed: %v", )
}
:= base64.StdEncoding.EncodeToString()
:= sha256.Sum256([]byte())
, := ([:])
if != nil {
return nil,
}
["policy"] =
["x-goog-signature"] = fmt.Sprintf("%x", )
func ( *PostPolicyV4Options, time.Time) error {
if == nil || .GoogleAccessID == "" {
return errors.New("storage: missing required GoogleAccessID")
}
if , := len(.PrivateKey) == 0, .SignBytes == nil; == {
return errors.New("storage: exactly one of PrivateKey or SignedBytes must be set")
}
if .Expires.Before() {
return errors.New("storage: expecting Expires to be in the future")
}
if .Style == nil {
.Style = PathStyle()
}
return nil
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |