mirror of
https://github.com/octoleo/restic.git
synced 2024-11-16 10:05:25 +00:00
s3: Prevent closing of the reader for GCS
This commit is contained in:
parent
9053b2000b
commit
51877cecf7
@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"restic"
|
"restic"
|
||||||
@ -14,6 +16,7 @@ import (
|
|||||||
"restic/errors"
|
"restic/errors"
|
||||||
|
|
||||||
"github.com/minio/minio-go"
|
"github.com/minio/minio-go"
|
||||||
|
"github.com/minio/minio-go/pkg/s3utils"
|
||||||
|
|
||||||
"restic/debug"
|
"restic/debug"
|
||||||
)
|
)
|
||||||
@ -22,6 +25,7 @@ import (
|
|||||||
type Backend struct {
|
type Backend struct {
|
||||||
client *minio.Client
|
client *minio.Client
|
||||||
sem *backend.Semaphore
|
sem *backend.Semaphore
|
||||||
|
cfg Config
|
||||||
bucketname string
|
bucketname string
|
||||||
prefix string
|
prefix string
|
||||||
backend.Layout
|
backend.Layout
|
||||||
@ -50,6 +54,7 @@ func Open(cfg Config) (restic.Backend, error) {
|
|||||||
be := &Backend{
|
be := &Backend{
|
||||||
client: client,
|
client: client,
|
||||||
sem: sem,
|
sem: sem,
|
||||||
|
cfg: cfg,
|
||||||
bucketname: cfg.Bucket,
|
bucketname: cfg.Bucket,
|
||||||
prefix: cfg.Prefix,
|
prefix: cfg.Prefix,
|
||||||
}
|
}
|
||||||
@ -157,6 +162,19 @@ func (be *Backend) Path() string {
|
|||||||
return be.prefix
|
return be.prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (be *Backend) isGoogleCloudStorage() bool {
|
||||||
|
scheme := "https://"
|
||||||
|
if be.cfg.UseHTTP {
|
||||||
|
scheme = "http://"
|
||||||
|
}
|
||||||
|
url, err := url.Parse(scheme + be.cfg.Endpoint)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s3utils.IsGoogleEndpoint(*url)
|
||||||
|
}
|
||||||
|
|
||||||
// Save stores data in the backend at the handle.
|
// Save stores data in the backend at the handle.
|
||||||
func (be *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) {
|
func (be *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) {
|
||||||
debug.Log("Save %v", h)
|
debug.Log("Save %v", h)
|
||||||
@ -174,6 +192,11 @@ func (be *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err
|
|||||||
return errors.New("key already exists")
|
return errors.New("key already exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prevent GCS from closing the file
|
||||||
|
if be.isGoogleCloudStorage() {
|
||||||
|
rd = ioutil.NopCloser(rd)
|
||||||
|
}
|
||||||
|
|
||||||
be.sem.GetToken()
|
be.sem.GetToken()
|
||||||
debug.Log("PutObject(%v, %v)", be.bucketname, objName)
|
debug.Log("PutObject(%v, %v)", be.bucketname, objName)
|
||||||
n, err := be.client.PutObject(be.bucketname, objName, rd, "application/octet-stream")
|
n, err := be.client.PutObject(be.bucketname, objName, rd, "application/octet-stream")
|
||||||
|
Loading…
Reference in New Issue
Block a user