2015-01-21 08:16:06 +01:00
|
|
|
/*
|
2015-03-19 22:35:50 +01:00
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
2015-01-21 08:16:06 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-03-24 04:40:21 +01:00
|
|
|
package drivers
|
2015-01-18 22:31:22 +01:00
|
|
|
|
2015-01-19 01:10:48 +01:00
|
|
|
import (
|
|
|
|
"io"
|
2015-01-22 00:22:15 +01:00
|
|
|
"regexp"
|
2015-01-22 02:12:47 +01:00
|
|
|
"time"
|
2015-02-28 04:42:04 +01:00
|
|
|
"unicode/utf8"
|
2015-01-19 01:10:48 +01:00
|
|
|
)
|
2015-01-18 22:31:22 +01:00
|
|
|
|
2015-03-24 04:40:21 +01:00
|
|
|
// Driver - generic API interface for various drivers - donut, file, memory
|
|
|
|
type Driver interface {
|
2015-01-21 21:44:09 +01:00
|
|
|
// Bucket Operations
|
2015-02-16 02:03:27 +01:00
|
|
|
ListBuckets() ([]BucketMetadata, error)
|
2015-03-12 04:14:40 +01:00
|
|
|
CreateBucket(bucket string) error
|
2015-03-12 08:36:33 +01:00
|
|
|
CreateBucketPolicy(bucket string, p BucketPolicy) error
|
2015-03-08 23:57:18 +01:00
|
|
|
GetBucketPolicy(bucket string) (BucketPolicy, error)
|
2015-01-21 03:39:30 +01:00
|
|
|
|
2015-01-21 21:44:09 +01:00
|
|
|
// Object Operations
|
2015-03-12 08:36:33 +01:00
|
|
|
GetObject(w io.Writer, bucket, object string) (int64, error)
|
|
|
|
GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error)
|
2015-03-08 04:10:55 +01:00
|
|
|
GetObjectMetadata(bucket string, object string, prefix string) (ObjectMetadata, error)
|
2015-02-27 02:23:42 +01:00
|
|
|
ListObjects(bucket string, resources BucketResourcesMetadata) ([]ObjectMetadata, BucketResourcesMetadata, error)
|
2015-03-17 21:32:10 +01:00
|
|
|
CreateObject(bucket string, key string, contentType string, md5sum string, data io.Reader) error
|
2015-01-21 09:50:23 +01:00
|
|
|
}
|
|
|
|
|
2015-03-06 05:32:04 +01:00
|
|
|
// BucketMetadata - name and create date
|
2015-01-21 09:50:23 +01:00
|
|
|
type BucketMetadata struct {
|
|
|
|
Name string
|
2015-01-22 02:12:47 +01:00
|
|
|
Created time.Time
|
2015-01-21 09:50:23 +01:00
|
|
|
}
|
|
|
|
|
2015-03-06 05:32:04 +01:00
|
|
|
// ObjectMetadata - object key and its relevant metadata
|
2015-01-21 03:39:30 +01:00
|
|
|
type ObjectMetadata struct {
|
2015-02-28 23:44:26 +01:00
|
|
|
Bucket string
|
|
|
|
Key string
|
2015-02-05 02:32:40 +01:00
|
|
|
|
|
|
|
ContentType string
|
|
|
|
Created time.Time
|
2015-03-17 02:03:03 +01:00
|
|
|
Md5 string
|
2015-02-05 02:32:40 +01:00
|
|
|
Size int64
|
2015-01-21 03:39:30 +01:00
|
|
|
}
|
2015-01-22 00:22:15 +01:00
|
|
|
|
2015-03-09 00:58:36 +01:00
|
|
|
// FilterMode type
|
|
|
|
type FilterMode int
|
|
|
|
|
|
|
|
// FilterMode list
|
|
|
|
const (
|
|
|
|
DelimiterPrefixMode FilterMode = iota
|
|
|
|
DelimiterMode
|
|
|
|
PrefixMode
|
|
|
|
DefaultMode
|
|
|
|
)
|
|
|
|
|
2015-03-06 05:32:04 +01:00
|
|
|
// BucketResourcesMetadata - various types of bucket resources
|
2015-02-27 02:23:42 +01:00
|
|
|
type BucketResourcesMetadata struct {
|
|
|
|
Prefix string
|
|
|
|
Marker string
|
|
|
|
Maxkeys int
|
|
|
|
Delimiter string
|
|
|
|
IsTruncated bool
|
|
|
|
CommonPrefixes []string
|
2015-03-09 00:58:36 +01:00
|
|
|
Mode FilterMode
|
2015-02-27 02:23:42 +01:00
|
|
|
|
|
|
|
Policy bool
|
|
|
|
// TODO
|
|
|
|
Logging string
|
|
|
|
Notification string
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:58:36 +01:00
|
|
|
// GetMode - Populate filter mode
|
|
|
|
func GetMode(resources BucketResourcesMetadata) FilterMode {
|
|
|
|
var f FilterMode
|
|
|
|
switch true {
|
|
|
|
case resources.Delimiter != "" && resources.Prefix != "":
|
|
|
|
f = DelimiterPrefixMode
|
|
|
|
case resources.Delimiter != "" && resources.Prefix == "":
|
|
|
|
f = DelimiterMode
|
|
|
|
case resources.Delimiter == "" && resources.Prefix != "":
|
|
|
|
f = PrefixMode
|
|
|
|
case resources.Delimiter == "" && resources.Prefix == "":
|
|
|
|
f = DefaultMode
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDelimiterPrefixSet Delimiter and Prefix set
|
|
|
|
func (b BucketResourcesMetadata) IsDelimiterPrefixSet() bool {
|
|
|
|
return b.Mode == DelimiterPrefixMode
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDelimiterSet Delimiter set
|
|
|
|
func (b BucketResourcesMetadata) IsDelimiterSet() bool {
|
|
|
|
return b.Mode == DelimiterMode
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPrefixSet Prefix set
|
|
|
|
func (b BucketResourcesMetadata) IsPrefixSet() bool {
|
|
|
|
return b.Mode == PrefixMode
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDefault No query values
|
|
|
|
func (b BucketResourcesMetadata) IsDefault() bool {
|
|
|
|
return b.Mode == DefaultMode
|
|
|
|
}
|
|
|
|
|
2015-03-06 05:32:04 +01:00
|
|
|
// IsValidBucket - verify bucket name in accordance with
|
|
|
|
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html
|
2015-01-22 00:22:15 +01:00
|
|
|
func IsValidBucket(bucket string) bool {
|
|
|
|
if len(bucket) < 3 || len(bucket) > 63 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if bucket[0] == '.' || bucket[len(bucket)-1] == '.' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if match, _ := regexp.MatchString("\\.\\.", bucket); match == true {
|
|
|
|
return false
|
|
|
|
}
|
2015-02-23 11:11:27 +01:00
|
|
|
// We don't support buckets with '.' in them
|
|
|
|
match, _ := regexp.MatchString("^[a-zA-Z][a-zA-Z0-9\\-]+[a-zA-Z0-9]$", bucket)
|
2015-01-22 00:22:15 +01:00
|
|
|
return match
|
|
|
|
}
|
2015-01-26 02:41:59 +01:00
|
|
|
|
2015-03-06 05:32:04 +01:00
|
|
|
// IsValidObject - verify object name in accordance with
|
|
|
|
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
|
2015-01-26 02:41:59 +01:00
|
|
|
func IsValidObject(object string) bool {
|
2015-02-28 04:42:04 +01:00
|
|
|
if len(object) > 1024 || len(object) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2015-02-28 04:49:18 +01:00
|
|
|
if !utf8.ValidString(object) {
|
2015-02-28 04:42:04 +01:00
|
|
|
return false
|
|
|
|
}
|
2015-01-26 02:41:59 +01:00
|
|
|
return true
|
|
|
|
}
|