github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/receiver/gcs.go (about) 1 // Copyright 2018 The WPT Dashboard Project. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package receiver 6 7 import ( 8 "context" 9 "io" 10 11 "cloud.google.com/go/storage" 12 ) 13 14 // nolint:godox // TODO: This interface might also be useful to other APIs. 15 16 type gcs interface { 17 NewWriter(bucketName, fileName, contentType, contentEncoding string) (io.WriteCloser, error) 18 } 19 20 type gcsImpl struct { 21 ctx context.Context // nolint:containedctx // TODO: Fix containedctx lint error 22 client *storage.Client 23 } 24 25 func (g *gcsImpl) NewWriter(bucketName, fileName, contentType, contentEncoding string) (io.WriteCloser, error) { 26 if g.client == nil { 27 var err error 28 g.client, err = storage.NewClient(g.ctx) 29 if err != nil { 30 return nil, err 31 } 32 } 33 bucket := g.client.Bucket(bucketName) 34 w := bucket.Object(fileName).NewWriter(g.ctx) 35 if contentType != "" { 36 w.ContentType = contentType 37 } 38 if contentEncoding != "" { 39 w.ContentEncoding = contentEncoding 40 } 41 42 return w, nil 43 }