Skip to content

Commit 66dab2c

Browse files
authored
added tags response from listObjectsV2WithMetadata (#1298)
1 parent e556d26 commit 66dab2c

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

src/internal/type.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ export interface ItemBucketMetadata {
9191
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9292
[key: string]: any
9393
}
94+
export interface ItemBucketTags {
95+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
96+
[key: string]: any
97+
}
9498

9599
export interface BucketItemFromList {
96100
name: string
@@ -120,6 +124,7 @@ export type BucketItem =
120124

121125
export type BucketItemWithMetadata = BucketItem & {
122126
metadata?: ItemBucketMetadata | ItemBucketMetadataList
127+
tags?: ItemBucketTags
123128
}
124129

125130
export interface BucketStream<T> extends ReadableStream {

src/internal/xml-parser.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
CopyObjectResultV1,
1515
ObjectLockInfo,
1616
ReplicationConfig,
17+
Tags,
1718
} from './type.ts'
1819
import { RETENTION_VALIDITY_UNITS } from './type.ts'
1920

@@ -129,13 +130,24 @@ export function parseListObjectsV2WithMetadata(xml: string) {
129130
const lastModified = new Date(content.LastModified)
130131
const etag = sanitizeETag(content.ETag)
131132
const size = content.Size
133+
134+
let tags: Tags = {}
135+
if (content.UserTags != null) {
136+
toArray(content.UserTags.split('&')).forEach((tag) => {
137+
const [key, value] = tag.split('=')
138+
tags[key] = value
139+
})
140+
} else {
141+
tags = {}
142+
}
143+
132144
let metadata
133145
if (content.UserMetadata != null) {
134146
metadata = toArray(content.UserMetadata)[0]
135147
} else {
136148
metadata = null
137149
}
138-
result.objects.push({ name, lastModified, etag, size, metadata })
150+
result.objects.push({ name, lastModified, etag, size, metadata, tags })
139151
})
140152
}
141153

tests/functional/functional-tests.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3620,6 +3620,65 @@ describe('functional tests', function () {
36203620
)
36213621
})
36223622
})
3623+
describe('listObjectsV2WithMetadata with tags and metadata', function () {
3624+
const bucketName = 'minio-js-test-tags-' + uuid.v4()
3625+
const fdObjectName = 'datafile-100-kB'
3626+
const fdObject = dataDir ? fs.readFileSync(dataDir + '/' + fdObjectName) : Buffer.alloc(100 * 1024, 0)
3627+
const objectName = 'objectwithtags'
3628+
const tags = { key1: 'value1', key2: 'value2' }
3629+
const metadata = { 'X-Amz-Meta-Test': 'test-value' }
3630+
3631+
before(() => {
3632+
return client.makeBucket(bucketName, '').then((res) => {
3633+
return client.putObject(bucketName, objectName, fdObject, fdObject.length, metadata).then((res) => {
3634+
return client.setObjectTagging(bucketName, objectName, tags)
3635+
})
3636+
})
3637+
})
3638+
after(() => client.removeObject(bucketName, objectName).then((_) => client.removeBucket(bucketName)))
3639+
3640+
step(
3641+
`extensions.listObjectsV2WithMetadata(bucketName, prefix, recursive)_bucketName:${bucketName}, prefix:"", recursive:true`,
3642+
function (done) {
3643+
const listStream = client.extensions.listObjectsV2WithMetadata(bucketName, '', true)
3644+
let listedObject = null
3645+
3646+
listStream.on('data', function (obj) {
3647+
listedObject = obj
3648+
})
3649+
3650+
listStream.on('end', function () {
3651+
if (!listedObject) {
3652+
return done(new Error('No objects were listed'))
3653+
}
3654+
3655+
if (listedObject.name !== objectName) {
3656+
return done(new Error(`Expected object name: ${objectName}, received: ${listedObject.name}`))
3657+
}
3658+
3659+
if (!_.isEqual(listedObject.tags, tags)) {
3660+
return done(
3661+
new Error(`Expected tags: ${JSON.stringify(tags)}, received: ${JSON.stringify(listedObject.tags)}`),
3662+
)
3663+
}
3664+
3665+
if (!_.isEqual(listedObject.metadata['X-Amz-Meta-Test'], metadata['X-Amz-Meta-Test'])) {
3666+
return done(
3667+
new Error(
3668+
`Expected metadata: ${JSON.stringify(metadata)}, received: ${JSON.stringify(listedObject.metadata)}`,
3669+
),
3670+
)
3671+
}
3672+
3673+
done()
3674+
})
3675+
3676+
listStream.on('error', function (e) {
3677+
done(e)
3678+
})
3679+
},
3680+
)
3681+
})
36233682
describe('Object Name special characters test with a Prefix', () => {
36243683
// Isolate the bucket/object for easy debugging and tracking.
36253684
const bucketNameForSpCharObjects = 'minio-js-test-obj-spnpre-' + uuid.v4()

0 commit comments

Comments
 (0)