Merge pull request #666 from harshavardhana/pr_out_update_to_new_changes_in_objectstorage_go
This commit is contained in:
Generated
+1
-1
@@ -36,7 +36,7 @@
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/minio/objectstorage-go",
|
||||
"Rev": "67b1b645616fbc186425f25d914c13eb10ae96b4"
|
||||
"Rev": "83fd992ca0e7ba5a26317a41c1c298244b0ce83d"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/shiena/ansicolor",
|
||||
|
||||
+2
@@ -2,3 +2,5 @@ language: go
|
||||
go:
|
||||
- 1.4.2
|
||||
sudo: false
|
||||
notifications:
|
||||
slack: travisci:0lsU2EbtMOizjtgSmBZUNEQS
|
||||
+6
-7
@@ -215,8 +215,7 @@ 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 becomes multipart
|
||||
// one can change this value during a library import
|
||||
// MinimumPartSize minimum part size per object after which PutObject behaves internally as multipart
|
||||
var MinimumPartSize uint64 = 1024 * 1024 * 5
|
||||
|
||||
// maxParts - unexported right now
|
||||
@@ -226,13 +225,13 @@ var maxParts = uint64(10000)
|
||||
var maxPartSize uint64 = 1024 * 1024 * 1024 * 5
|
||||
|
||||
// GetPartSize - calculate the optimal part size for the given objectSize
|
||||
//
|
||||
// NOTE: Assumption here is that for any given object upload to a S3 compatible object
|
||||
// storage it will have the following parameters as constants
|
||||
// ---------
|
||||
// maxParts
|
||||
// maximumPartSize
|
||||
// minimumPartSize
|
||||
// ---------
|
||||
//
|
||||
// maxParts
|
||||
// maximumPartSize
|
||||
// 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
|
||||
|
||||
+161
-10
@@ -14,30 +14,145 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package objectstorage_test
|
||||
package objectstorage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
. "github.com/minio/objectstorage-go"
|
||||
)
|
||||
|
||||
func TestBucketOperations(t *testing.T) {
|
||||
ts := httptest.NewServer(robotsTxtHandler)
|
||||
defer ts.Close()
|
||||
func ExampleGetPartSize() {
|
||||
fmt.Println(GetPartSize(5000000000))
|
||||
// Output: 5242880
|
||||
}
|
||||
func ExampleGetPartSize_second() {
|
||||
fmt.Println(GetPartSize(50000000000000000))
|
||||
// Output: 5368709120
|
||||
}
|
||||
|
||||
func TestObjectOperations(t *testing.T) {
|
||||
ts := httptest.NewServer(robotsTxtHandler)
|
||||
defer ts.Close()
|
||||
// bucketHandler is an http.Handler that verifies bucket responses and validates incoming requests
|
||||
type bucketHandler struct {
|
||||
resource string
|
||||
}
|
||||
|
||||
func (h bucketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.Method == "GET":
|
||||
if r.URL.Path == "/" {
|
||||
response := []byte("<ListAllMyBucketsResult xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><Buckets><Bucket><Name>bucket</Name><CreationDate>2015-05-20T23:05:09.230Z</CreationDate></Bucket></Buckets><Owner><ID>minio</ID><DisplayName>minio</DisplayName></Owner></ListAllMyBucketsResult>")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
|
||||
w.Write(response)
|
||||
}
|
||||
case r.Method == "POST":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
case r.Method == "PUT":
|
||||
switch {
|
||||
case r.URL.Path == h.resource:
|
||||
_, ok := r.URL.Query()["acl"]
|
||||
if ok {
|
||||
if r.Header.Get("x-amz-acl") != "public-read-write" {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
case r.Method == "HEAD":
|
||||
switch {
|
||||
case r.URL.Path == h.resource:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
case r.Method == "DELETE":
|
||||
switch {
|
||||
case r.URL.Path != h.resource:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
default:
|
||||
h.resource = ""
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBucketOperations(t *testing.T) {
|
||||
bucket := bucketHandler(bucketHandler{
|
||||
resource: "/bucket",
|
||||
})
|
||||
server := httptest.NewServer(bucket)
|
||||
defer server.Close()
|
||||
|
||||
a := New(&Config{Endpoint: server.URL})
|
||||
err := a.MakeBucket("bucket", "private", "")
|
||||
if err != nil {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
|
||||
err = a.StatBucket("bucket")
|
||||
if err != nil {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
|
||||
err = a.StatBucket("bucket1")
|
||||
if err == nil {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
if err.Error() != "403 Forbidden" {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
|
||||
err = a.SetBucketACL("bucket", "public-read-write")
|
||||
if err != nil {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
|
||||
for b := range a.ListBuckets() {
|
||||
if b.Err != nil {
|
||||
t.Fatalf(b.Err.Error())
|
||||
}
|
||||
if b.Data.Name != "bucket" {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
}
|
||||
err = a.DeleteBucket("bucket")
|
||||
if err != nil {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
|
||||
err = a.DeleteBucket("bucket1")
|
||||
if err == nil {
|
||||
t.Fatalf("Error")
|
||||
}
|
||||
if err.Error() != "404 Not Found" {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
}
|
||||
|
||||
// objectHandler is an http.Handler that verifies object responses and validates incoming requests
|
||||
type objectHandler struct {
|
||||
resource string
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (h objectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.Method == "PUT":
|
||||
case r.Method == "HEAD":
|
||||
case r.Method == "POST":
|
||||
case r.Method == "GET":
|
||||
case r.Method == "DELETE":
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartSize(t *testing.T) {
|
||||
var maxPartSize uint64 = 1024 * 1024 * 1024 * 5
|
||||
partSize := GetPartSize(5000000000000000000)
|
||||
if partSize > MinimumPartSize {
|
||||
if partSize != maxPartSize {
|
||||
if partSize > maxPartSize {
|
||||
t.Fatal("invalid result, cannot be bigger than MaxPartSize 5GB")
|
||||
}
|
||||
}
|
||||
@@ -46,3 +161,39 @@ func TestPartSize(t *testing.T) {
|
||||
t.Fatal("invalid result, cannot be bigger than MinimumPartSize 5MB")
|
||||
}
|
||||
}
|
||||
|
||||
func TestURLEncoding(t *testing.T) {
|
||||
type urlStrings struct {
|
||||
name string
|
||||
encodedName string
|
||||
}
|
||||
|
||||
want := []urlStrings{
|
||||
{
|
||||
name: "bigfile-1._%",
|
||||
encodedName: "bigfile-1._%25",
|
||||
},
|
||||
{
|
||||
name: "本語",
|
||||
encodedName: "%E6%9C%AC%E8%AA%9E",
|
||||
},
|
||||
{
|
||||
name: "本b語.1",
|
||||
encodedName: "%E6%9C%ACb%E8%AA%9E.1",
|
||||
},
|
||||
{
|
||||
name: ">123>3123123",
|
||||
encodedName: "%3E123%3E3123123",
|
||||
},
|
||||
}
|
||||
|
||||
for _, u := range want {
|
||||
encodedName, err := urlEncodeName(u.name)
|
||||
if err != nil {
|
||||
t.Fatalf("Error")
|
||||
}
|
||||
if u.encodedName != encodedName {
|
||||
t.Errorf("Error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-1
@@ -326,7 +326,14 @@ func (a *lowLevelAPI) deleteBucket(bucket string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return resp.Body.Close()
|
||||
defer resp.Body.Close()
|
||||
if resp != nil {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// Head has no response body, handle it
|
||||
return fmt.Errorf("%s", resp.Status)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Object Read/Write/Stat Operations
|
||||
@@ -544,6 +551,10 @@ func (a *lowLevelAPI) listBuckets() (*listAllMyBucketsResult, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp != nil {
|
||||
// for un-authenticated requests, amazon sends a redirect handle it
|
||||
if resp.StatusCode == http.StatusTemporaryRedirect {
|
||||
return nil, fmt.Errorf("%s", resp.Status)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, responseToError(resp)
|
||||
}
|
||||
|
||||
+11
-3
@@ -103,11 +103,19 @@ func (r *request) Do() (resp *http.Response, err error) {
|
||||
if r.config.AccessKeyID != "" && r.config.SecretAccessKey != "" {
|
||||
r.SignV4()
|
||||
}
|
||||
client := &http.Client{}
|
||||
transport := http.DefaultTransport
|
||||
if r.config.Transport != nil {
|
||||
client.Transport = r.config.Transport
|
||||
transport = r.config.Transport
|
||||
}
|
||||
return client.Do(r.req)
|
||||
// do not use http.Client{}, while it may seem intuitive but the problem seems to be
|
||||
// that http.Client{} internally follows redirects and there is no easier way to disable
|
||||
// it from outside using a configuration parameter -
|
||||
// this auto redirect causes complications in verifying subsequent errors
|
||||
//
|
||||
// The best is to use RoundTrip() directly, so the request comes back to the caller where
|
||||
// we are going to handle such replies. And indeed that is the right thing to do here.
|
||||
//
|
||||
return transport.RoundTrip(r.req)
|
||||
}
|
||||
|
||||
// Set - set additional headers if any
|
||||
|
||||
Reference in New Issue
Block a user