Update to new minio-go

This commit is contained in:
Harshavardhana
2015-06-11 15:51:27 -07:00
parent 388c443010
commit 84da2396c7
7 changed files with 112 additions and 55 deletions
+1 -1
View File
@@ -32,7 +32,7 @@
},
{
"ImportPath": "github.com/minio/minio-go",
"Rev": "a41bd929a5c2451970eb6fa731156d9481f77b1c"
"Rev": "76a773cadcbf32c9d437a2871b1e449b80be255b"
},
{
"ImportPath": "github.com/minio/minio/pkg/iodine",
+28 -10
View File
@@ -137,12 +137,30 @@ type Config struct {
// Standard options
AccessKeyID string
SecretAccessKey string
Region string // Optional field. If empty, region is determined automatically.
Endpoint string
// Advanced options
AcceptType string // specify this to get server response in non XML style if server supports it
Transport http.RoundTripper // custom transport usually for debugging, by default its nil
// Specify this to get server response in non XML style if server supports it
AcceptType string
// Optional field. If empty, region is determined automatically.
Region string
// Expert options
//
// Set this to override default transport ``http.DefaultTransport``
//
// This transport is usually needed for debugging OR to add your own
// custom TLS certificates on the client transport, for custom CA's and
// certs which are not part of standard certificate authority
//
// For example :-
//
// tr := &http.Transport{
// TLSClientConfig: &tls.Config{RootCAs: pool},
// DisableCompression: true,
// }
//
Transport http.RoundTripper
// internal
// use SetUserAgent append to default, useful when minio-go is used with in your application
@@ -208,8 +226,8 @@ func (a completedParts) Len() int { return len(a) }
func (a completedParts) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a completedParts) Less(i, j int) bool { return a[i].PartNumber < a[j].PartNumber }
// MinimumPartSize minimum part size per object after which PutObject behaves internally as multipart
var MinimumPartSize int64 = 1024 * 1024 * 5
// minimumPartSize minimum part size per object after which PutObject behaves internally as multipart
var minimumPartSize int64 = 1024 * 1024 * 5
// maxParts - unexported right now
var maxParts = int64(10000)
@@ -226,7 +244,7 @@ var maxPartSize int64 = 1024 * 1024 * 1024 * 5
// maximumPartSize
// minimumPartSize
//
// if a the partSize after division with maxParts is greater than MinimumPartSize
// if a the partSize after division with maxParts is greater than minimumPartSize
// then choose that to be the new part size, if not return MinimumPartSize
//
// special case where it happens to be that partSize is indeed bigger than the
@@ -234,13 +252,13 @@ var maxPartSize int64 = 1024 * 1024 * 1024 * 5
func getPartSize(objectSize int64) int64 {
partSize := (objectSize / (maxParts - 1)) // make sure last part has enough buffer and handle this poperly
{
if partSize > MinimumPartSize {
if partSize > minimumPartSize {
if partSize > maxPartSize {
return maxPartSize
}
return partSize
}
return MinimumPartSize
return minimumPartSize
}
}
@@ -419,9 +437,9 @@ func (a api) PutObject(bucket, object, contentType string, size int64, data io.R
return err
}
switch {
case size < MinimumPartSize:
case size < minimumPartSize:
// Single Part use case, use PutObject directly
for part := range multiPart(data, MinimumPartSize, nil) {
for part := range multiPart(data, minimumPartSize, nil) {
if part.Err != nil {
return part.Err
}
+10 -6
View File
@@ -199,14 +199,14 @@ func TestObjectOperations(t *testing.T) {
func TestPartSize(t *testing.T) {
var maxPartSize int64 = 1024 * 1024 * 1024 * 5
partSize := getPartSize(5000000000000000000)
if partSize > MinimumPartSize {
if partSize > minimumPartSize {
if partSize > maxPartSize {
t.Fatal("invalid result, cannot be bigger than MaxPartSize 5GB")
t.Fatal("invalid result, cannot be bigger than maxPartSize 5GB")
}
}
partSize = getPartSize(50000000000)
if partSize > MinimumPartSize {
t.Fatal("invalid result, cannot be bigger than MinimumPartSize 5MB")
if partSize > minimumPartSize {
t.Fatal("invalid result, cannot be bigger than minimumPartSize 5MB")
}
}
@@ -251,9 +251,10 @@ func TestURLEncoding(t *testing.T) {
}
func TestErrorResponse(t *testing.T) {
a := lowLevelAPI{&Config{}}
errorResponse := []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Error><Code>AccessDenied</Code><Message>Access Denied</Message><Resource>/mybucket/myphoto.jpg</Resource><RequestId>F19772218238A85A</RequestId><HostId>GuWkjyviSiGHizehqpmsD1ndz5NClSP19DOT+s2mv7gXGQ8/X1lhbDGiIJEXpGFD</HostId></Error>")
errorReader := bytes.NewReader(errorResponse)
err := responseToError(errorReader)
err := a.responseToError(errorReader)
if err == nil {
t.Fatal("Error")
}
@@ -280,7 +281,10 @@ func TestErrorResponse(t *testing.T) {
if resp.HostID != "GuWkjyviSiGHizehqpmsD1ndz5NClSP19DOT+s2mv7gXGQ8/X1lhbDGiIJEXpGFD" {
t.Fatal("Error")
}
if resp.XML() == "" {
if resp.ToXML() == "" {
t.Fatal("Error")
}
if resp.ToJSON() == "" {
t.Fatal("Error")
}
}
+11 -5
View File
@@ -17,6 +17,7 @@
package minio
import (
"encoding/json"
"encoding/xml"
"io"
"strings"
@@ -55,7 +56,7 @@ type ErrorResponse struct {
// ..., err := s3.GetObject(...)
// if err != nil {
// resp := s3.ToErrorResponse(err)
// fmt.Println(resp.XML())
// fmt.Println(resp.ToXML())
// }
// ...
// ...
@@ -69,11 +70,17 @@ func ToErrorResponse(err error) *ErrorResponse {
}
// XML send raw xml marshalled as string
func (e ErrorResponse) XML() string {
func (e ErrorResponse) ToXML() string {
b, _ := xml.Marshal(&e)
return string(b)
}
// JSON send raw json marshalled as string
func (e ErrorResponse) ToJSON() string {
b, _ := json.Marshal(&e)
return string(b)
}
// Error formats HTTP error string
func (e ErrorResponse) Error() string {
return e.Message
@@ -82,10 +89,9 @@ func (e ErrorResponse) Error() string {
/// Internal function not exposed
// responseToError returns a new encoded ErrorResponse structure
func responseToError(body io.Reader) error {
func (a lowLevelAPI) responseToError(errBody io.Reader) error {
var respError ErrorResponse
decoder := xml.NewDecoder(body)
err := decoder.Decode(&respError)
err := acceptTypeDecoder(errBody, a.config.AcceptType, &respError)
if err != nil {
return err
}
+24 -19
View File
@@ -19,6 +19,7 @@ package minio
import (
"bytes"
"encoding/base64"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
@@ -35,6 +36,8 @@ type lowLevelAPI struct {
// putBucketRequest wrapper creates a new PutBucket request
func (a lowLevelAPI) putBucketRequest(bucket, acl, location string) (*request, error) {
var r *request
var err error
op := &operation{
HTTPServer: a.config.Endpoint,
HTTPMethod: "PUT",
@@ -46,14 +49,20 @@ func (a lowLevelAPI) putBucketRequest(bucket, acl, location string) (*request, e
case location != "":
createBucketConfig := new(createBucketConfiguration)
createBucketConfig.Location = location
createBucketConfigBytes, err := xml.Marshal(createBucketConfig)
var createBucketConfigBytes []byte
switch {
case a.config.AcceptType == "application/xml":
createBucketConfigBytes, err = xml.Marshal(createBucketConfig)
case a.config.AcceptType == "application/json":
createBucketConfigBytes, err = json.Marshal(createBucketConfig)
default:
createBucketConfigBytes, err = xml.Marshal(createBucketConfig)
}
if err != nil {
return nil, err
}
createBucketConfigBuffer = bytes.NewReader(createBucketConfigBytes)
}
var r *request
var err error
switch {
case createBucketConfigBuffer == nil:
r, err = newRequest(op, a.config, nil)
@@ -112,7 +121,7 @@ func (a lowLevelAPI) putBucket(bucket, acl, location string) error {
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return responseToError(resp.Body)
return a.responseToError(resp.Body)
}
}
return nil
@@ -146,7 +155,7 @@ func (a lowLevelAPI) putBucketACL(bucket, acl string) error {
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return responseToError(resp.Body)
return a.responseToError(resp.Body)
}
}
return nil
@@ -179,12 +188,11 @@ func (a lowLevelAPI) getBucketACL(bucket string) (accessControlPolicy, error) {
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return accessControlPolicy{}, responseToError(resp.Body)
return accessControlPolicy{}, a.responseToError(resp.Body)
}
}
policy := accessControlPolicy{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&policy)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &policy)
if err != nil {
return accessControlPolicy{}, err
}
@@ -227,12 +235,11 @@ func (a lowLevelAPI) getBucketLocation(bucket string) (string, error) {
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return "", responseToError(resp.Body)
return "", a.responseToError(resp.Body)
}
}
var locationConstraint string
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&locationConstraint)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &locationConstraint)
if err != nil {
return "", err
}
@@ -311,12 +318,11 @@ func (a lowLevelAPI) listObjects(bucket, marker, prefix, delimiter string, maxke
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return listBucketResult{}, responseToError(resp.Body)
return listBucketResult{}, a.responseToError(resp.Body)
}
}
listBucketResult := listBucketResult{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&listBucketResult)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &listBucketResult)
if err != nil {
return listBucketResult, err
}
@@ -467,7 +473,7 @@ func (a lowLevelAPI) putObject(bucket, object, contentType string, md5SumBytes [
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return ObjectStat{}, responseToError(resp.Body)
return ObjectStat{}, a.responseToError(resp.Body)
}
}
var metadata ObjectStat
@@ -523,7 +529,7 @@ func (a lowLevelAPI) getPartialObject(bucket, object string, offset, length int6
case http.StatusOK:
case http.StatusPartialContent:
default:
return nil, ObjectStat{}, responseToError(resp.Body)
return nil, ObjectStat{}, a.responseToError(resp.Body)
}
}
md5sum := strings.Trim(resp.Header.Get("ETag"), "\"") // trim off the odd double quotes
@@ -702,12 +708,11 @@ func (a lowLevelAPI) listBuckets() (listAllMyBucketsResult, error) {
return listAllMyBucketsResult{}, errors.New(resp.Status)
}
if resp.StatusCode != http.StatusOK {
return listAllMyBucketsResult{}, responseToError(resp.Body)
return listAllMyBucketsResult{}, a.responseToError(resp.Body)
}
}
listAllMyBucketsResult := listAllMyBucketsResult{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&listAllMyBucketsResult)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &listAllMyBucketsResult)
if err != nil {
return listAllMyBucketsResult, err
}
+19
View File
@@ -21,6 +21,8 @@ import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"encoding/xml"
"errors"
"io"
"regexp"
@@ -28,6 +30,23 @@ import (
"unicode/utf8"
)
type decoder interface {
Decode(v interface{}) error
}
func acceptTypeDecoder(body io.Reader, acceptType string, v interface{}) error {
var decoder decoder
switch {
case acceptType == "application/xml":
decoder = xml.NewDecoder(body)
case acceptType == "application/json":
decoder = json.NewDecoder(body)
default:
decoder = xml.NewDecoder(body)
}
return decoder.Decode(v)
}
// urlEncodedName- encode the strings from UTF-8 byte representations to HTML hex escape sequences
func urlEncodeName(objectName string) (string, error) {
// if object matches reserved string, no need to encode them
+19 -14
View File
@@ -20,6 +20,7 @@ import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/xml"
"fmt"
"io"
@@ -102,12 +103,11 @@ func (a lowLevelAPI) listMultipartUploads(bucket, keymarker, uploadIDMarker, pre
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return listMultipartUploadsResult{}, responseToError(resp.Body)
return listMultipartUploadsResult{}, a.responseToError(resp.Body)
}
}
listMultipartUploadsResult := listMultipartUploadsResult{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&listMultipartUploadsResult)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &listMultipartUploadsResult)
if err != nil {
return listMultipartUploadsResult, err
}
@@ -142,12 +142,11 @@ func (a lowLevelAPI) initiateMultipartUpload(bucket, object string) (initiateMul
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return initiateMultipartUploadResult{}, responseToError(resp.Body)
return initiateMultipartUploadResult{}, a.responseToError(resp.Body)
}
}
initiateMultipartUploadResult := initiateMultipartUploadResult{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&initiateMultipartUploadResult)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &initiateMultipartUploadResult)
if err != nil {
return initiateMultipartUploadResult, err
}
@@ -165,7 +164,15 @@ func (a lowLevelAPI) completeMultipartUploadRequest(bucket, object, uploadID str
HTTPMethod: "POST",
HTTPPath: "/" + bucket + "/" + encodedObject + "?uploadId=" + uploadID,
}
completeMultipartUploadBytes, err := xml.Marshal(complete)
var completeMultipartUploadBytes []byte
switch {
case a.config.AcceptType == "application/xml":
completeMultipartUploadBytes, err = xml.Marshal(complete)
case a.config.AcceptType == "application/json":
completeMultipartUploadBytes, err = json.Marshal(complete)
default:
completeMultipartUploadBytes, err = xml.Marshal(complete)
}
if err != nil {
return nil, err
}
@@ -191,12 +198,11 @@ func (a lowLevelAPI) completeMultipartUpload(bucket, object, uploadID string, c
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return completeMultipartUploadResult{}, responseToError(resp.Body)
return completeMultipartUploadResult{}, a.responseToError(resp.Body)
}
}
completeMultipartUploadResult := completeMultipartUploadResult{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&completeMultipartUploadResult)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &completeMultipartUploadResult)
if err != nil {
return completeMultipartUploadResult, err
}
@@ -294,12 +300,11 @@ func (a lowLevelAPI) listObjectParts(bucket, object, uploadID string, partNumber
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return listObjectPartsResult{}, responseToError(resp.Body)
return listObjectPartsResult{}, a.responseToError(resp.Body)
}
}
listObjectPartsResult := listObjectPartsResult{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&listObjectPartsResult)
err = acceptTypeDecoder(resp.Body, a.config.AcceptType, &listObjectPartsResult)
if err != nil {
return listObjectPartsResult, err
}
@@ -345,7 +350,7 @@ func (a lowLevelAPI) uploadPart(bucket, object, uploadID string, md5SumBytes []b
defer resp.Body.Close()
if resp != nil {
if resp.StatusCode != http.StatusOK {
return completePart{}, responseToError(resp.Body)
return completePart{}, a.responseToError(resp.Body)
}
}
return cPart, nil