Skip to content

Commit

Permalink
Added a new method SetUserMetadata to the PostPolicy (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
hnb2 authored and nitisht committed Oct 17, 2017
1 parent 944f238 commit fe263bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,9 @@ policy.SetContentType("image/png")
// Only allow content size in range 1KB to 1MB.
policy.SetContentLengthRange(1024, 1024*1024)

// Add a user metadata using the key "custom" and value "user"
policy.SetUserMetadata("custom", "user")

// Get the POST form key/value object:

url, formData, err := minioClient.PresignedPostPolicy(policy)
Expand Down
6 changes: 6 additions & 0 deletions functional_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2053,6 +2053,8 @@ func testPresignedPostPolicy() {
defer reader.Close()

objectName := randString(60, rand.NewSource(time.Now().UnixNano()), "")
metadataKey := randString(60, rand.NewSource(time.Now().UnixNano()), "")
metadataValue := randString(60, rand.NewSource(time.Now().UnixNano()), "")

buf, err := ioutil.ReadAll(reader)
if err != nil {
Expand Down Expand Up @@ -2089,12 +2091,16 @@ func testPresignedPostPolicy() {
if err := policy.SetContentLengthRange(1024*1024, 1024); err == nil {
failureLog(function, args, startTime, "", "SetContentLengthRange did not fail for invalid conditions", err).Fatal()
}
if err := policy.SetUserMetadata("", ""); err == nil {
failureLog(function, args, startTime, "", "SetUserMetadata did not fail for invalid conditions", err).Fatal()
}

policy.SetBucket(bucketName)
policy.SetKey(objectName)
policy.SetExpires(time.Now().UTC().AddDate(0, 0, 10)) // expires in 10 days
policy.SetContentType("binary/octet-stream")
policy.SetContentLengthRange(10, 1024*1024)
policy.SetUserMetadata(metadataKey, metadataValue)
args["policy"] = policy.String()

presignedPostPolicyURL, formData, err := c.PresignedPostPolicy(policy)
Expand Down
22 changes: 22 additions & 0 deletions post-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ func (p *PostPolicy) SetSuccessStatusAction(status string) error {
return nil
}

// SetUserMetadata - Set user metadata as a key/value couple.
// Can be retrieved through a HEAD request or an event.
func (p *PostPolicy) SetUserMetadata(key string, value string) error {
if strings.TrimSpace(key) == "" || key == "" {
return ErrInvalidArgument("Key is empty")
}
if strings.TrimSpace(value) == "" || value == "" {
return ErrInvalidArgument("Value is empty")
}
headerName := fmt.Sprintf("x-amz-meta-%s", key)
policyCond := policyCondition{
matchType: "eq",
condition: fmt.Sprintf("$%s", headerName),
value: value,
}
if err := p.addNewPolicy(policyCond); err != nil {
return err
}
p.formData[headerName] = value
return nil
}

// addNewPolicy - internal helper to validate adding new policies.
func (p *PostPolicy) addNewPolicy(policyCond policyCondition) error {
if policyCond.matchType == "" || policyCond.condition == "" || policyCond.value == "" {
Expand Down

0 comments on commit fe263bf

Please sign in to comment.