|
| 1 | +# @constructive-io/bucket-provisioner |
| 2 | + |
| 3 | +<p align="center" width="100%"> |
| 4 | + <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" /> |
| 5 | +</p> |
| 6 | + |
| 7 | +<p align="center" width="100%"> |
| 8 | + <a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml"> |
| 9 | + <img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" /> |
| 10 | + </a> |
| 11 | + <a href="https://github.com/constructive-io/constructive/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a> |
| 12 | + <a href="https://www.npmjs.com/package/@constructive-io/bucket-provisioner"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=packages%2Fbucket-provisioner%2Fpackage.json"/></a> |
| 13 | +</p> |
| 14 | + |
| 15 | +S3-compatible bucket provisioning library for the Constructive storage module. Creates and configures buckets with the correct privacy policies, CORS rules, versioning, and lifecycle settings for private, public, and temporary file storage. |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +- **Privacy enforcement** — Block All Public Access for private/temp buckets, public-read policy for public buckets |
| 20 | +- **CORS configuration** — Browser-compatible rules for presigned URL uploads |
| 21 | +- **Lifecycle rules** — Auto-cleanup for temp buckets (abandoned uploads) |
| 22 | +- **Versioning** — Optional S3 versioning for durability |
| 23 | +- **Multi-provider** — Works with AWS S3, MinIO, Cloudflare R2, Google Cloud Storage, and DigitalOcean Spaces |
| 24 | +- **Inspect/audit** — Read back a bucket's current configuration for verification |
| 25 | +- **Typed errors** — Structured `ProvisionerError` with error codes for programmatic handling |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +```bash |
| 30 | +pnpm add @constructive-io/bucket-provisioner |
| 31 | +``` |
| 32 | + |
| 33 | +## Quick Start |
| 34 | + |
| 35 | +```typescript |
| 36 | +import { BucketProvisioner } from '@constructive-io/bucket-provisioner'; |
| 37 | + |
| 38 | +const provisioner = new BucketProvisioner({ |
| 39 | + connection: { |
| 40 | + provider: 'minio', |
| 41 | + region: 'us-east-1', |
| 42 | + endpoint: 'http://minio:9000', |
| 43 | + accessKeyId: 'minioadmin', |
| 44 | + secretAccessKey: 'minioadmin', |
| 45 | + }, |
| 46 | + allowedOrigins: ['https://app.example.com'], |
| 47 | +}); |
| 48 | + |
| 49 | +// Provision a private bucket (presigned URLs only) |
| 50 | +const result = await provisioner.provision({ |
| 51 | + bucketName: 'my-app-private', |
| 52 | + accessType: 'private', |
| 53 | + versioning: true, |
| 54 | +}); |
| 55 | + |
| 56 | +console.log(result); |
| 57 | +// { |
| 58 | +// bucketName: 'my-app-private', |
| 59 | +// accessType: 'private', |
| 60 | +// blockPublicAccess: true, |
| 61 | +// versioning: true, |
| 62 | +// corsRules: [...], |
| 63 | +// lifecycleRules: [], |
| 64 | +// ... |
| 65 | +// } |
| 66 | +``` |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +### Provision a Public Bucket |
| 71 | + |
| 72 | +Public buckets serve files via direct URL or CDN. The provisioner applies a public-read bucket policy and configures CORS for browser uploads. |
| 73 | + |
| 74 | +```typescript |
| 75 | +const result = await provisioner.provision({ |
| 76 | + bucketName: 'my-app-public', |
| 77 | + accessType: 'public', |
| 78 | + publicUrlPrefix: 'https://cdn.example.com/public', |
| 79 | +}); |
| 80 | +// result.blockPublicAccess === false |
| 81 | +// result.publicUrlPrefix === 'https://cdn.example.com/public' |
| 82 | +``` |
| 83 | + |
| 84 | +### Provision a Temp Bucket |
| 85 | + |
| 86 | +Temp buckets are staging areas for uploads. They behave like private buckets but include a lifecycle rule to auto-delete objects after a configurable period. |
| 87 | + |
| 88 | +```typescript |
| 89 | +const result = await provisioner.provision({ |
| 90 | + bucketName: 'my-app-temp', |
| 91 | + accessType: 'temp', |
| 92 | +}); |
| 93 | +// result.lifecycleRules[0].id === 'temp-cleanup' |
| 94 | +// result.lifecycleRules[0].expirationDays === 1 |
| 95 | +``` |
| 96 | + |
| 97 | +### Inspect an Existing Bucket |
| 98 | + |
| 99 | +Read back a bucket's current configuration to verify it matches expectations. |
| 100 | + |
| 101 | +```typescript |
| 102 | +const config = await provisioner.inspect('my-app-private', 'private'); |
| 103 | +console.log(config.blockPublicAccess); // true |
| 104 | +console.log(config.versioning); // true |
| 105 | +console.log(config.corsRules.length); // 1 |
| 106 | +``` |
| 107 | + |
| 108 | +### Use with AWS S3 |
| 109 | + |
| 110 | +For AWS S3, no endpoint is needed — just region and credentials. |
| 111 | + |
| 112 | +```typescript |
| 113 | +const provisioner = new BucketProvisioner({ |
| 114 | + connection: { |
| 115 | + provider: 's3', |
| 116 | + region: 'us-west-2', |
| 117 | + accessKeyId: process.env.AWS_ACCESS_KEY_ID!, |
| 118 | + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!, |
| 119 | + }, |
| 120 | + allowedOrigins: ['https://app.example.com'], |
| 121 | +}); |
| 122 | +``` |
| 123 | + |
| 124 | +### Use with Cloudflare R2 |
| 125 | + |
| 126 | +```typescript |
| 127 | +const provisioner = new BucketProvisioner({ |
| 128 | + connection: { |
| 129 | + provider: 'r2', |
| 130 | + region: 'auto', |
| 131 | + endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, |
| 132 | + accessKeyId: R2_ACCESS_KEY, |
| 133 | + secretAccessKey: R2_SECRET_KEY, |
| 134 | + }, |
| 135 | + allowedOrigins: ['https://app.example.com'], |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +## API |
| 140 | + |
| 141 | +### `BucketProvisioner` |
| 142 | + |
| 143 | +The main class that orchestrates bucket creation and configuration. |
| 144 | + |
| 145 | +#### `new BucketProvisioner(options)` |
| 146 | + |
| 147 | +| Option | Type | Description | |
| 148 | +|--------|------|-------------| |
| 149 | +| `connection.provider` | `'s3' \| 'minio' \| 'r2' \| 'gcs' \| 'spaces'` | Storage provider type | |
| 150 | +| `connection.region` | `string` | S3 region (e.g., `'us-east-1'`) | |
| 151 | +| `connection.endpoint` | `string?` | S3-compatible endpoint URL. Required for non-AWS providers. | |
| 152 | +| `connection.accessKeyId` | `string` | AWS access key ID | |
| 153 | +| `connection.secretAccessKey` | `string` | AWS secret access key | |
| 154 | +| `connection.forcePathStyle` | `boolean?` | Force path-style URLs (auto-detected per provider) | |
| 155 | +| `allowedOrigins` | `string[]` | Domains allowed for CORS (e.g., `['https://app.example.com']`) | |
| 156 | + |
| 157 | +#### `provisioner.provision(options): Promise<ProvisionResult>` |
| 158 | + |
| 159 | +Creates and configures a bucket. Steps: |
| 160 | + |
| 161 | +1. Creates the bucket (or verifies it exists) |
| 162 | +2. Configures Block Public Access |
| 163 | +3. Applies bucket policy (public-read or none) |
| 164 | +4. Sets CORS rules for presigned URL uploads |
| 165 | +5. Optionally enables versioning |
| 166 | +6. Adds lifecycle rules for temp buckets |
| 167 | + |
| 168 | +| Option | Type | Description | |
| 169 | +|--------|------|-------------| |
| 170 | +| `bucketName` | `string` | S3 bucket name | |
| 171 | +| `accessType` | `'public' \| 'private' \| 'temp'` | Determines which policies are applied | |
| 172 | +| `region` | `string?` | Override region for this bucket | |
| 173 | +| `versioning` | `boolean?` | Enable S3 versioning (default: `false`) | |
| 174 | +| `publicUrlPrefix` | `string?` | CDN/public URL for public buckets | |
| 175 | + |
| 176 | +#### `provisioner.inspect(bucketName, accessType): Promise<ProvisionResult>` |
| 177 | + |
| 178 | +Reads back a bucket's current configuration (policy, CORS, versioning, lifecycle). |
| 179 | + |
| 180 | +#### `provisioner.getClient(): S3Client` |
| 181 | + |
| 182 | +Returns the underlying `@aws-sdk/client-s3` S3Client for advanced operations. |
| 183 | + |
| 184 | +#### `provisioner.bucketExists(bucketName): Promise<boolean>` |
| 185 | + |
| 186 | +Checks if a bucket exists and is accessible. |
| 187 | + |
| 188 | +### Policy Builders |
| 189 | + |
| 190 | +Standalone functions for generating S3 policy documents. |
| 191 | + |
| 192 | +#### `getPublicAccessBlock(accessType)` |
| 193 | + |
| 194 | +Returns the Block Public Access configuration for a given access type. |
| 195 | + |
| 196 | +#### `buildPublicReadPolicy(bucketName, keyPrefix?)` |
| 197 | + |
| 198 | +Builds a public-read bucket policy document. |
| 199 | + |
| 200 | +#### `buildCloudFrontOacPolicy(bucketName, distributionArn, keyPrefix?)` |
| 201 | + |
| 202 | +Builds a CloudFront Origin Access Control bucket policy. |
| 203 | + |
| 204 | +#### `buildPresignedUrlIamPolicy(bucketName)` |
| 205 | + |
| 206 | +Builds the minimum-permission IAM policy for the presigned URL plugin. |
| 207 | + |
| 208 | +### CORS Builders |
| 209 | + |
| 210 | +#### `buildUploadCorsRules(allowedOrigins, maxAgeSeconds?)` |
| 211 | + |
| 212 | +CORS rules for public/temp buckets (PUT, GET, HEAD). |
| 213 | + |
| 214 | +#### `buildPrivateCorsRules(allowedOrigins, maxAgeSeconds?)` |
| 215 | + |
| 216 | +CORS rules for private buckets (PUT, HEAD only — no GET). |
| 217 | + |
| 218 | +### Lifecycle Builders |
| 219 | + |
| 220 | +#### `buildTempCleanupRule(expirationDays?, prefix?)` |
| 221 | + |
| 222 | +Lifecycle rule for auto-expiring temp bucket objects. |
| 223 | + |
| 224 | +#### `buildAbortIncompleteMultipartRule(days?)` |
| 225 | + |
| 226 | +Lifecycle rule for cleaning up incomplete multipart uploads. |
| 227 | + |
| 228 | +### Error Handling |
| 229 | + |
| 230 | +All errors thrown by the provisioner are instances of `ProvisionerError`: |
| 231 | + |
| 232 | +```typescript |
| 233 | +import { ProvisionerError } from '@constructive-io/bucket-provisioner'; |
| 234 | + |
| 235 | +try { |
| 236 | + await provisioner.provision({ bucketName: 'test', accessType: 'private' }); |
| 237 | +} catch (err) { |
| 238 | + if (err instanceof ProvisionerError) { |
| 239 | + console.error(err.code); // 'POLICY_FAILED', 'CORS_FAILED', etc. |
| 240 | + console.error(err.message); // Human-readable description |
| 241 | + console.error(err.cause); // Original AWS SDK error |
| 242 | + } |
| 243 | +} |
| 244 | +``` |
| 245 | + |
| 246 | +Error codes: |
| 247 | + |
| 248 | +| Code | Description | |
| 249 | +|------|-------------| |
| 250 | +| `CONNECTION_FAILED` | Could not connect to the storage endpoint | |
| 251 | +| `BUCKET_ALREADY_EXISTS` | Bucket exists and is owned by another account | |
| 252 | +| `BUCKET_NOT_FOUND` | Bucket does not exist (for inspect/read operations) | |
| 253 | +| `INVALID_CONFIG` | Invalid configuration (missing credentials, origins, etc.) | |
| 254 | +| `POLICY_FAILED` | Failed to apply Block Public Access or bucket policy | |
| 255 | +| `CORS_FAILED` | Failed to set CORS configuration | |
| 256 | +| `LIFECYCLE_FAILED` | Failed to set lifecycle rules | |
| 257 | +| `VERSIONING_FAILED` | Failed to enable versioning | |
| 258 | +| `ACCESS_DENIED` | Credentials lack required permissions | |
| 259 | +| `PROVIDER_ERROR` | Generic provider error (check `cause` for details) | |
| 260 | + |
| 261 | +## Privacy Model |
| 262 | + |
| 263 | +| Access Type | Block Public Access | Bucket Policy | CORS Methods | Lifecycle | |
| 264 | +|-------------|-------------------|---------------|--------------|-----------| |
| 265 | +| `private` | All blocked | None (deleted) | PUT, HEAD | None | |
| 266 | +| `public` | Partially relaxed | Public-read | PUT, GET, HEAD | None | |
| 267 | +| `temp` | All blocked | None (deleted) | PUT, GET, HEAD | Auto-expire (1 day) | |
| 268 | + |
| 269 | +## Provider Notes |
| 270 | + |
| 271 | +| Provider | Endpoint Required | Path Style | Notes | |
| 272 | +|----------|------------------|------------|-------| |
| 273 | +| `s3` | No | Virtual-hosted | AWS default | |
| 274 | +| `minio` | Yes | Path-style | Local development, self-hosted | |
| 275 | +| `r2` | Yes | Path-style | Cloudflare R2 | |
| 276 | +| `gcs` | Yes | Path-style | GCS S3-compatible API | |
| 277 | +| `spaces` | Yes | Virtual-hosted | DigitalOcean Spaces | |
0 commit comments