| 1 | import type * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 2 | import type * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import type { RuntimeContext } from "../../RuntimeContext.ts"; |
| 5 | import type { Bucket } from "./Bucket.ts"; |
| 6 | import type { |
| 7 | ObjectNotFound, |
| 8 | ObjectRequestFailed, |
| 9 | PutObjectContent, |
| 10 | } from "./ObjectMedia.ts"; |
| 11 | |
| 12 | /** Write-only client for one Cloud Storage bucket. */ |
| 13 | export interface WriteBucketClient { |
| 14 | /** Upload (or overwrite) an object. `ObjectNotFound` means the bucket is gone. */ |
| 15 | put( |
| 16 | name: string, |
| 17 | body: string | Uint8Array, |
| 18 | options?: Omit<PutObjectContent, "name" | "body">, |
| 19 | ): Effect.Effect< |
| 20 | storage.Storage_Object, |
| 21 | ObjectNotFound | ObjectRequestFailed, |
| 22 | RuntimeContext |
| 23 | >; |
| 24 | /** Delete an object. Deleting a missing object succeeds. */ |
| 25 | delete( |
| 26 | object: string, |
| 27 | ): Effect.Effect<void, storage.DeleteObjectsError, RuntimeContext>; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Write access to a Cloud Storage {@link Bucket}: `put`, `delete`. |
| 32 | * Grants `roles/storage.objectUser` on the bucket only (overwriting needs |
| 33 | * delete, which `objectCreator` lacks). |
| 34 | * |
| 35 | * ### Writing to a bucket |
| 36 | * **Example:** Upload and delete |
| 37 | * ```typescript |
| 38 | * const uploads = yield* GCP.Storage.WriteBucket(bucket); |
| 39 | * yield* uploads.put("hello.txt", "Hello!", { contentType: "text/plain" }); |
| 40 | * yield* uploads.delete("old.txt"); |
| 41 | * // …provided with Effect.provide(GCP.Storage.WriteBucketHttp) |
| 42 | * ``` |
| 43 | * |
| 44 | * @binding |
| 45 | * @category Storage |
| 46 | */ |
| 47 | export interface WriteBucket extends Binding.Service< |
| 48 | WriteBucket, |
| 49 | "GCP.Storage.WriteBucket", |
| 50 | (bucket: Bucket) => Effect.Effect<WriteBucketClient> |
| 51 | > {} |
| 52 | |
| 53 | export const WriteBucket = Binding.Service<WriteBucket>( |
| 54 | "GCP.Storage.WriteBucket", |
| 55 | ); |