Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
# Lab4 extensions: presigned direct upload (track 1)
|
||||
|
||||
The mandatory contract in `openapi.json` is unchanged. The proxy endpoint
|
||||
`POST /api/v1/chats/{chat_id}/attachments` remains the required path and is the
|
||||
fallback for clients that cannot reach S3. This extension adds an optional
|
||||
direct path:
|
||||
|
||||
1. `POST /api/v1/chats/{chat_id}/attachments/presign` with
|
||||
`{filename, content_type, size_bytes, sha256}` creates a `pending`
|
||||
attachment row and returns a short-lived presigned `PUT` URL.
|
||||
2. The client uploads the bytes directly to the private bucket using the URL
|
||||
and the returned `Content-Type` / `x-amz-meta-sha256` headers.
|
||||
3. `POST /api/v1/attachments/{attachment_id}/complete` with
|
||||
`{sha256, size_bytes}` verifies ownership, the stored object size and the
|
||||
SHA-256 of the bytes. Only then does the attachment become `ready`.
|
||||
|
||||
## Rules
|
||||
|
||||
- The bucket stays private. The URL is the only grant, expires after
|
||||
`PRESIGN_TTL_SECONDS` (default 300 s), and uses a unique server-generated
|
||||
object key.
|
||||
- Complete is owner-only and chat-member-only. A different user receives 404.
|
||||
- Complete is idempotent for the same `(sha256, size_bytes)` pair. A different
|
||||
pair on a ready attachment is `409`.
|
||||
- Completing before the object exists, after the URL expired, or after the
|
||||
pending row was cleaned up is `409`.
|
||||
- A wrong size or checksum is `400`; the wrong object is deleted and is never
|
||||
promoted to `ready`.
|
||||
- `GET /api/v1/attachments/{id}` may return `pending` for this extension.
|
||||
The mandatory proxy path always returns `ready`.
|
||||
- Incomplete uploads are removed by `python -m app.reconcile --apply` after
|
||||
`PRESIGN_GRACE_SECONDS` (default 3600 s). Orphan objects that never got a
|
||||
metadata row are removed after `ORPHAN_GRACE_SECONDS` (default 86400 s).
|
||||
- The complete call reads the object once through the API to verify its
|
||||
checksum. That is a deliberate integrity cost: direct upload avoids API
|
||||
ingress, but the API still verifies egress. A future S3 checksum API could
|
||||
remove this read; SeaweedFS 3.80 does not provide the same portable hook.
|
||||
@@ -0,0 +1,158 @@
|
||||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {
|
||||
"title": "Messenger lab4 extensions: presigned direct upload",
|
||||
"version": "1.4.0-extension.1",
|
||||
"description": "Backward-compatible extension. The mandatory proxy upload in openapi.json remains supported and is the fallback."
|
||||
},
|
||||
"paths": {
|
||||
"/api/v1/chats/{chat_id}/attachments/presign": {
|
||||
"post": {
|
||||
"operationId": "presignAttachmentUpload",
|
||||
"summary": "Create a short-lived presigned PUT URL for a private-bucket object",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "chat_id",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {"type": "string", "format": "uuid"}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/PresignRequest"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"201": {
|
||||
"description": "URL created; the attachment is pending until complete",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/PresignResponse"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {"$ref": "#/components/responses/Error"},
|
||||
"401": {"$ref": "#/components/responses/Error"},
|
||||
"404": {"$ref": "#/components/responses/Error"},
|
||||
"413": {"$ref": "#/components/responses/Error"},
|
||||
"415": {"$ref": "#/components/responses/Error"},
|
||||
"503": {"$ref": "#/components/responses/Error"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/attachments/{attachment_id}/complete": {
|
||||
"post": {
|
||||
"operationId": "completeAttachmentUpload",
|
||||
"summary": "Verify size and SHA-256 of a direct upload and make it ready",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "attachment_id",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {"type": "string", "format": "uuid"}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/CompleteRequest"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Ready attachment, or the same ready attachment on an idempotent retry",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/AttachmentExtension"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {"$ref": "#/components/responses/Error"},
|
||||
"401": {"$ref": "#/components/responses/Error"},
|
||||
"404": {"$ref": "#/components/responses/Error"},
|
||||
"409": {"$ref": "#/components/responses/Error"},
|
||||
"503": {"$ref": "#/components/responses/Error"}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"responses": {
|
||||
"Error": {
|
||||
"description": "Typed error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "../openapi.json#/components/schemas/Error"}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"schemas": {
|
||||
"PresignRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"filename": {"type": "string", "minLength": 1, "maxLength": 255},
|
||||
"content_type": {"type": "string", "enum": ["image/png", "image/jpeg"]},
|
||||
"size_bytes": {"type": "integer", "minimum": 1, "maximum": 10485760},
|
||||
"sha256": {"type": "string", "pattern": "^[a-f0-9]{64}$"}
|
||||
},
|
||||
"required": ["filename", "content_type", "size_bytes", "sha256"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"PresignResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"attachment_id": {"type": "string", "format": "uuid"},
|
||||
"upload_url": {"type": "string", "format": "uri"},
|
||||
"method": {"type": "string", "const": "PUT"},
|
||||
"headers": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Content-Type": {"type": "string", "enum": ["image/png", "image/jpeg"]},
|
||||
"x-amz-meta-sha256": {"type": "string", "pattern": "^[a-f0-9]{64}$"}
|
||||
},
|
||||
"required": ["Content-Type", "x-amz-meta-sha256"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"expires_at": {"type": "string", "format": "date-time"}
|
||||
},
|
||||
"required": ["attachment_id", "upload_url", "method", "headers", "expires_at"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"CompleteRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sha256": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
|
||||
"size_bytes": {"type": "integer", "minimum": 1, "maximum": 10485760}
|
||||
},
|
||||
"required": ["sha256", "size_bytes"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"AttachmentExtension": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {"type": "string", "format": "uuid"},
|
||||
"chat_id": {"type": "string", "format": "uuid"},
|
||||
"owner_id": {"type": "string", "format": "uuid"},
|
||||
"filename": {"type": "string", "minLength": 1, "maxLength": 255},
|
||||
"content_type": {"type": "string", "enum": ["image/png", "image/jpeg"]},
|
||||
"size_bytes": {"type": "integer", "minimum": 1, "maximum": 10485760},
|
||||
"sha256": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
|
||||
"status": {"type": "string", "enum": ["pending", "ready", "failed"]},
|
||||
"variants": {"type": "array", "items": {"type": "object"}},
|
||||
"error_code": {"type": ["string", "null"]},
|
||||
"created_at": {"type": "string", "format": "date-time"},
|
||||
"updated_at": {"type": "string", "format": "date-time"}
|
||||
},
|
||||
"required": ["id", "chat_id", "owner_id", "filename", "content_type", "size_bytes", "sha256", "status", "variants", "error_code", "created_at", "updated_at"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user