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
5 changes: 5 additions & 0 deletions .changeset/forward-content-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tus/s3-store": minor
---

Forward the `contentEncoding` upload metadata to S3 as `Content-Encoding` when creating the multipart upload, matching `contentType` and `cacheControl`.
14 changes: 14 additions & 0 deletions packages/s3-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Use](#use)
- [API](#api)
- [`new S3Store(options)`](#new-s3storeoptions)
- [Metadata](#metadata)
- [Extensions](#extensions)
- [Examples](#examples)
- [Example: using `credentials` to fetch credentials inside a AWS container](#example-using-credentials-to-fetch-credentials-inside-a-aws-container)
Expand Down Expand Up @@ -138,6 +139,19 @@ overall upload bandwidth available for other concurrent uploads.
throughput. Instead, it will result in additional resource consumption without
proportional gains in transfer speed.

## Metadata

These upload metadata keys are forwarded to S3 as headers on the stored object:

| Metadata key | S3 header |
| ----------------- | ------------------ |
| `contentType` | `Content-Type` |
| `cacheControl` | `Cache-Control` |
| `contentEncoding` | `Content-Encoding` |

`contentEncoding` only labels the object; S3 does not compress it. Set it (e.g. `gzip`)
only when the client uploads bytes that are already encoded that way.

## Extensions

The tus protocol supports optional [extensions][]. Below is a table of the supported
Expand Down
4 changes: 4 additions & 0 deletions packages/s3-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ export class S3Store extends DataStore {
request.CacheControl = upload.metadata.cacheControl
}

if (upload.metadata?.contentEncoding) {
request.ContentEncoding = upload.metadata.contentEncoding
}

upload.creation_date = new Date().toISOString()

const res = await this.client.createMultipartUpload(request)
Expand Down
27 changes: 27 additions & 0 deletions packages/s3-store/src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {AddressInfo} from 'node:net'
import {Readable} from 'node:stream'
import stream from 'node:stream/promises'
import {setTimeout as sleep} from 'node:timers/promises'
import {gzipSync} from 'node:zlib'

import {NoSuchKey, NoSuchUpload, NotFound} from '@aws-sdk/client-s3'
import sinon from 'sinon'
Expand Down Expand Up @@ -330,6 +331,32 @@ describe('S3DataStore', () => {
assert.equal(await store.getIncompletePartSize(upload.id), undefined)
})

it('should preserve content headers metadata on the completed object', async function () {
const store = this.datastore as S3Store
const body = gzipSync('x')
const upload = new Upload({
id: shared.testId('content-headers'),
size: body.length,
offset: 0,
metadata: {
contentType: 'text/plain',
cacheControl: 'max-age=3600',
contentEncoding: 'gzip',
},
})

await store.create(upload)
const offset = await store.write(Readable.from(body), upload.id, 0)
assert.equal(offset, body.length)

// @ts-expect-error protected property
const client = store.client
const head = await client.headObject({Bucket: s3ClientConfig.bucket, Key: upload.id})
assert.equal(head.ContentType, 'text/plain')
assert.equal(head.CacheControl, 'max-age=3600')
assert.equal(head.ContentEncoding, 'gzip')
})

for (const [S3Error, completed] of [
[NoSuchUpload, true],
[NoSuchKey, true],
Expand Down
Loading