Repository navigation
fix(bb-file-bucket): reconcile current files after version deletion #693
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
xyos
merged 6 commits into
aws:main
from
ikenyal:fix/file-bucket-current-version-deletion
Nov 3, 2026
+435
−32
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
017b18d
fix(bb-file-bucket): reconcile current files after version deletion
ikenyal 1cb4b33
Merge main into fix/file-bucket-current-version-deletion
ikenyal 17ed7cc
Merge remote-tracking branch 'upstream/main' into fix/file-bucket-cur…
ikenyal a4f3d60
docs(file-bucket): clarify historical delete-marker limitation
ikenyal 02caae3
Merge remote-tracking branch 'upstream/main' into fix/file-bucket-cur…
ikenyal 46fdd5a
fix(file-bucket): keep version deletions retryable
ikenyal 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,6 @@ | ||
| --- | ||
| '@aws-blocks/bb-file-bucket': patch | ||
| '@aws-blocks/blocks': patch | ||
| --- | ||
|
|
||
| Fix local FileBucket version deletion so removing the current version promotes the next stored version, including its metadata, for reads, scans, and downloads. Removing the last stored version now removes the current file instead of leaving deleted content accessible. Existing delete markers and older-version deletion behavior are preserved. |
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
161 changes: 161 additions & 0 deletions
161
packages/bb-file-bucket/src/current-version-deletion.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,161 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { test, beforeEach, afterEach } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { existsSync, readFileSync, rmSync, statSync, utimesSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { Scope } from '@aws-blocks/core'; | ||
| import { FileBucket } from './index.mock.js'; | ||
| import { contentPath, metaPath, deleteMarkerPath, versionContentPath } from './paths.js'; | ||
|
|
||
| const scope = new Scope('version-delete'); | ||
| const bucketId = 'files'; | ||
| const dataDir = join(process.cwd(), '.bb-data', `${scope.fullId}-${bucketId}`); | ||
| const key = 'reports/document.txt'; | ||
|
|
||
| beforeEach(() => rmSync(dataDir, { recursive: true, force: true })); | ||
| afterEach(() => rmSync(dataDir, { recursive: true, force: true })); | ||
|
|
||
| async function scanFiles(bucket: FileBucket) { | ||
| const files = []; | ||
| for await (const file of bucket.scan()) files.push(file); | ||
| return files; | ||
| } | ||
|
|
||
| async function putVersions(bucket: FileBucket) { | ||
| await bucket.put(key, 'old', { | ||
| contentType: 'text/plain', | ||
| metadata: { revision: 'old' }, | ||
| cacheControl: 'max-age=60', | ||
| }); | ||
| await bucket.put(key, 'new content', { | ||
| contentType: 'text/csv', | ||
| metadata: { revision: 'new' }, | ||
| cacheControl: 'no-cache', | ||
| }); | ||
| return bucket.listVersions(key); | ||
| } | ||
|
|
||
| test('deleting the current version promotes the previous body and metadata without a new version', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| const [latest, previous] = await putVersions(bucket); | ||
| const timestamp = new Date('2020-01-01T00:00:00.000Z'); | ||
| utimesSync(versionContentPath(dataDir, key, previous.versionId), timestamp, timestamp); | ||
| await bucket.delete(key, { versionId: latest.versionId }); | ||
| const file = await bucket.get(key); | ||
| assert.ok(file); | ||
| assert.equal(file.body.toString(), 'old'); | ||
| assert.equal(file.contentType, 'text/plain'); | ||
| assert.deepEqual(file.metadata, { revision: 'old' }); | ||
| assert.equal(file.size, 3); | ||
| assert.equal(JSON.parse(readFileSync(metaPath(dataDir, key), 'utf8')).cacheControl, 'max-age=60'); | ||
| assert.equal(await bucket.get(key, { versionId: latest.versionId }), null); | ||
| const versions = await bucket.listVersions(key); | ||
| assert.equal(versions.length, 1); | ||
| assert.equal(versions[0].versionId, previous.versionId); | ||
| assert.equal(versions[0].isCurrent, true); | ||
| const files = await scanFiles(bucket); | ||
| assert.equal(files.length, 1); | ||
| assert.equal(files[0].size, 3); | ||
| assert.equal(files[0].lastModified.getTime(), timestamp.getTime()); | ||
| const reopened = new FileBucket(scope, bucketId); | ||
| assert.equal((await reopened.get(key))?.body.toString(), 'old'); | ||
| assert.deepEqual((await reopened.get(key))?.metadata, { revision: 'old' }); | ||
| }); | ||
|
|
||
| test('equal version timestamps use the same numeric ordering as listVersions', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| await bucket.put(key, 'first'); | ||
| await bucket.put(key, 'second'); | ||
| await bucket.put(key, 'third'); | ||
| const timestamp = new Date('2020-01-01T00:00:00.000Z'); | ||
| for (const version of await bucket.listVersions(key)) { | ||
| utimesSync(versionContentPath(dataDir, key, version.versionId), timestamp, timestamp); | ||
| } | ||
| const [latest, previous] = await bucket.listVersions(key); | ||
| await bucket.delete(key, { versionId: latest.versionId }); | ||
| assert.equal((await bucket.get(key))?.body.toString(), 'second'); | ||
| assert.equal((await bucket.listVersions(key))[0].versionId, previous.versionId); | ||
| }); | ||
|
|
||
| test('deleting the only version removes the current body and sidecar', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| await bucket.put(key, 'only'); | ||
| const [version] = await bucket.listVersions(key); | ||
| await bucket.delete(key, { versionId: version.versionId }); | ||
| assert.equal(await bucket.get(key), null); | ||
| assert.deepEqual(await scanFiles(bucket), []); | ||
| assert.deepEqual(await bucket.listVersions(key), []); | ||
| assert.equal(existsSync(contentPath(dataDir, key)), false); | ||
| assert.equal(existsSync(metaPath(dataDir, key)), false); | ||
| }); | ||
|
|
||
| test('repeated newest-first deletion promotes each remaining version and then removes the file', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| await bucket.put(key, 'first'); | ||
| await bucket.put(key, 'second'); | ||
| await bucket.put(key, 'third'); | ||
| for (const expected of ['second', 'first', null]) { | ||
| const [current] = await bucket.listVersions(key); | ||
| await bucket.delete(key, { versionId: current.versionId }); | ||
| assert.equal((await bucket.get(key))?.body.toString() ?? null, expected); | ||
| } | ||
| assert.deepEqual(await scanFiles(bucket), []); | ||
| }); | ||
|
|
||
| test('deleting an older version leaves current content, metadata, and modification time unchanged', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| const [latest, previous] = await putVersions(bucket); | ||
| const before = statSync(contentPath(dataDir, key)).mtimeMs; | ||
| await bucket.delete(key, { versionId: previous.versionId }); | ||
| assert.equal((await bucket.get(key))?.body.toString(), 'new content'); | ||
| assert.deepEqual((await bucket.get(key))?.metadata, { revision: 'new' }); | ||
| assert.equal(statSync(contentPath(dataDir, key)).mtimeMs, before); | ||
| assert.equal((await bucket.listVersions(key))[0].versionId, latest.versionId); | ||
| }); | ||
|
|
||
| test('deleting an unknown version is a no-op for existing and missing objects', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| const versions = await putVersions(bucket); | ||
| const before = statSync(contentPath(dataDir, key)).mtimeMs; | ||
| await bucket.delete(key, { versionId: 'v999' }); | ||
| await bucket.delete('missing.txt', { versionId: 'v999' }); | ||
| assert.equal((await bucket.get(key))?.body.toString(), 'new content'); | ||
| assert.equal(statSync(contentPath(dataDir, key)).mtimeMs, before); | ||
| assert.deepEqual(await bucket.listVersions(key), versions); | ||
| assert.equal(await bucket.get('missing.txt'), null); | ||
| }); | ||
|
|
||
| test('deleting versions does not remove an existing delete marker or resurrect the object', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| const versions = await putVersions(bucket); | ||
| await bucket.delete(key); | ||
| for (const version of versions) { | ||
| await bucket.delete(key, { versionId: version.versionId }); | ||
| assert.equal(await bucket.get(key), null); | ||
| assert.deepEqual(await scanFiles(bucket), []); | ||
| assert.equal(existsSync(deleteMarkerPath(dataDir, key)), true); | ||
| assert.ok((await bucket.listVersions(key)).every((remaining) => !remaining.isCurrent)); | ||
| } | ||
| assert.equal(existsSync(contentPath(dataDir, key)), false); | ||
| await bucket.put(key, 'uploaded again'); | ||
| assert.equal((await bucket.get(key))?.body.toString(), 'uploaded again'); | ||
| }); | ||
|
|
||
| test('concurrent version deletions do not retain a stale current copy', async () => { | ||
| const bucket = new FileBucket(scope, bucketId); | ||
| const versions = await putVersions(bucket); | ||
| await Promise.all(versions.map((version) => bucket.delete(key, { versionId: version.versionId }))); | ||
| assert.equal(await bucket.get(key), null); | ||
| assert.deepEqual(await bucket.listVersions(key), []); | ||
| assert.deepEqual(await scanFiles(bucket), []); | ||
| }); | ||
|
|
||
| test('non-versioned deletion still removes the object', async () => { | ||
| const bucket = new FileBucket(scope, bucketId, { versioned: false }); | ||
| await bucket.put(key, 'data'); | ||
| await bucket.delete(key); | ||
| assert.equal(await bucket.get(key), null); | ||
| assert.deepEqual(await scanFiles(bucket), []); | ||
| }); |
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
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.