s3 put writer now blocks on close
This commit is contained in:
+4
-2
@@ -72,8 +72,10 @@ func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
var auth s3.Auth
|
||||
auth.AccessKeyID = hostCfg.Auth.AccessKeyID
|
||||
auth.SecretAccessKey = hostCfg.Auth.SecretAccessKey
|
||||
if hostCfg.Auth != nil {
|
||||
auth.AccessKeyID = hostCfg.Auth.AccessKeyID
|
||||
auth.SecretAccessKey = hostCfg.Auth.SecretAccessKey
|
||||
}
|
||||
clnt = s3.GetNewClient(urlStr, &auth, mcUserAgent, debug)
|
||||
return clnt, nil
|
||||
case urlFile:
|
||||
|
||||
+10
-8
@@ -33,7 +33,7 @@ type auth struct {
|
||||
}
|
||||
|
||||
type hostConfig struct {
|
||||
Auth auth
|
||||
Auth *auth
|
||||
}
|
||||
|
||||
type mcConfig struct {
|
||||
@@ -131,10 +131,10 @@ func checkMcConfig(config *mcConfig) (err error) {
|
||||
if host == "" {
|
||||
return iodine.New(fmt.Errorf("Empty host URL"), nil)
|
||||
}
|
||||
if hostCfg.Auth.AccessKeyID == "" {
|
||||
if hostCfg.Auth != nil && hostCfg.Auth.AccessKeyID == "" {
|
||||
return iodine.New(fmt.Errorf("AccessKeyID is empty for Host [%s]", host), nil)
|
||||
}
|
||||
if hostCfg.Auth.SecretAccessKey == "" {
|
||||
if hostCfg.Auth != nil && hostCfg.Auth.SecretAccessKey == "" {
|
||||
return iodine.New(fmt.Errorf("SecretAccessKey is empty for Host [%s]", host), nil)
|
||||
}
|
||||
}
|
||||
@@ -243,12 +243,12 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
|
||||
Version: currentConfigVersion,
|
||||
Hosts: map[string]hostConfig{
|
||||
"http*://s3*.amazonaws.com": {
|
||||
Auth: auth{
|
||||
Auth: &auth{
|
||||
AccessKeyID: accessKeyID,
|
||||
SecretAccessKey: secretAccesskey,
|
||||
}},
|
||||
"http*://localhost:*": {
|
||||
Auth: auth{
|
||||
Auth: &auth{
|
||||
AccessKeyID: accessKeyID,
|
||||
SecretAccessKey: secretAccesskey,
|
||||
}},
|
||||
@@ -272,7 +272,7 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
|
||||
Version: currentConfigVersion,
|
||||
Hosts: map[string]hostConfig{
|
||||
"http*://s3*.amazonaws.com": {
|
||||
Auth: auth{
|
||||
Auth: &auth{
|
||||
AccessKeyID: accessKeyID,
|
||||
SecretAccessKey: secretAccesskey,
|
||||
}},
|
||||
@@ -317,8 +317,10 @@ func getHostConfig(requestURL string) (*hostConfig, error) {
|
||||
}
|
||||
if match {
|
||||
var hostCfg hostConfig
|
||||
hostCfg.Auth.AccessKeyID = cfg.Auth.AccessKeyID
|
||||
hostCfg.Auth.SecretAccessKey = cfg.Auth.SecretAccessKey
|
||||
if hostCfg.Auth != nil {
|
||||
hostCfg.Auth.AccessKeyID = cfg.Auth.AccessKeyID
|
||||
hostCfg.Auth.SecretAccessKey = cfg.Auth.SecretAccessKey
|
||||
}
|
||||
return &hostCfg, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +104,7 @@ func doCopyCmd(ctx *cli.Context) {
|
||||
writers = append(writers, writer)
|
||||
}
|
||||
bar := startBar(length)
|
||||
bar.Start()
|
||||
writers = append(writers, bar)
|
||||
|
||||
// write progress bar
|
||||
@@ -111,6 +112,16 @@ func doCopyCmd(ctx *cli.Context) {
|
||||
|
||||
// copy data to writers
|
||||
_, err = io.CopyN(multiWriter, reader, length)
|
||||
|
||||
// close writers
|
||||
for _, writer := range writeClosers {
|
||||
err := writer.Close()
|
||||
if err != nil {
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
}
|
||||
}
|
||||
|
||||
// time.Sleep(5 * time.Second)
|
||||
if err != nil {
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalln("Unable to write to target")
|
||||
|
||||
+48
-4
@@ -51,6 +51,7 @@ import (
|
||||
|
||||
"github.com/minio-io/mc/pkg/client"
|
||||
"github.com/minio-io/minio/pkg/iodine"
|
||||
"sync"
|
||||
)
|
||||
|
||||
/// Object API operations
|
||||
@@ -58,6 +59,7 @@ import (
|
||||
// Put - upload new object to bucket
|
||||
func (c *s3Client) Put(bucket, key, md5HexString string, size int64) (io.WriteCloser, error) {
|
||||
r, w := io.Pipe()
|
||||
blockingWriter := NewBlockingWriteCloser(w)
|
||||
go func() {
|
||||
req := newReq(c.keyURL(bucket, key), c.UserAgent, r)
|
||||
req.Method = "PUT"
|
||||
@@ -67,7 +69,10 @@ func (c *s3Client) Put(bucket, key, md5HexString string, size int64) (io.WriteCl
|
||||
if strings.TrimSpace(md5HexString) != "" {
|
||||
md5, err := hex.DecodeString(md5HexString)
|
||||
if err != nil {
|
||||
r.CloseWithError(iodine.New(err, nil))
|
||||
err := iodine.New(err, nil)
|
||||
r.CloseWithError(err)
|
||||
blockingWriter.Release(err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-MD5", base64.StdEncoding.EncodeToString(md5))
|
||||
}
|
||||
@@ -77,17 +82,56 @@ func (c *s3Client) Put(bucket, key, md5HexString string, size int64) (io.WriteCl
|
||||
res, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
r.CloseWithError(iodine.New(err, nil))
|
||||
err := iodine.New(err, nil)
|
||||
r.CloseWithError(err)
|
||||
blockingWriter.Release(err)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
r.CloseWithError(iodine.New(err, nil))
|
||||
err := iodine.New(err, nil)
|
||||
r.CloseWithError(err)
|
||||
blockingWriter.Release(err)
|
||||
return
|
||||
}
|
||||
blockingWriter.Release(nil)
|
||||
r.Close()
|
||||
}()
|
||||
return w, nil
|
||||
return blockingWriter, nil
|
||||
}
|
||||
|
||||
type blockingWriteCloser struct {
|
||||
w io.WriteCloser
|
||||
release *sync.WaitGroup
|
||||
err error
|
||||
}
|
||||
|
||||
func (b *blockingWriteCloser) Write(p []byte) (int, error) {
|
||||
n, err := b.w.Write(p)
|
||||
err = iodine.New(err, nil)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (b *blockingWriteCloser) Close() error {
|
||||
err := b.w.Close()
|
||||
if err != nil {
|
||||
b.err = err
|
||||
}
|
||||
b.release.Wait()
|
||||
return b.err
|
||||
}
|
||||
|
||||
func (b *blockingWriteCloser) Release(err error) {
|
||||
b.release.Done()
|
||||
if err != nil {
|
||||
b.err = err
|
||||
}
|
||||
}
|
||||
|
||||
func NewBlockingWriteCloser(w io.WriteCloser) *blockingWriteCloser {
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
return &blockingWriteCloser{w: w, release: wg}
|
||||
}
|
||||
|
||||
// Stat - returns 0, "", os.ErrNotExist if not on S3
|
||||
|
||||
Reference in New Issue
Block a user