Merge pull request #1046 from restic/s3-split-open

s3: Split Create() from Open()
This commit is contained in:
Alexander Neumann 2017-06-19 19:52:40 +02:00
commit 8760de42fe
3 changed files with 21 additions and 10 deletions

View file

@ -471,7 +471,7 @@ func create(s string, opts options.Options) (restic.Backend, error) {
case "sftp":
return sftp.Create(cfg.(sftp.Config))
case "s3":
return s3.Open(cfg.(s3.Config))
return s3.Create(cfg.(s3.Config))
case "swift":
return swift.Open(cfg.(swift.Config))
case "b2":

View file

@ -31,9 +31,7 @@ var _ restic.Backend = &Backend{}
const defaultLayout = "default"
// Open opens the S3 backend at bucket and region. The bucket is created if it
// does not exist yet.
func Open(cfg Config) (restic.Backend, error) {
func open(cfg Config) (*Backend, error) {
debug.Log("open, config %#v", cfg)
if cfg.MaxRetries > 0 {
@ -65,7 +63,20 @@ func Open(cfg Config) (restic.Backend, error) {
be.Layout = l
found, err := client.BucketExists(cfg.Bucket)
return be, nil
}
// Open opens the S3 backend at bucket and region. The bucket is created if it
// does not exist yet.
func Open(cfg Config) (restic.Backend, error) {
return open(cfg)
}
// Create opens the S3 backend at bucket and region and creates the bucket if
// it does not exist yet.
func Create(cfg Config) (restic.Backend, error) {
be, err := open(cfg)
found, err := be.client.BucketExists(cfg.Bucket)
if err != nil {
debug.Log("BucketExists(%v) returned err %v", cfg.Bucket, err)
return nil, errors.Wrap(err, "client.BucketExists")
@ -73,7 +84,7 @@ func Open(cfg Config) (restic.Backend, error) {
if !found {
// create new bucket with default ACL in default region
err = client.MakeBucket(cfg.Bucket, "")
err = be.client.MakeBucket(cfg.Bucket, "")
if err != nil {
return nil, errors.Wrap(err, "client.MakeBucket")
}

View file

@ -103,9 +103,9 @@ type MinioTestConfig struct {
stopServer func()
}
func openS3(t testing.TB, cfg MinioTestConfig) (be restic.Backend, err error) {
func createS3(t testing.TB, cfg MinioTestConfig) (be restic.Backend, err error) {
for i := 0; i < 10; i++ {
be, err = s3.Open(cfg.Config)
be, err = s3.Create(cfg.Config)
if err != nil {
t.Logf("s3 open: try %d: error %v", i, err)
time.Sleep(500 * time.Millisecond)
@ -142,7 +142,7 @@ func newMinioTestSuite(ctx context.Context, t testing.TB) *test.Suite {
Create: func(config interface{}) (restic.Backend, error) {
cfg := config.(MinioTestConfig)
be, err := openS3(t, cfg)
be, err := createS3(t, cfg)
if err != nil {
return nil, err
}
@ -236,7 +236,7 @@ func newS3TestSuite(t testing.TB) *test.Suite {
Create: func(config interface{}) (restic.Backend, error) {
cfg := config.(s3.Config)
be, err := s3.Open(cfg)
be, err := s3.Create(cfg)
if err != nil {
return nil, err
}