Add ObjectTagging Support (#8754)
This PR adds support for AWS S3 ObjectTagging API as explained here https://docs.aws.amazon.com/AmazonS3/latest/dev/object-tagging.htmlmaster
parent
dd93eee1e3
commit
61c17c8933
@ -0,0 +1,44 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"fmt" |
||||
) |
||||
|
||||
// Error is the generic type for any error happening during tag
|
||||
// parsing.
|
||||
type Error struct { |
||||
err error |
||||
} |
||||
|
||||
// Errorf - formats according to a format specifier and returns
|
||||
// the string as a value that satisfies error of type tagging.Error
|
||||
func Errorf(format string, a ...interface{}) error { |
||||
return Error{err: fmt.Errorf(format, a...)} |
||||
} |
||||
|
||||
// Unwrap the internal error.
|
||||
func (e Error) Unwrap() error { return e.err } |
||||
|
||||
// Error 'error' compatible method.
|
||||
func (e Error) Error() string { |
||||
if e.err == nil { |
||||
return "tagging: cause <nil>" |
||||
} |
||||
return e.err.Error() |
||||
} |
@ -0,0 +1,62 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"encoding/xml" |
||||
"unicode/utf8" |
||||
) |
||||
|
||||
// Tag - single tag
|
||||
type Tag struct { |
||||
XMLName xml.Name `xml:"Tag"` |
||||
Key string `xml:"Key"` |
||||
Value string `xml:"Value"` |
||||
} |
||||
|
||||
// Validate - validates the tag element
|
||||
func (t Tag) Validate() error { |
||||
if err := t.validateKey(); err != nil { |
||||
return err |
||||
} |
||||
if err := t.validateValue(); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// validateKey - checks if key is valid or not.
|
||||
func (t Tag) validateKey() error { |
||||
// cannot be longer than maxTagKeyLength characters
|
||||
if utf8.RuneCountInString(t.Key) > maxTagKeyLength { |
||||
return ErrInvalidTagKey |
||||
} |
||||
// cannot be empty
|
||||
if len(t.Key) == 0 { |
||||
return ErrInvalidTagKey |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// validateValue - checks if value is valid or not.
|
||||
func (t Tag) validateValue() error { |
||||
// cannot be longer than maxTagValueLength characters
|
||||
if utf8.RuneCountInString(t.Value) > maxTagValueLength { |
||||
return ErrInvalidTagValue |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,113 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/xml" |
||||
"io" |
||||
"net/url" |
||||
) |
||||
|
||||
// S3 API limits for tags
|
||||
// Ref: https://docs.aws.amazon.com/AmazonS3/latest/dev/object-tagging.html
|
||||
const ( |
||||
maxTags = 10 |
||||
maxTagKeyLength = 128 |
||||
maxTagValueLength = 256 |
||||
) |
||||
|
||||
// errors returned by tagging package
|
||||
var ( |
||||
ErrTooManyTags = Errorf("Cannot have more than 10 object tags") |
||||
ErrInvalidTagKey = Errorf("The TagKey you have provided is invalid") |
||||
ErrInvalidTagValue = Errorf("The TagValue you have provided is invalid") |
||||
ErrInvalidTag = Errorf("Cannot provide multiple Tags with the same key") |
||||
) |
||||
|
||||
// Tagging - object tagging interface
|
||||
type Tagging struct { |
||||
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ Tagging"` |
||||
TagSet TagSet `xml:"TagSet"` |
||||
} |
||||
|
||||
// Validate - validates the tagging configuration
|
||||
func (t Tagging) Validate() error { |
||||
// Tagging can't have more than 10 tags
|
||||
if len(t.TagSet.Tags) > maxTags { |
||||
return ErrTooManyTags |
||||
} |
||||
// Validate all the rules in the tagging config
|
||||
for _, ts := range t.TagSet.Tags { |
||||
if t.TagSet.ContainsDuplicate(ts.Key) { |
||||
return ErrInvalidTag |
||||
} |
||||
if err := ts.Validate(); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// String - returns a string in format "tag1=value1&tag2=value2" with all the
|
||||
// tags in this Tagging Struct
|
||||
func (t Tagging) String() string { |
||||
var buf bytes.Buffer |
||||
for _, tag := range t.TagSet.Tags { |
||||
if buf.Len() > 0 { |
||||
buf.WriteString("&") |
||||
} |
||||
buf.WriteString(tag.Key + "=") |
||||
buf.WriteString(tag.Value) |
||||
} |
||||
return buf.String() |
||||
} |
||||
|
||||
// FromString - returns a Tagging struct when given a string in format
|
||||
// "tag1=value1&tag2=value2"
|
||||
func FromString(tagStr string) (Tagging, error) { |
||||
tags, err := url.ParseQuery(tagStr) |
||||
if err != nil { |
||||
return Tagging{}, err |
||||
} |
||||
var idx = 0 |
||||
parsedTags := make([]Tag, len(tags)) |
||||
for k := range tags { |
||||
parsedTags[idx].Key = k |
||||
parsedTags[idx].Value = tags.Get(k) |
||||
idx++ |
||||
} |
||||
return Tagging{ |
||||
TagSet: TagSet{ |
||||
Tags: parsedTags, |
||||
}, |
||||
}, nil |
||||
} |
||||
|
||||
// ParseTagging - parses incoming xml data in given reader
|
||||
// into Tagging interface. After parsing, also validates the
|
||||
// parsed fields based on S3 API constraints.
|
||||
func ParseTagging(reader io.Reader) (*Tagging, error) { |
||||
var t Tagging |
||||
if err := xml.NewDecoder(reader).Decode(&t); err != nil { |
||||
return nil, err |
||||
} |
||||
if err := t.Validate(); err != nil { |
||||
return nil, err |
||||
} |
||||
return &t, nil |
||||
} |
@ -0,0 +1,41 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"encoding/xml" |
||||
) |
||||
|
||||
// TagSet - Set of tags under Tagging
|
||||
type TagSet struct { |
||||
XMLName xml.Name `xml:"TagSet"` |
||||
Tags []Tag `xml:"Tag"` |
||||
} |
||||
|
||||
// ContainsDuplicate - returns true if duplicate keys are present in TagSet
|
||||
func (t TagSet) ContainsDuplicate(key string) bool { |
||||
var found bool |
||||
for _, tag := range t.Tags { |
||||
if tag.Key == key { |
||||
if found { |
||||
return true |
||||
} |
||||
found = true |
||||
} |
||||
} |
||||
return false |
||||
} |
Loading…
Reference in new issue