Skip to content

Commit 33236a0

Browse files
authored
fix: listObject v1 s3 endpoint (#765)
1 parent 3d9254a commit 33236a0

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/storage/object.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface ListObjectsV2Result {
4646
objects: Obj[]
4747
hasNext: boolean
4848
nextCursor?: string
49+
nextCursorKey?: string
4950
}
5051

5152
/**
@@ -661,6 +662,8 @@ export class ObjectStorage {
661662
})
662663

663664
let nextContinuationToken: string | undefined
665+
let nextCursorKey: string | undefined
666+
664667
if (isTruncated) {
665668
const sortColumn = (cursor?.sortColumn || options?.sortBy?.column) as
666669
| 'name'
@@ -677,11 +680,13 @@ export class ObjectStorage {
677680
? new Date(searchResult[searchResult.length - 1][sortColumn] || '').toISOString()
678681
: undefined,
679682
})
683+
nextCursorKey = searchResult[searchResult.length - 1].name
680684
}
681685

682686
return {
683687
hasNext: isTruncated,
684688
nextCursor: nextContinuationToken,
689+
nextCursorKey,
685690
folders: folders,
686691
objects: objects,
687692
}

src/storage/protocols/s3/s3-handler.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,16 @@ export class S3ProtocolHandler {
164164
EncodingType: command.EncodingType,
165165
MaxKeys: command.MaxKeys,
166166
Prefix: command.Prefix,
167-
ContinuationToken: command.Marker,
167+
StartAfter: command.Marker,
168+
cursorV1: true,
168169
})
169170

170171
return {
171172
responseBody: {
172173
ListBucketResult: {
173174
Name: list.responseBody.ListBucketResult.Name,
174175
Prefix: list.responseBody.ListBucketResult.Prefix,
175-
Marker: list.responseBody.ListBucketResult.ContinuationToken,
176+
Marker: list.responseBody.ListBucketResult.NextContinuationToken,
176177
MaxKeys: list.responseBody.ListBucketResult.MaxKeys,
177178
IsTruncated: list.responseBody.ListBucketResult.IsTruncated,
178179
Contents: list.responseBody.ListBucketResult.Contents,
@@ -190,7 +191,7 @@ export class S3ProtocolHandler {
190191
*
191192
* @param command
192193
*/
193-
async listObjectsV2(command: ListObjectsV2CommandInput) {
194+
async listObjectsV2(command: ListObjectsV2CommandInput & { cursorV1?: boolean }) {
194195
if (!command.Bucket) {
195196
throw ERRORS.MissingParameter('Bucket')
196197
}
@@ -249,7 +250,11 @@ export class S3ProtocolHandler {
249250
}
250251

251252
if (results.nextCursor) {
252-
response.ListBucketResult.NextContinuationToken = results.nextCursor
253+
if (command.cursorV1) {
254+
response.ListBucketResult.NextContinuationToken = results.nextCursorKey
255+
} else {
256+
response.ListBucketResult.NextContinuationToken = results.nextCursor
257+
}
253258
}
254259

255260
return {

src/test/s3-protocol.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,36 @@ describe('S3 Protocol', () => {
233233
const resp = await client.send(listBuckets)
234234
expect(resp.Contents?.length).toBe(3)
235235
})
236+
237+
it('list all keys with pagination', async () => {
238+
const bucket = await createBucket(client)
239+
const listObjects = new ListObjectsCommand({
240+
Bucket: bucket,
241+
Delimiter: '/',
242+
MaxKeys: 1,
243+
})
244+
245+
await Promise.all([
246+
uploadFile(client, bucket, 'test-1.jpg', 1),
247+
uploadFile(client, bucket, 'prefix-1/test-1.jpg', 1),
248+
uploadFile(client, bucket, 'prefix-3/test-1.jpg', 1),
249+
uploadFile(client, bucket, 'prefix-4/test-1.jpg', 1),
250+
])
251+
252+
const resp = await client.send(listObjects)
253+
expect(resp.CommonPrefixes?.length).toBe(1)
254+
expect(resp.IsTruncated).toBe(true)
255+
256+
const listObjects2 = new ListObjectsCommand({
257+
Bucket: bucket,
258+
Delimiter: '/',
259+
MaxKeys: 3,
260+
Marker: resp.Marker,
261+
})
262+
const resp2 = await client.send(listObjects2)
263+
expect(resp2.CommonPrefixes?.length).toBe(2)
264+
expect(resp2.Contents?.length).toBe(1)
265+
})
236266
})
237267

238268
describe('ListObjectsV2Command', () => {

0 commit comments

Comments
 (0)