go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/gsutil/gsutil.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package gsutil contains utility functions for Google Storage. 16 package gsutil 17 18 import ( 19 "context" 20 "time" 21 22 "cloud.google.com/go/storage" 23 24 "go.chromium.org/luci/common/errors" 25 "go.chromium.org/luci/common/gcloud/gs" 26 ) 27 28 type Key string 29 30 // GenerateSignedURL generates object signed URL. 31 func GenerateSignedURL(ctx context.Context, gsClient *storage.Client, bucket, object string, 32 expiration time.Time, opts *storage.SignedURLOptions) (string, error) { 33 34 // For passing GoogleAcessId used in testing 35 if opts == nil { 36 // GoogleAccessID & privateKey don't need to be provided as they will be 37 // automatically detected in GCE. 38 // See: https://pkg.go.dev/cloud.google.com/go/storage#hdr-Credential_requirements_for_signing 39 opts = &storage.SignedURLOptions{ 40 Scheme: storage.SigningSchemeV4, 41 Method: "GET", 42 Expires: expiration, 43 } 44 } 45 46 // Use https://pkg.go.dev/cloud.google.com/go/storage#BucketHandle.SignedURL 47 // to generate the signed URL. 48 url, err := gsClient.Bucket(bucket).SignedURL(object, opts) 49 if err != nil { 50 return "", errors.Reason("GenerateSignedURL(%q/%q): %v", bucket, object, err).Err() 51 } 52 53 return url, nil 54 } 55 56 // Split returns the bucket and filename components of the Path. 57 func Split(path string) (bucket string, filename string) { 58 return gs.Path(path).Split() 59 }