Skip to content

Commit

Permalink
fix: more API refactors, remove all redundant functions (#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana authored Jul 17, 2020
1 parent 16b4b13 commit 36db8b2
Show file tree
Hide file tree
Showing 8 changed files with 552 additions and 729 deletions.
49 changes: 15 additions & 34 deletions api-remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,21 @@ type RemoveObjectError struct {
}

// generateRemoveMultiObjects - generate the XML request for remove multi objects request
func generateRemoveMultiObjectsRequest(objects []ObjectVersion) []byte {
rmObjects := []deleteObject{}
func generateRemoveMultiObjectsRequest(objects []ObjectInfo) []byte {
delObjects := []deleteObject{}
for _, obj := range objects {
rmObjects = append(rmObjects, deleteObject(obj))
delObjects = append(delObjects, deleteObject{
Key: obj.Key,
VersionID: obj.VersionID,
})
}
xmlBytes, _ := xml.Marshal(deleteMultiObjects{Objects: rmObjects, Quiet: true})
xmlBytes, _ := xml.Marshal(deleteMultiObjects{Objects: delObjects, Quiet: true})
return xmlBytes
}

// processRemoveMultiObjectsResponse - parse the remove multi objects web service
// and return the success/failure result status for each object
func processRemoveMultiObjectsResponse(body io.Reader, objects []ObjectVersion, errorCh chan<- RemoveObjectError) {
func processRemoveMultiObjectsResponse(body io.Reader, objects []ObjectInfo, errorCh chan<- RemoveObjectError) {
// Parse multi delete XML response
rmResult := &deleteMultiObjectsResult{}
err := xmlDecoder(body, rmResult)
Expand All @@ -154,16 +157,15 @@ func processRemoveMultiObjectsResponse(body io.Reader, objects []ObjectVersion,
}
}

// ObjectVersion points to a specific object version
type ObjectVersion struct {
Key string
VersionID string
// RemoveObjectsOptions represents options specified by user for RemoveObjects call
type RemoveObjectsOptions struct {
GovernanceBypass bool
}

// RemoveObjectsWithVersions removes multiple objects from a bucket while
// RemoveObjects removes multiple objects from a bucket while
// it is possible to specify objects versions which are received from
// objectsCh. Remove failures are sent back via error channel.
func (c Client) RemoveObjectsWithVersions(ctx context.Context, bucketName string, objectsCh <-chan ObjectVersion, opts RemoveObjectsOptions) <-chan RemoveObjectError {
func (c Client) RemoveObjects(ctx context.Context, bucketName string, objectsCh <-chan ObjectInfo, opts RemoveObjectsOptions) <-chan RemoveObjectError {
errorCh := make(chan RemoveObjectError, 1)

// Validate if bucket name is valid.
Expand All @@ -188,7 +190,7 @@ func (c Client) RemoveObjectsWithVersions(ctx context.Context, bucketName string
}

// Generate and call MultiDelete S3 requests based on entries received from objectsCh
func (c Client) removeObjects(ctx context.Context, bucketName string, objectsCh <-chan ObjectVersion, errorCh chan<- RemoveObjectError, opts RemoveObjectsOptions) {
func (c Client) removeObjects(ctx context.Context, bucketName string, objectsCh <-chan ObjectInfo, errorCh chan<- RemoveObjectError, opts RemoveObjectsOptions) {
maxEntries := 1000
finish := false
urlValues := make(url.Values)
Expand All @@ -203,7 +205,7 @@ func (c Client) removeObjects(ctx context.Context, bucketName string, objectsCh
break
}
count := 0
var batch []ObjectVersion
var batch []ObjectInfo

// Try to gather 1000 entries
for object := range objectsCh {
Expand Down Expand Up @@ -264,27 +266,6 @@ func (c Client) removeObjects(ctx context.Context, bucketName string, objectsCh
}
}

// RemoveObjectsOptions represents options specified by user for RemoveObjects call
type RemoveObjectsOptions struct {
GovernanceBypass bool
}

// RemoveObjects removes multiple objects from a bucket.
// The list of objects to remove are received from objectsCh.
// Remove failures are sent back via error channel.
func (c Client) RemoveObjects(ctx context.Context, bucketName string, objectsCh <-chan string, opts RemoveObjectsOptions) <-chan RemoveObjectError {
objectsVersionsCh := make(chan ObjectVersion)
go func() {
defer close(objectsVersionsCh)
for obj := range objectsCh {
objectsVersionsCh <- ObjectVersion{
Key: obj,
}
}
}()
return c.RemoveObjectsWithVersions(ctx, bucketName, objectsVersionsCh, opts)
}

// RemoveIncompleteUpload aborts an partially uploaded object.
func (c Client) RemoveIncompleteUpload(ctx context.Context, bucketName, objectName string) error {
// Input validation.
Expand Down
108 changes: 14 additions & 94 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type Client struct {
type Options struct {
Creds *credentials.Credentials
Secure bool
Transport *http.Transport
Region string
BucketLookup BucketLookupType

Expand Down Expand Up @@ -129,43 +130,12 @@ const (
BucketLookupPath
)

// NewV2 - instantiate minio client with Amazon S3 signature version
// '2' compatibility.
func NewV2(endpoint string, accessKeyID, secretAccessKey string, secure bool) (*Client, error) {
clnt, err := privateNew(endpoint, Options{
Creds: credentials.NewStaticV2(accessKeyID, secretAccessKey, ""),
Secure: secure,
BucketLookup: BucketLookupAuto,
})
if err != nil {
return nil, err
}
clnt.overrideSignerType = credentials.SignatureV2
return clnt, nil
}

// NewV4 - instantiate minio client with Amazon S3 signature version
// '4' compatibility.
func NewV4(endpoint string, accessKeyID, secretAccessKey string, secure bool) (*Client, error) {
clnt, err := privateNew(endpoint, Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: secure,
BucketLookup: BucketLookupAuto,
})
if err != nil {
return nil, err
// New - instantiate minio client with options
func New(endpoint string, opts *Options) (*Client, error) {
if opts == nil {
return nil, errors.New("no options provided")
}
clnt.overrideSignerType = credentials.SignatureV4
return clnt, nil
}

// New - instantiate minio client, adds automatic verification of signature.
func New(endpoint, accessKeyID, secretAccessKey string, secure bool) (*Client, error) {
clnt, err := privateNew(endpoint, Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: secure,
BucketLookup: BucketLookupAuto,
})
clnt, err := privateNew(endpoint, opts)
if err != nil {
return nil, err
}
Expand All @@ -177,40 +147,8 @@ func New(endpoint, accessKeyID, secretAccessKey string, secure bool) (*Client, e
if s3utils.IsAmazonEndpoint(*clnt.endpointURL) {
clnt.overrideSignerType = credentials.SignatureV4
}
return clnt, nil
}

// NewWithCredentials - instantiate minio client with credentials provider
// for retrieving credentials from various credentials provider such as
// IAM, File, Env etc.
func NewWithCredentials(endpoint string, creds *credentials.Credentials, secure bool, region string) (*Client, error) {
return privateNew(endpoint, Options{
Creds: creds,
Secure: secure,
BucketLookup: BucketLookupAuto,
Region: region,
})
}

// NewWithRegion - instantiate minio client, with region configured. Unlike New(),
// NewWithRegion avoids bucket-location lookup operations and it is slightly faster.
// Use this function when if your application deals with single region.
func NewWithRegion(endpoint, accessKeyID, secretAccessKey string, secure bool, region string) (*Client, error) {
creds := credentials.NewStaticV4(accessKeyID, secretAccessKey, "")
return privateNew(endpoint, Options{
Creds: creds,
Secure: secure,
Region: region,
BucketLookup: BucketLookupAuto,
})
}

// NewWithOptions - instantiate minio client with options
func NewWithOptions(endpoint string, opts *Options) (*Client, error) {
if opts == nil {
return nil, errors.New("no options provided")
}
return privateNew(endpoint, *opts)
return clnt, nil
}

// EndpointURL returns the URL of the S3 endpoint.
Expand Down Expand Up @@ -302,7 +240,7 @@ func (c *Client) redirectHeaders(req *http.Request, via []*http.Request) error {
return nil
}

func privateNew(endpoint string, opts Options) (*Client, error) {
func privateNew(endpoint string, opts *Options) (*Client, error) {
// construct endpoint.
endpointURL, err := getEndpointURL(endpoint, opts.Secure)
if err != nil {
Expand All @@ -328,9 +266,12 @@ func privateNew(endpoint string, opts Options) (*Client, error) {
// Save endpoint URL, user agent for future uses.
clnt.endpointURL = endpointURL

transport, err := DefaultTransport(opts.Secure)
if err != nil {
return nil, err
transport := opts.Transport
if transport == nil {
transport, err = DefaultTransport(opts.Secure)
if err != nil {
return nil, err
}
}

// Instantiate http client and bucket location cache.
Expand Down Expand Up @@ -377,27 +318,6 @@ func (c *Client) SetAppInfo(appName string, appVersion string) {
}
}

// SetCustomTransport - set new custom transport.
func (c *Client) SetCustomTransport(customHTTPTransport http.RoundTripper) {
// 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 follow this example :-
//
// tr := &http.Transport{
// TLSClientConfig: &tls.Config{RootCAs: pool},
// DisableCompression: true,
// }
// api.SetCustomTransport(tr)
//
if c.httpClient != nil {
c.httpClient.Transport = customHTTPTransport
}
}

// TraceOn - enable HTTP tracing.
func (c *Client) TraceOn(outputStream io.Writer) {
// if outputStream is nil then default to os.Stdout.
Expand Down
5 changes: 4 additions & 1 deletion api_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ func TestMakeTargetURL(t *testing.T) {

for i, testCase := range testCases {
// Initialize a MinIO client
c, _ := New(testCase.addr, "foo", "bar", testCase.secure)
c, _ := New(testCase.addr, &Options{
Creds: credentials.NewStaticV4("foo", "bar", ""),
Secure: testCase.secure,
})
isVirtualHost := c.isVirtualHostStyleRequest(*c.endpointURL, testCase.bucketName)
u, err := c.makeTargetURL(testCase.bucketName, testCase.objectName, testCase.bucketLocation, isVirtualHost, testCase.queryValues)
// Check the returned error
Expand Down
6 changes: 4 additions & 2 deletions bucket-cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,10 @@ func TestGetBucketLocationRequest(t *testing.T) {
client := &Client{}
var err error
if testCase.info.endPoint != "" {

client, err = New(testCase.info.endPoint, testCase.info.accessKey, testCase.info.secretKey, testCase.info.enableInsecure)
client, err = New(testCase.info.endPoint, &Options{
Creds: credentials.NewStaticV4(testCase.info.accessKey, testCase.info.secretKey, ""),
Secure: testCase.info.enableInsecure,
})
if err != nil {
t.Fatalf("Test %d: Failed to create new Client: %s", i+1, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type Core struct {
// NewCore - Returns new initialized a Core client, this CoreClient should be
// only used under special conditions such as need to access lower primitives
// and being able to use them to write your own wrappers.
func NewCore(endpoint string, accessKeyID, secretAccessKey string, secure bool) (*Core, error) {
func NewCore(endpoint string, opts *Options) (*Core, error) {
var s3Client Core
client, err := NewV4(endpoint, accessKeyID, secretAccessKey, secure)
client, err := New(endpoint, opts)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 36db8b2

Please sign in to comment.