diff --git a/gcsstore/gcsservice.go b/gcsstore/gcsservice.go index b9b8c87..7d3b8d5 100644 --- a/gcsstore/gcsservice.go +++ b/gcsstore/gcsservice.go @@ -10,6 +10,7 @@ import ( "cloud.google.com/go/storage" "golang.org/x/net/context" + "google.golang.org/api/googleapi" "google.golang.org/api/iterator" "google.golang.org/api/option" @@ -290,13 +291,19 @@ func (service *GCSService) WriteObject(ctx context.Context, params GCSObjectPara w := obj.NewWriter(ctx) - defer w.Close() - n, err := io.Copy(w, r) if err != nil { return 0, err } + err = w.Close() + if err != nil { + if gErr, ok := err.(*googleapi.Error); ok && gErr.Code == 404 { + return 0, fmt.Errorf("gcsstore: the bucket %s could not be found while trying to write an object", params.Bucket) + } + return 0, err + } + return n, err } diff --git a/gcsstore/gcsservice_test.go b/gcsstore/gcsservice_test.go index ad9573f..39388fe 100644 --- a/gcsstore/gcsservice_test.go +++ b/gcsstore/gcsservice_test.go @@ -360,6 +360,13 @@ func TestWriteObject(t *testing.T) { "expiry_date": "1425333671141", }) + gock.New("https://googleapis.com"). + Post("/upload/storage/v1/b/test-bucket/o"). + MatchParam("alt", "json"). + MatchParam("key", "foo"). + Reply(200). + JSON(map[string]string{}) + ctx := context.Background() client, err := storage.NewClient(ctx, option.WithAPIKey("foo")) if err != nil { @@ -379,7 +386,7 @@ func TestWriteObject(t *testing.T) { }, reader) if err != nil { - t.Errorf("Error deleting object: %+v", err) + t.Errorf("Error writing object: %+v", err) } if size != 1 {