NewPublishedStorage creates a GCS-backed published storage.
(bucket, prefix, credentialsFile, serviceAccountJSON, project, endpoint, defaultACL, storageClass, encryptionKey string, disableMultiDel, debug bool)
| 39 | |
| 40 | // NewPublishedStorage creates a GCS-backed published storage. |
| 41 | func NewPublishedStorage(bucket, prefix, credentialsFile, serviceAccountJSON, |
| 42 | project, endpoint, defaultACL, storageClass, encryptionKey string, |
| 43 | disableMultiDel, debug bool) (*PublishedStorage, error) { |
| 44 | |
| 45 | ctx := context.TODO() |
| 46 | opts := make([]option.ClientOption, 0, 4) |
| 47 | |
| 48 | if endpoint != "" { |
| 49 | opts = append(opts, option.WithEndpoint(endpoint), option.WithoutAuthentication()) |
| 50 | } else if credentialsFile != "" { |
| 51 | opts = append(opts, option.WithAuthCredentialsFile(option.ServiceAccount, credentialsFile)) |
| 52 | } else if serviceAccountJSON != "" { |
| 53 | opts = append(opts, option.WithAuthCredentialsJSON(option.ServiceAccount, []byte(serviceAccountJSON))) |
| 54 | } |
| 55 | |
| 56 | if project != "" { |
| 57 | opts = append(opts, option.WithQuotaProject(project)) |
| 58 | } |
| 59 | |
| 60 | // When pointing at a non-production endpoint (an explicit override or the |
| 61 | // STORAGE_EMULATOR_HOST hook the GCS Go client honours natively), force |
| 62 | // JSON reads. The default XML download API uses virtual-host-style URLs |
| 63 | // (https://<bucket>.storage.googleapis.com/...) that emulators and most |
| 64 | // dev/staging endpoints can't serve under a single listener; the JSON API |
| 65 | // uses path-based URLs that work the same against real GCS or an emulator. |
| 66 | if endpoint != "" || os.Getenv("STORAGE_EMULATOR_HOST") != "" { |
| 67 | opts = append(opts, storage.WithJSONReads()) |
| 68 | } |
| 69 | |
| 70 | client, err := storage.NewClient(ctx, opts...) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | |
| 75 | result := &PublishedStorage{ |
| 76 | client: client, |
| 77 | bucket: client.Bucket(bucket), |
| 78 | bucketName: bucket, |
| 79 | prefix: prefix, |
| 80 | acl: defaultACL, |
| 81 | storageClass: storageClass, |
| 82 | encryptionKey: encryptionKey, |
| 83 | disableMultiDel: disableMultiDel, |
| 84 | debug: debug, |
| 85 | } |
| 86 | |
| 87 | return result, nil |
| 88 | } |
| 89 | |
| 90 | func (g *PublishedStorage) String() string { |
| 91 | return fmt.Sprintf("GCS: %s/%s", g.bucketName, g.prefix) |
no outgoing calls