예제 #1
0
파일: s3.go 프로젝트: koolhead17/mc
// New returns an initialized s3Client structure. if debug use a internal trace transport
func New(config *client.Config) (client.Client, *probe.Error) {
	u := client.NewURL(config.HostURL)
	transport := http.DefaultTransport
	if config.Debug == true {
		if config.Signature == "S3v4" {
			transport = httptracer.GetNewTraceTransport(NewTraceV4(), http.DefaultTransport)
		}
		if config.Signature == "S3v2" {
			transport = httptracer.GetNewTraceTransport(NewTraceV2(), http.DefaultTransport)
		}
	}
	s3Conf := minio.Config{
		AccessKeyID:     config.AccessKeyID,
		SecretAccessKey: config.SecretAccessKey,
		Transport:       transport,
		Endpoint:        u.Scheme + u.SchemeSeparator + u.Host,
		Signature: func() minio.SignatureType {
			if config.Signature == "S3v2" {
				return minio.SignatureV2
			}
			return minio.SignatureV4
		}(),
	}
	s3Conf.SetUserAgent(config.AppName, config.AppVersion, config.AppComments...)
	api, err := minio.New(s3Conf)
	if err != nil {
		return nil, probe.NewError(err)
	}
	s3Clnt := &s3Client{
		mu:           new(sync.Mutex),
		api:          api,
		hostURL:      u,
		virtualStyle: isVirtualHostStyle(u.Host),
	}
	return s3Clnt, nil
}
예제 #2
0
파일: s3v2.go 프로젝트: dudymas/mc
// New returns an initialized s3Client structure. if debug use a internal trace transport
func New(config *client.Config) (client.Client, *probe.Error) {
	u := client.NewURL(config.HostURL)
	transport := http.DefaultTransport
	if config.Debug == true {
		transport = httptracer.GetNewTraceTransport(NewTrace(), http.DefaultTransport)
	}
	s3Conf := minio.Config{
		AccessKeyID:     config.AccessKeyID,
		SecretAccessKey: config.SecretAccessKey,
		Transport:       transport,
		Endpoint:        u.Scheme + u.SchemeSeparator + u.Host,
	}
	s3Conf.AccessKeyID = config.AccessKeyID
	s3Conf.SecretAccessKey = config.SecretAccessKey
	s3Conf.Transport = transport
	s3Conf.SetUserAgent(config.AppName, config.AppVersion, config.AppComments...)
	s3Conf.Endpoint = u.Scheme + u.SchemeSeparator + u.Host
	api, err := minio.New(s3Conf)
	if err != nil {
		return nil, probe.NewError(err)
	}
	return &s3Client{api: api, hostURL: u}, nil
}
예제 #3
0
파일: s3.go 프로젝트: fwessels/mc
// newFactory encloses New function with client cache.
func newFactory() func(config *client.Config) (client.Client, *probe.Error) {
	clientCache := make(map[uint32]minio.CloudStorageAPI)
	mutex := &sync.Mutex{}

	// Return New function.
	return func(config *client.Config) (client.Client, *probe.Error) {
		u := client.NewURL(config.HostURL)
		transport := http.DefaultTransport
		if config.Debug == true {
			if config.Signature == "S3v4" {
				transport = httptracer.GetNewTraceTransport(NewTraceV4(), http.DefaultTransport)
			}
			if config.Signature == "S3v2" {
				transport = httptracer.GetNewTraceTransport(NewTraceV2(), http.DefaultTransport)
			}
		}

		// New S3 configuration.
		s3Conf := minio.Config{
			AccessKeyID:     config.AccessKey,
			SecretAccessKey: config.SecretKey,
			Transport:       transport,
			Endpoint:        u.Scheme + u.SchemeSeparator + u.Host,
			Signature: func() minio.SignatureType {
				if config.Signature == "S3v2" {
					return minio.SignatureV2
				}
				return minio.SignatureV4
			}(),
		}

		s3Conf.SetUserAgent(config.AppName, config.AppVersion, config.AppComments...)

		// Generate a hash out of s3Conf.
		confHash := fnv.New32a()
		confHash.Write([]byte(s3Conf.Endpoint + s3Conf.AccessKeyID + s3Conf.SecretAccessKey))
		confSum := confHash.Sum32()

		// Lookup previous cache by hash.
		mutex.Lock()
		defer mutex.Unlock()
		var api minio.CloudStorageAPI
		found := false
		if api, found = clientCache[confSum]; !found {
			// Not found. Instantiate a new minio client.
			var e error
			api, e = minio.New(s3Conf)
			if e != nil {
				return nil, probe.NewError(e)
			}
			// Cache the new minio client with hash of config as key.
			clientCache[confSum] = api
		}

		s3Clnt := &s3Client{
			mu:           new(sync.Mutex),
			api:          api,
			hostURL:      u,
			virtualStyle: isVirtualHostStyle(u.Host),
		}
		return s3Clnt, nil
	}
}
예제 #4
0
// newFactory encloses New function with client cache.
func newFactory() func(config *Config) (Client, *probe.Error) {
	clientCache := make(map[uint32]*minio.Client)
	mutex := &sync.Mutex{}

	// Return New function.
	return func(config *Config) (Client, *probe.Error) {
		// Creates a parsed URL.
		targetURL := newClientURL(config.HostURL)
		// By default enable HTTPs.
		useTLS := true
		if targetURL.Scheme == "http" {
			useTLS = false
		}

		// Instantiate s3
		s3Clnt := &s3Client{}
		// Allocate a new mutex.
		s3Clnt.mutex = new(sync.Mutex)
		// Save the target URL.
		s3Clnt.targetURL = targetURL

		// Save if target supports virtual host style.
		hostName := targetURL.Host
		s3Clnt.virtualStyle = isVirtualHostStyle(hostName)

		if s3Clnt.virtualStyle {
			// If Amazon URL replace it with 's3.amazonaws.com'
			if isAmazon(hostName) {
				hostName = amazonHostName
			}
			// If Google URL replace it with 'storage.googleapis.com'
			if isGoogle(hostName) {
				hostName = googleHostName
			}
		}

		// Generate a hash out of s3Conf.
		confHash := fnv.New32a()
		confHash.Write([]byte(hostName + config.AccessKey + config.SecretKey))
		confSum := confHash.Sum32()

		// Lookup previous cache by hash.
		mutex.Lock()
		defer mutex.Unlock()
		var api *minio.Client
		var found bool
		if api, found = clientCache[confSum]; !found {
			// Not found. Instantiate a new minio
			var e error
			if strings.ToUpper(config.Signature) == "S3V2" {
				// if Signature version '2' use NewV2 directly.
				api, e = minio.NewV2(hostName, config.AccessKey, config.SecretKey, useTLS)
			} else {
				// if Signature version '4' use NewV4 directly.
				api, e = minio.NewV4(hostName, config.AccessKey, config.SecretKey, useTLS)
			}
			if e != nil {
				return nil, probe.NewError(e)
			}

			transport := http.DefaultTransport

			if useTLS {
				tlsConfig := &tls.Config{RootCAs: globalRootCAs}
				if config.Insecure {
					tlsConfig.InsecureSkipVerify = true
				}
				transport = &http.Transport{
					TLSClientConfig: tlsConfig,
				}
			}

			if config.Debug {
				if strings.EqualFold(config.Signature, "S3v4") {
					transport = httptracer.GetNewTraceTransport(newTraceV4(), transport)
				} else if strings.EqualFold(config.Signature, "S3v2") {
					transport = httptracer.GetNewTraceTransport(newTraceV2(), transport)
				}
				// Set custom transport.
			}
			api.SetCustomTransport(transport)
			// Cache the new minio client with hash of config as key.
			clientCache[confSum] = api
		}
		// Set app info.
		api.SetAppInfo(config.AppName, config.AppVersion)

		// Store the new api object.
		s3Clnt.api = api

		return s3Clnt, nil
	}
}