feat: S3 client compatibility — virtual-hosted style, CORS, presigned multi-method, ListMultipartUploads, edge case fixes
- Virtual-hosted bucket detection from Host header (extractS3BucketFromHost) - S3 CORS headers + OPTIONS preflight + x-amz-id-2/HostId everywhere - Presigned GET/PUT/HEAD/DELETE via centralized auth (no GET-only restriction) - ListMultipartUploads with DB/xml helpers - UploadPart partNumber range validation (1-10000) - CompleteMultipartUpload ETag matching and ascending order validation - DeleteObjects quiet mode support - CopyObject URL-decode and conditional if-match/if-none-match - encoding-type=url support in ListObjects V1/V2 XML - Safe range-based prefix matching (replaces SQL LIKE) - STREAMING-AWS4-HMAC-SHA256-PAYLOAD → 501 NotImplemented - Traefik wildcard HostRegex for virtual-hosted style - S3_VHOST_DOMAINS config env var
This commit is contained in:
+3
-1
@@ -62,7 +62,9 @@ export const listObjectsByPrefix = async (
|
||||
maxKeys: number,
|
||||
startAfter: string | null,
|
||||
): Promise<{ objects: S3FileRecord[]; prefixes: string[] }> => {
|
||||
let query = sql`SELECT * FROM files WHERE bucket_id = ${bucketId}::uuid AND is_deleted = false AND s3_key LIKE ${`${prefix}%`}`;
|
||||
let query = prefix
|
||||
? sql`SELECT * FROM files WHERE bucket_id = ${bucketId}::uuid AND is_deleted = false AND s3_key >= ${prefix} AND s3_key < ${`${prefix}`}`
|
||||
: sql`SELECT * FROM files WHERE bucket_id = ${bucketId}::uuid AND is_deleted = false`;
|
||||
|
||||
if (startAfter) {
|
||||
query = sql`${query} AND s3_key > ${startAfter}`;
|
||||
|
||||
@@ -90,3 +90,40 @@ export const listMultipartParts = async (uploadId: string): Promise<MultipartPar
|
||||
createdAt: new Date(r.created_at as string),
|
||||
}));
|
||||
};
|
||||
|
||||
const mapRowToMultipartUpload = (r: Record<string, unknown>): MultipartUpload => ({
|
||||
uploadId: r.upload_id as string,
|
||||
bucketId: r.bucket_id as string,
|
||||
s3Key: r.s3_key as string,
|
||||
initiatedAt: new Date(r.initiated_at as string),
|
||||
status: r.status as string,
|
||||
initiatedBy: (r.initiated_by as string | null) || '',
|
||||
});
|
||||
|
||||
export const listMultipartUploadsByBucket = async (
|
||||
bucketId: string,
|
||||
maxUploads: number,
|
||||
keyMarker: string | null,
|
||||
): Promise<{ uploads: MultipartUpload[]; isTruncated: boolean; nextKeyMarker: string | null }> => {
|
||||
const limit = Math.min(Math.max(maxUploads || 1000, 1), 1000);
|
||||
const result = (await db.execute(
|
||||
keyMarker
|
||||
? sql`SELECT upload_id, bucket_id, s3_key, initiated_at, status, initiated_by
|
||||
FROM multipart_uploads
|
||||
WHERE bucket_id = ${bucketId}::uuid AND status = 'in_progress' AND s3_key > ${keyMarker}
|
||||
ORDER BY s3_key, initiated_at
|
||||
LIMIT ${limit + 1}`
|
||||
: sql`SELECT upload_id, bucket_id, s3_key, initiated_at, status, initiated_by
|
||||
FROM multipart_uploads
|
||||
WHERE bucket_id = ${bucketId}::uuid AND status = 'in_progress'
|
||||
ORDER BY s3_key, initiated_at
|
||||
LIMIT ${limit + 1}`,
|
||||
)) as unknown as Record<string, unknown>[];
|
||||
|
||||
const uploads = result.slice(0, limit).map(mapRowToMultipartUpload);
|
||||
return {
|
||||
uploads,
|
||||
isTruncated: result.length > limit,
|
||||
nextKeyMarker: result.length > limit ? uploads.at(-1)?.s3Key || null : null,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user