forked from TrueCloudLab/restic
Merge pull request #1046 from restic/s3-split-open
s3: Split Create() from Open()
This commit is contained in:
commit
8760de42fe
3 changed files with 21 additions and 10 deletions
|
@ -471,7 +471,7 @@ func create(s string, opts options.Options) (restic.Backend, error) {
|
||||||
case "sftp":
|
case "sftp":
|
||||||
return sftp.Create(cfg.(sftp.Config))
|
return sftp.Create(cfg.(sftp.Config))
|
||||||
case "s3":
|
case "s3":
|
||||||
return s3.Open(cfg.(s3.Config))
|
return s3.Create(cfg.(s3.Config))
|
||||||
case "swift":
|
case "swift":
|
||||||
return swift.Open(cfg.(swift.Config))
|
return swift.Open(cfg.(swift.Config))
|
||||||
case "b2":
|
case "b2":
|
||||||
|
|
|
@ -31,9 +31,7 @@ var _ restic.Backend = &Backend{}
|
||||||
|
|
||||||
const defaultLayout = "default"
|
const defaultLayout = "default"
|
||||||
|
|
||||||
// Open opens the S3 backend at bucket and region. The bucket is created if it
|
func open(cfg Config) (*Backend, error) {
|
||||||
// does not exist yet.
|
|
||||||
func Open(cfg Config) (restic.Backend, error) {
|
|
||||||
debug.Log("open, config %#v", cfg)
|
debug.Log("open, config %#v", cfg)
|
||||||
|
|
||||||
if cfg.MaxRetries > 0 {
|
if cfg.MaxRetries > 0 {
|
||||||
|
@ -65,7 +63,20 @@ func Open(cfg Config) (restic.Backend, error) {
|
||||||
|
|
||||||
be.Layout = l
|
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 {
|
if err != nil {
|
||||||
debug.Log("BucketExists(%v) returned err %v", cfg.Bucket, err)
|
debug.Log("BucketExists(%v) returned err %v", cfg.Bucket, err)
|
||||||
return nil, errors.Wrap(err, "client.BucketExists")
|
return nil, errors.Wrap(err, "client.BucketExists")
|
||||||
|
@ -73,7 +84,7 @@ func Open(cfg Config) (restic.Backend, error) {
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
// create new bucket with default ACL in default region
|
// create new bucket with default ACL in default region
|
||||||
err = client.MakeBucket(cfg.Bucket, "")
|
err = be.client.MakeBucket(cfg.Bucket, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "client.MakeBucket")
|
return nil, errors.Wrap(err, "client.MakeBucket")
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,9 +103,9 @@ type MinioTestConfig struct {
|
||||||
stopServer func()
|
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++ {
|
for i := 0; i < 10; i++ {
|
||||||
be, err = s3.Open(cfg.Config)
|
be, err = s3.Create(cfg.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf("s3 open: try %d: error %v", i, err)
|
t.Logf("s3 open: try %d: error %v", i, err)
|
||||||
time.Sleep(500 * time.Millisecond)
|
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) {
|
Create: func(config interface{}) (restic.Backend, error) {
|
||||||
cfg := config.(MinioTestConfig)
|
cfg := config.(MinioTestConfig)
|
||||||
|
|
||||||
be, err := openS3(t, cfg)
|
be, err := createS3(t, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,7 @@ func newS3TestSuite(t testing.TB) *test.Suite {
|
||||||
Create: func(config interface{}) (restic.Backend, error) {
|
Create: func(config interface{}) (restic.Backend, error) {
|
||||||
cfg := config.(s3.Config)
|
cfg := config.(s3.Config)
|
||||||
|
|
||||||
be, err := s3.Open(cfg)
|
be, err := s3.Create(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue