Merge pull request #1174 from krishnasrinivas/presigned-post-s3v2
presigned post for s3v2
This commit is contained in:
+14
-1
@@ -87,7 +87,20 @@ func (c *s3Client) ShareDownload(expires time.Duration) (string, *probe.Error) {
|
||||
}
|
||||
|
||||
func (c *s3Client) ShareUpload(recursive bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) {
|
||||
return nil, probe.NewError(client.APINotImplemented{API: "Share", APIType: "filesystem"})
|
||||
bucket, object := c.url2BucketAndObject()
|
||||
p := minio.NewPostPolicy()
|
||||
p.SetExpires(time.Now().UTC().Add(expires))
|
||||
if len(contentType) > 0 {
|
||||
p.SetContentType(contentType)
|
||||
}
|
||||
p.SetBucket(bucket)
|
||||
if recursive {
|
||||
p.SetKeyStartsWith(object)
|
||||
} else {
|
||||
p.SetKey(object)
|
||||
}
|
||||
m, err := c.api.PresignedPostPolicy(p)
|
||||
return m, probe.NewError(err)
|
||||
}
|
||||
|
||||
// PutObject - put object
|
||||
|
||||
+16
@@ -556,6 +556,22 @@ func (a apiCore) putObject(bucket, object, contentType string, md5SumBytes []byt
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func (a apiCore) presignedPostPolicyRequest(p *PostPolicy) *request {
|
||||
r := new(request)
|
||||
r.config = a.config
|
||||
return r
|
||||
}
|
||||
|
||||
func (a apiCore) presignedPostPolicy(p *PostPolicy) map[string]string {
|
||||
r := a.presignedPostPolicyRequest(p)
|
||||
|
||||
policyBase64 := p.base64()
|
||||
p.formData["policy"] = policyBase64
|
||||
p.formData["AWSAccessKeyId"] = r.config.AccessKeyID
|
||||
p.formData["signature"] = r.PostPresignSignature(policyBase64)
|
||||
return p.formData
|
||||
}
|
||||
|
||||
func (a apiCore) presignedGetObjectRequest(bucket, object string, expires, offset, length int64) (*request, error) {
|
||||
op := &operation{
|
||||
HTTPServer: a.config.Endpoint,
|
||||
|
||||
+15
@@ -73,6 +73,7 @@ type ObjectAPI interface {
|
||||
// PresignedAPI - object specific for now
|
||||
type PresignedAPI interface {
|
||||
PresignedGetObject(bucket, object string, expires time.Duration) (string, error)
|
||||
PresignedPostPolicy(*PostPolicy) (map[string]string, error)
|
||||
}
|
||||
|
||||
// BucketStatCh - bucket metadata over read channel
|
||||
@@ -217,6 +218,20 @@ func New(config Config) (API, error) {
|
||||
return api{apiCore{&config}}, nil
|
||||
}
|
||||
|
||||
// PresignedPostPolicy return POST form data that can be used for object upload
|
||||
func (a api) PresignedPostPolicy(p *PostPolicy) (map[string]string, error) {
|
||||
if p.expiration.IsZero() {
|
||||
return nil, errors.New("Expiration time must be specified")
|
||||
}
|
||||
if _, ok := p.formData["key"]; !ok {
|
||||
return nil, errors.New("object key must be specified")
|
||||
}
|
||||
if _, ok := p.formData["bucket"]; !ok {
|
||||
return nil, errors.New("bucket name must be specified")
|
||||
}
|
||||
return a.presignedPostPolicy(p), nil
|
||||
}
|
||||
|
||||
/// Object operations
|
||||
|
||||
/// Expires maximum is 7days - ie. 604800 and minimum is 1
|
||||
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package minio
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// expirationDateFormat date format for expiration key in json policy
|
||||
const expirationDateFormat = "2006-01-02T15:04:05.999Z"
|
||||
|
||||
// contentLengthRange - min and max size of content
|
||||
type contentLengthRange struct {
|
||||
min int
|
||||
max int
|
||||
}
|
||||
|
||||
func (c contentLengthRange) marshalJSON() string {
|
||||
return fmt.Sprintf("[\"content-length-range\",%d,%d]", c.min, c.max)
|
||||
}
|
||||
|
||||
// Policy explanation: http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
|
||||
type policy struct {
|
||||
matchType string
|
||||
key string
|
||||
value string
|
||||
}
|
||||
|
||||
func (p policy) marshalJSON() string {
|
||||
return fmt.Sprintf("[\"%s\",\"%s\",\"%s\"]", p.matchType, p.key, p.value)
|
||||
}
|
||||
|
||||
// PostPolicy provides strict static type conversion and validation for Amazon S3's POST policy JSON string.
|
||||
type PostPolicy struct {
|
||||
expiration time.Time // expiration date and time of the POST policy.
|
||||
policies []policy
|
||||
contentLengthRange contentLengthRange
|
||||
|
||||
// Post form data
|
||||
formData map[string]string
|
||||
}
|
||||
|
||||
// NewPostPolicy instantiate new post policy
|
||||
func NewPostPolicy() *PostPolicy {
|
||||
p := &PostPolicy{}
|
||||
p.policies = make([]policy, 0)
|
||||
p.formData = make(map[string]string)
|
||||
return p
|
||||
}
|
||||
|
||||
// SetExpires expiration time
|
||||
func (p *PostPolicy) SetExpires(t time.Time) error {
|
||||
if t.IsZero() {
|
||||
return errors.New("time input invalid")
|
||||
}
|
||||
p.expiration = t
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetKey Object name
|
||||
func (p *PostPolicy) SetKey(key string) error {
|
||||
if strings.TrimSpace(key) == "" || key == "" {
|
||||
return errors.New("key invalid")
|
||||
}
|
||||
policy := policy{"eq", "$key", key}
|
||||
p.policies = append(p.policies, policy)
|
||||
p.formData["key"] = key
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetKeyStartsWith Object name that can start with
|
||||
func (p *PostPolicy) SetKeyStartsWith(keyStartsWith string) error {
|
||||
if strings.TrimSpace(keyStartsWith) == "" || keyStartsWith == "" {
|
||||
return errors.New("key-starts-with invalid")
|
||||
}
|
||||
policy := policy{"starts-with", "$key", keyStartsWith}
|
||||
p.policies = append(p.policies, policy)
|
||||
p.formData["key"] = keyStartsWith
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetBucket bucket name
|
||||
func (p *PostPolicy) SetBucket(bucket string) error {
|
||||
if strings.TrimSpace(bucket) == "" || bucket == "" {
|
||||
return errors.New("bucket invalid")
|
||||
}
|
||||
policy := policy{"eq", "$bucket", bucket}
|
||||
p.policies = append(p.policies, policy)
|
||||
p.formData["bucket"] = bucket
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetContentType content-type
|
||||
func (p *PostPolicy) SetContentType(contentType string) error {
|
||||
if strings.TrimSpace(contentType) == "" || contentType == "" {
|
||||
return errors.New("contentType invalid")
|
||||
}
|
||||
policy := policy{"eq", "$Content-Type", contentType}
|
||||
p.policies = append(p.policies, policy)
|
||||
p.formData["Content-Type"] = contentType
|
||||
return nil
|
||||
}
|
||||
|
||||
// marshalJSON provides Marshalled JSON
|
||||
func (p PostPolicy) marshalJSON() []byte {
|
||||
var expirationStr string
|
||||
if p.expiration.IsZero() == false {
|
||||
expirationStr = `"expiration":"` + p.expiration.Format(expirationDateFormat) + `"`
|
||||
}
|
||||
var policiesStr string
|
||||
policies := []string{}
|
||||
for _, policy := range p.policies {
|
||||
policies = append(policies, policy.marshalJSON())
|
||||
}
|
||||
if p.contentLengthRange.min != 0 || p.contentLengthRange.max != 0 {
|
||||
policies = append(policies, p.contentLengthRange.marshalJSON())
|
||||
}
|
||||
if len(policies) > 0 {
|
||||
policiesStr = `"conditions":[` + strings.Join(policies, ",") + "]"
|
||||
}
|
||||
retStr := "{"
|
||||
if len(expirationStr) > 0 {
|
||||
retStr = retStr + expirationStr + ","
|
||||
}
|
||||
if len(policiesStr) > 0 {
|
||||
retStr = retStr + policiesStr
|
||||
}
|
||||
retStr = retStr + "}"
|
||||
return []byte(retStr)
|
||||
}
|
||||
|
||||
// base64 produces base64 of PostPolicy's Marshalled json
|
||||
func (p PostPolicy) base64() string {
|
||||
return base64.StdEncoding.EncodeToString(p.marshalJSON())
|
||||
}
|
||||
+8
@@ -319,6 +319,14 @@ func (r *request) PreSignV2() (string, error) {
|
||||
return r.req.URL.String(), nil
|
||||
}
|
||||
|
||||
func (r *request) PostPresignSignature(policyBase64 string) string {
|
||||
hm := hmac.New(sha1.New, []byte(r.config.SecretAccessKey))
|
||||
hm.Write([]byte(policyBase64))
|
||||
|
||||
signature := base64.StdEncoding.EncodeToString(hm.Sum(nil))
|
||||
return signature
|
||||
}
|
||||
|
||||
// Authorization = "AWS" + " " + AWSAccessKeyId + ":" + Signature;
|
||||
// Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) );
|
||||
//
|
||||
|
||||
Vendored
+2
-2
@@ -34,8 +34,8 @@
|
||||
},
|
||||
{
|
||||
"path": "github.com/minio/minio-go-legacy",
|
||||
"revision": "dda8ea3a7d8ce5571c41fe5c465330113cbb8a6b",
|
||||
"revisionTime": "2015-10-08T15:15:47-07:00"
|
||||
"revision": "89b0e06a751c2d987292db8c5e1c7a486b6420db",
|
||||
"revisionTime": "2015-10-08T16:58:29-07:00"
|
||||
},
|
||||
{
|
||||
"path": "github.com/minio/minio/pkg/probe",
|
||||
|
||||
Reference in New Issue
Block a user