1
0
mirror of https://github.com/minio/mc.git synced 2025-11-13 12:22:45 +03:00
Files
mc/common.go

84 lines
1.8 KiB
Go

/*
* Mini Object Storage, (C) 2014,2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"encoding/json"
"net/http"
"os"
"os/user"
"path"
"github.com/minio-io/mc/pkg/s3"
)
// Common authentication file
const (
auth = "auth.json"
mcDir = "/.minio/mc/"
)
func getAuthFilePath() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
return path.Join(u.HomeDir+mcDir, auth), nil
}
func getNewClient(auth *s3.Auth) (*s3.Client, error) {
return &s3.Client{
Auth: auth,
Transport: http.DefaultTransport,
}, nil
}
func getEnvironment() (auth *s3.Auth, err error) {
var s3Auth *os.File
var accessKey, secretKey, endpoint string
authFile, err := getAuthFilePath()
if err != nil {
return nil, err
}
s3Auth, err = os.Open(authFile)
defer s3Auth.Close()
if err != nil {
accessKey = os.Getenv("AWS_ACCESS_KEY_ID")
secretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
endpoint = os.Getenv("S3_ENDPOINT")
if endpoint == "" {
endpoint = "s3.amazonaws.com"
}
auth = &s3.Auth{
AccessKey: accessKey,
SecretAccessKey: secretKey,
Endpoint: endpoint,
}
} else {
var n int
s3Authbytes := make([]byte, 256)
n, err = s3Auth.Read(s3Authbytes)
err = json.Unmarshal(s3Authbytes[:n], &auth)
if err != nil {
return nil, err
}
}
return auth, nil
}