forked from TrueCloudLab/restic
Add debug, do not create bucket if it already exists
This commit is contained in:
parent
69e6e9e5c7
commit
314182e7e0
1 changed files with 14 additions and 12 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/minio/minio-go"
|
"github.com/minio/minio-go"
|
||||||
|
@ -43,21 +44,17 @@ func Open(cfg Config) (backend.Backend, error) {
|
||||||
be := &S3Backend{client: client, bucketname: cfg.Bucket}
|
be := &S3Backend{client: client, bucketname: cfg.Bucket}
|
||||||
be.createConnections()
|
be.createConnections()
|
||||||
|
|
||||||
// create new bucket with default ACL in default region
|
if err := client.BucketExists(cfg.Bucket); err != nil {
|
||||||
err = client.MakeBucket(cfg.Bucket, "", "")
|
debug.Log("s3.Open", "BucketExists(%v) returned err %v, trying to create the bucket", err)
|
||||||
|
|
||||||
if err != nil {
|
// create new bucket with default ACL in default region
|
||||||
e, ok := err.(minio.ErrorResponse)
|
err = client.MakeBucket(cfg.Bucket, "", "")
|
||||||
if ok && e.Code == "BucketAlreadyExists" {
|
|
||||||
debug.Log("s3.Open", "ignoring error that bucket %q already exists", cfg.Bucket)
|
if err != nil {
|
||||||
err = nil
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return be, nil
|
return be, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +100,7 @@ func (bb *s3Blob) Size() uint {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bb *s3Blob) Finalize(t backend.Type, name string) error {
|
func (bb *s3Blob) Finalize(t backend.Type, name string) error {
|
||||||
|
debug.Log("s3.blob.Finalize()", "bucket %v, finalize %v, %d bytes", bb.b.bucketname, name, bb.buf.Len())
|
||||||
if bb.final {
|
if bb.final {
|
||||||
return errors.New("Already finalized")
|
return errors.New("Already finalized")
|
||||||
}
|
}
|
||||||
|
@ -114,16 +112,19 @@ func (bb *s3Blob) Finalize(t backend.Type, name string) error {
|
||||||
// Check key does not already exist
|
// Check key does not already exist
|
||||||
_, err := bb.b.client.StatObject(bb.b.bucketname, path)
|
_, err := bb.b.client.StatObject(bb.b.bucketname, path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
debug.Log("s3.blob.Finalize()", "%v already exists", name)
|
||||||
return errors.New("key already exists")
|
return errors.New("key already exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedBytes := bb.buf.Len()
|
expectedBytes := bb.buf.Len()
|
||||||
|
|
||||||
<-bb.b.connChan
|
<-bb.b.connChan
|
||||||
|
debug.Log("s3.Finalize", "PutObject(%v, %v, %v, %v)",
|
||||||
|
bb.b.bucketname, path, int64(bb.buf.Len()), "binary/octet-stream")
|
||||||
n, err := bb.b.client.PutObject(bb.b.bucketname, path, bb.buf, int64(bb.buf.Len()), "binary/octet-stream")
|
n, err := bb.b.client.PutObject(bb.b.bucketname, path, bb.buf, int64(bb.buf.Len()), "binary/octet-stream")
|
||||||
|
debug.Log("s3.Finalize", "finalized %v -> n %v, err %#v", path, n, err)
|
||||||
bb.b.connChan <- struct{}{}
|
bb.b.connChan <- struct{}{}
|
||||||
|
|
||||||
debug.Log("s3.Finalize", "finalized %v -> n %v, err %v", path, n, err)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -216,6 +217,7 @@ func (be *S3Backend) Remove(t backend.Type, name string) error {
|
||||||
// goroutine is started for this. If the channel done is closed, sending
|
// goroutine is started for this. If the channel done is closed, sending
|
||||||
// stops.
|
// stops.
|
||||||
func (be *S3Backend) List(t backend.Type, done <-chan struct{}) <-chan string {
|
func (be *S3Backend) List(t backend.Type, done <-chan struct{}) <-chan string {
|
||||||
|
debug.Log("s3.List", "listing %v", t)
|
||||||
ch := make(chan string)
|
ch := make(chan string)
|
||||||
|
|
||||||
prefix := s3path(t, "")
|
prefix := s3path(t, "")
|
||||||
|
|
Loading…
Reference in a new issue