| 38 | * 通过 PUT 请求上传内容到 S3 |
| 39 | */ |
| 40 | export class S3FileWriter implements FileWriter { |
| 41 | client: S3Client; |
| 42 | |
| 43 | bucket: string; |
| 44 | |
| 45 | key: string; |
| 46 | |
| 47 | modifiedDate?: number; |
| 48 | |
| 49 | constructor(client: S3Client, bucket: string, key: string, modifiedDate?: number) { |
| 50 | this.client = client; |
| 51 | this.bucket = bucket; |
| 52 | this.key = key; |
| 53 | this.modifiedDate = modifiedDate; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * 写入文件内容 |
| 58 | * @param content 文件内容(字符串或 Blob) |
| 59 | * @throws {S3Error} 上传失败 |
| 60 | */ |
| 61 | async write(content: string | Blob): Promise<void> { |
| 62 | const body = content instanceof Blob ? new Uint8Array(await content.arrayBuffer()) : content; |
| 63 | |
| 64 | const headers: Record<string, string> = { |
| 65 | "content-type": "application/octet-stream", |
| 66 | }; |
| 67 | if (this.modifiedDate) { |
| 68 | // 历史兼容:S3 侧使用 createtime 元数据保存文件时间,实际来源是 FileCreateOptions.modifiedDate。 |
| 69 | headers["x-amz-meta-createtime"] = new Date(this.modifiedDate).toISOString(); |
| 70 | } |
| 71 | |
| 72 | await this.client.request("PUT", this.bucket, this.key, { |
| 73 | body: typeof body === "string" ? body : body, |
| 74 | headers, |
| 75 | }); |
| 76 | } |
| 77 | } |
nothing calls this directly
no outgoing calls
no test coverage detected