Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new method SetUserMetadata to the PostPolicy #839

Merged
merged 3 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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