-
Notifications
You must be signed in to change notification settings - Fork 1
feat(storage): support object version operations #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@tigrisdata/storage': minor | ||
| --- | ||
|
|
||
| Add support for S3 object-version operations on snapshot-enabled buckets: | ||
|
|
||
| - New `listVersions({ prefix, delimiter, limit, keyMarker, versionIdMarker })` returns the object versions and delete markers for a bucket prefix, plus `nextKeyMarker` / `nextVersionIdMarker` for pagination. | ||
| - `head`, `get`, and `remove` now accept a `versionId` option. On `head` / `get` it selects the specified version; on `remove` it permanently deletes that version (without one, `remove` creates a delete marker on a versioned bucket, matching S3 semantics). | ||
|
|
||
| Versioning is enabled implicitly by snapshots — pass `enableSnapshot: true` to `createBucket`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { ListObjectVersionsCommand } from '@aws-sdk/client-s3'; | ||
| import { handleError } from '@shared/utils'; | ||
| import { config, missingConfigError } from '../config'; | ||
| import { createTigrisClient } from '../tigris-client'; | ||
| import type { TigrisStorageConfig, TigrisStorageResponse } from '../types'; | ||
|
|
||
| export type ListVersionsOptions = { | ||
| delimiter?: string; | ||
| prefix?: string; | ||
| limit?: number; | ||
| keyMarker?: string; | ||
| /** | ||
| * Pagination position within a key's versions. S3 ignores this when | ||
| * `keyMarker` is not also set, so passing it alone returns an error. | ||
| */ | ||
| versionIdMarker?: string; | ||
| config?: TigrisStorageConfig; | ||
| }; | ||
|
|
||
| export type ObjectVersion = { | ||
| name: string; | ||
| versionId: string | undefined; | ||
| isLatest: boolean; | ||
| size: number; | ||
| lastModified: Date; | ||
| }; | ||
|
|
||
| export type DeleteMarker = { | ||
| name: string; | ||
| versionId: string | undefined; | ||
| isLatest: boolean; | ||
| lastModified: Date; | ||
| }; | ||
|
designcode marked this conversation as resolved.
|
||
|
|
||
| export type ListVersionsResponse = { | ||
| versions: ObjectVersion[]; | ||
| deleteMarkers: DeleteMarker[]; | ||
| commonPrefixes: string[]; | ||
| nextKeyMarker: string | undefined; | ||
| nextVersionIdMarker: string | undefined; | ||
| hasMore: boolean; | ||
| }; | ||
|
|
||
| export async function listVersions( | ||
| options?: ListVersionsOptions | ||
| ): Promise<TigrisStorageResponse<ListVersionsResponse, Error>> { | ||
| if (!options?.config?.bucket && !config.bucket) { | ||
| return missingConfigError('bucket'); | ||
| } | ||
|
|
||
| if (options?.versionIdMarker && !options?.keyMarker) { | ||
| return { | ||
| error: new Error('versionIdMarker requires keyMarker to also be set'), | ||
| }; | ||
| } | ||
|
|
||
| const { data: tigrisClient, error } = createTigrisClient(options?.config); | ||
|
|
||
| if (error) { | ||
| return { error }; | ||
| } | ||
|
|
||
| const command = new ListObjectVersionsCommand({ | ||
| Bucket: options?.config?.bucket ?? config.bucket, | ||
| Prefix: options?.prefix, | ||
| Delimiter: options?.delimiter, | ||
| MaxKeys: options?.limit, | ||
| KeyMarker: options?.keyMarker, | ||
| VersionIdMarker: options?.versionIdMarker, | ||
| }); | ||
|
|
||
| try { | ||
| return tigrisClient | ||
| .send(command) | ||
| .then((res) => ({ | ||
| data: { | ||
| versions: | ||
| res.Versions?.map((v) => ({ | ||
| name: v.Key ?? '', | ||
| versionId: v.VersionId, | ||
| isLatest: v.IsLatest ?? false, | ||
| size: v.Size ?? 0, | ||
| lastModified: v.LastModified ?? new Date(), | ||
| })) ?? [], | ||
| deleteMarkers: | ||
| res.DeleteMarkers?.map((m) => ({ | ||
| name: m.Key ?? '', | ||
| versionId: m.VersionId, | ||
| isLatest: m.IsLatest ?? false, | ||
| lastModified: m.LastModified ?? new Date(), | ||
| })) ?? [], | ||
|
designcode marked this conversation as resolved.
|
||
| commonPrefixes: | ||
| res.CommonPrefixes?.map((p) => p.Prefix ?? '').filter(Boolean) ?? | ||
| [], | ||
| nextKeyMarker: res.NextKeyMarker, | ||
| nextVersionIdMarker: res.NextVersionIdMarker, | ||
| hasMore: res.IsTruncated ?? false, | ||
| }, | ||
| })) | ||
| .catch(handleError); | ||
| } catch (error) { | ||
| return handleError(error as Error); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
packages/storage/src/test/object-versions.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
| import { createBucket } from '../lib/bucket/create'; | ||
| import { removeBucket } from '../lib/bucket/remove'; | ||
| import { config } from '../lib/config'; | ||
| import { get } from '../lib/object/get'; | ||
| import { head } from '../lib/object/head'; | ||
| import { listVersions } from '../lib/object/list-versions'; | ||
| import { put } from '../lib/object/put'; | ||
| import { remove } from '../lib/object/remove'; | ||
| import { shouldSkipIntegrationTests } from './setup'; | ||
|
|
||
| const skipTests = shouldSkipIntegrationTests(); | ||
|
|
||
| describe.skipIf(skipTests)('Object versioning Integration Tests', () => { | ||
| const bucket = `test-versions-${Date.now()}`.toLowerCase(); | ||
| const bucketConfig = { ...config, bucket }; | ||
|
|
||
| beforeAll(async () => { | ||
| const result = await createBucket(bucket, { | ||
| enableSnapshot: true, | ||
| config, | ||
| }); | ||
| expect( | ||
| result.error, | ||
| `bucket create failed: ${result.error?.message}` | ||
| ).toBeUndefined(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await removeBucket(bucket, { force: true, config }); | ||
| }); | ||
|
|
||
| it('lists multiple versions of the same key', async () => { | ||
| const key = `nested/key-${Date.now()}.txt`; | ||
|
|
||
| await put(key, 'v1', { config: bucketConfig }); | ||
| await put(key, 'v2', { config: bucketConfig }); | ||
|
|
||
| const result = await listVersions({ | ||
| prefix: key, | ||
| config: bucketConfig, | ||
| }); | ||
|
|
||
| expect(result.error).toBeUndefined(); | ||
| expect(result.data?.versions).toHaveLength(2); | ||
| expect(result.data?.deleteMarkers).toHaveLength(0); | ||
|
|
||
| const versions = result.data?.versions ?? []; | ||
| expect(versions.filter((v) => v.isLatest)).toHaveLength(1); | ||
| for (const v of versions) { | ||
| expect(v.name).toBe(key); | ||
| expect(v.versionId).toBeTruthy(); | ||
| expect(v.lastModified).toBeInstanceOf(Date); | ||
| } | ||
| }); | ||
|
|
||
| it('reads a prior version with get(versionId) and head(versionId)', async () => { | ||
| const key = `read-prev-${Date.now()}.txt`; | ||
|
|
||
| await put(key, 'old', { config: bucketConfig }); | ||
| await put(key, 'new', { config: bucketConfig }); | ||
|
|
||
| const { data: list } = await listVersions({ | ||
| prefix: key, | ||
| config: bucketConfig, | ||
| }); | ||
| const older = list?.versions.find((v) => !v.isLatest); | ||
| expect(older).toBeDefined(); | ||
|
|
||
| const getRes = await get(key, 'string', { | ||
| versionId: older?.versionId, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(getRes.error).toBeUndefined(); | ||
| expect(getRes.data).toBe('old'); | ||
|
|
||
| const latest = await get(key, 'string', { config: bucketConfig }); | ||
| expect(latest.data).toBe('new'); | ||
|
|
||
| const headRes = await head(key, { | ||
| versionId: older?.versionId, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(headRes.error).toBeUndefined(); | ||
| expect(headRes.data?.path).toBe(key); | ||
| }); | ||
|
|
||
| it('remove() without versionId creates a delete marker', async () => { | ||
| const key = `del-marker-${Date.now()}.txt`; | ||
| await put(key, 'v1', { config: bucketConfig }); | ||
|
|
||
| const removeRes = await remove(key, { config: bucketConfig }); | ||
| expect(removeRes.error).toBeUndefined(); | ||
|
|
||
| const { data } = await listVersions({ | ||
| prefix: key, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(data?.versions).toHaveLength(1); | ||
| expect(data?.deleteMarkers).toHaveLength(1); | ||
| expect(data?.deleteMarkers[0]?.isLatest).toBe(true); | ||
| }); | ||
|
|
||
| it('remove() with versionId permanently deletes that version', async () => { | ||
| const key = `perm-del-${Date.now()}.txt`; | ||
| await put(key, 'v1', { config: bucketConfig }); | ||
| await put(key, 'v2', { config: bucketConfig }); | ||
|
|
||
| const { data: before } = await listVersions({ | ||
| prefix: key, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(before?.versions).toHaveLength(2); | ||
|
|
||
| const older = before?.versions.find((v) => !v.isLatest); | ||
| expect(older?.versionId).toBeTruthy(); | ||
|
|
||
| const removeRes = await remove(key, { | ||
| versionId: older?.versionId, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(removeRes.error).toBeUndefined(); | ||
|
|
||
| const { data: after } = await listVersions({ | ||
| prefix: key, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(after?.versions).toHaveLength(1); | ||
| expect(after?.deleteMarkers).toHaveLength(0); | ||
| expect(after?.versions.some((v) => v.versionId === older?.versionId)).toBe( | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| it('paginates with keyMarker / versionIdMarker', async () => { | ||
| const prefix = `page-${Date.now()}/`; | ||
| const keys = [`${prefix}a.txt`, `${prefix}b.txt`, `${prefix}c.txt`]; | ||
| for (const k of keys) { | ||
| await put(k, 'x', { config: bucketConfig }); | ||
| } | ||
|
|
||
| const first = await listVersions({ | ||
| prefix, | ||
| limit: 2, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(first.data?.hasMore).toBe(true); | ||
| expect(first.data?.versions).toHaveLength(2); | ||
| expect(first.data?.nextKeyMarker).toBeTruthy(); | ||
|
|
||
| const second = await listVersions({ | ||
| prefix, | ||
| keyMarker: first.data?.nextKeyMarker, | ||
| versionIdMarker: first.data?.nextVersionIdMarker, | ||
| config: bucketConfig, | ||
| }); | ||
| expect(second.data?.hasMore).toBe(false); | ||
| expect(second.data?.versions.length).toBeGreaterThanOrEqual(1); | ||
|
|
||
| const firstNames = first.data?.versions.map((v) => v.name) ?? []; | ||
| const secondNames = second.data?.versions.map((v) => v.name) ?? []; | ||
| expect(firstNames.some((n) => secondNames.includes(n))).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.