Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions packages/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,19 @@ async function startServer() {
2,
),
)
} else {
console.error(
JSON.stringify(
{
type: "error",
message: "Unknown error",
cause: error,
},
undefined,
2,
),
)
}
console.error(
JSON.stringify(
{
type: "error",
message: "Unknown error",
cause: error,
},
undefined,
2,
),
)

console.error("Restarting in 3 seconds...")
await new Promise((resolve) => setTimeout(resolve, 3000))
Expand Down
24 changes: 19 additions & 5 deletions packages/api/src/utilities/storage/ensureStorageBucket.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { S3 } from "@aws-sdk/client-s3"
import { CreateBucketCommand, HeadBucketCommand } from "@aws-sdk/client-s3"
import { CreateBucketCommand, HeadBucketCommand, type HeadBucketCommandOutput } from "@aws-sdk/client-s3"

/**
* Ensures the storage bucket exists, creating it if necessary.
* Should be called once at server startup.
*/
export async function ensureStorageBucket(client: S3, bucketName: string) {
let headResult: HeadBucketCommandOutput | undefined
try {
await client.send(new HeadBucketCommand({ Bucket: bucketName }))
headResult = await client.send(new HeadBucketCommand({ Bucket: bucketName }))
} catch (error: unknown) {
const name = error instanceof Object && "name" in error ? (error as { name: string }).name : undefined
const httpStatusCode =
Expand All @@ -18,12 +19,25 @@ export async function ensureStorageBucket(client: S3, bucketName: string) {
? (error.$metadata as { httpStatusCode: number }).httpStatusCode
: undefined

if (name === "NotFound" || name === "NoSuchBucket" || httpStatusCode === 404 || httpStatusCode === 403) {
if (name === "NotFound" || name === "NoSuchBucket" || httpStatusCode === 404) {
console.info(`Bucket "${bucketName}" not found, creating it...`)
await client.send(new CreateBucketCommand({ Bucket: bucketName }))
console.info(`Bucket "${bucketName}" created.`)
} else {
throw error
return
}

// Some S3-compatible providers (OpenStack Swift, etc.) return 400
// or other non-standard codes for HeadBucket. Log and continue
// rather than crashing the server — actual storage operations
// will surface real errors at request time.
console.warn(
`HeadBucket check failed (HTTP ${httpStatusCode ?? "unknown"}, name: ${name ?? "unknown"}). ` +
`Assuming bucket "${bucketName}" exists. Storage errors will surface at request time if misconfigured.`,
)
return
}

if (headResult) {
console.info(`Bucket "${bucketName}" is accessible.`)
}
}
1 change: 0 additions & 1 deletion packages/api/src/utilities/storage/generatePutSignedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export async function generatePutSignedUrl(parameters: {
const signedUrl = await getSignedUrl(
parameters.var.clients.storagePublic,
new PutObjectCommand({
ACL: "private",
Bucket: parameters.var.env.STORAGE_BUCKET_NAME,
Key: parameters.storageKey,
ContentLength: parameters.contentLength,
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/utilities/storage/putObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export async function putObject(parameters: {
}) {
try {
const command = new PutObjectCommand({
ACL: "private",
Bucket: parameters.var.env.STORAGE_BUCKET_NAME,
Key: parameters.storageKey,
ContentLength: parameters.contentLength,
Expand Down
Loading