(
key: string,
body: Uint8Array,
metadata: PutObjectMetadata = {},
)
| 36 | * Writes one object directly to the configured R2 bucket from the container. |
| 37 | */ |
| 38 | export const putObject = async ( |
| 39 | key: string, |
| 40 | body: Uint8Array, |
| 41 | metadata: PutObjectMetadata = {}, |
| 42 | ): Promise<void> => { |
| 43 | const headers: Record<string, string> = { |
| 44 | 'Content-Length': String(body.byteLength), |
| 45 | }; |
| 46 | |
| 47 | if (metadata.cacheControl) { |
| 48 | headers['Cache-Control'] = metadata.cacheControl; |
| 49 | } |
| 50 | |
| 51 | if (metadata.contentType) { |
| 52 | headers['Content-Type'] = metadata.contentType; |
| 53 | } |
| 54 | |
| 55 | const response = await client.client.fetch(buildObjectUrl(key), { |
| 56 | method: 'PUT', |
| 57 | body: body.slice(), |
| 58 | headers, |
| 59 | }); |
| 60 | |
| 61 | if (!response.ok) { |
| 62 | const text = await response.text(); |
| 63 | throw new Error( |
| 64 | `Failed to upload ${key}: ${response.status} ${text || response.statusText}`, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | console.log(`[r2] PUT ${key} (${body.byteLength} bytes)`); |
| 69 | }; |
no test coverage detected