Skip to content

Commit df8222c

Browse files
trim21prakashsvmx
andauthored
refactor statObject (#1187)
* refactor * refactor * up * up * up * up * format code * up * error message * unify import name * Update docs/API.md --------- Co-authored-by: Prakash Senthil Vel <[email protected]>
1 parent 7efb0ac commit df8222c

File tree

9 files changed

+113
-140
lines changed

9 files changed

+113
-140
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ The full API Reference is available here.
201201
- [get-partialobject.js](https://github.com/minio/minio-js/blob/master/examples/get-partialobject.js)
202202
- [remove-object.js](https://github.com/minio/minio-js/blob/master/examples/remove-object.js)
203203
- [remove-incomplete-upload.js](https://github.com/minio/minio-js/blob/master/examples/remove-incomplete-upload.js)
204-
- [stat-object.js](https://github.com/minio/minio-js/blob/master/examples/stat-object.js)
204+
- [stat-object.mjs](https://github.com/minio/minio-js/blob/master/examples/stat-object.mjs)
205205
- [get-object-retention.js](https://github.com/minio/minio-js/blob/master/examples/get-object-retention.js)
206206
- [put-object-retention.js](https://github.com/minio/minio-js/blob/master/examples/put-object-retention.js)
207207
- [put-object-tagging.js](https://github.com/minio/minio-js/blob/master/examples/put-object-tagging.js)

README_zh_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mc ls play/europetrip/
170170
* [get-partialobject.js](https://github.com/minio/minio-js/blob/master/examples/get-partialobject.js)
171171
* [remove-object.js](https://github.com/minio/minio-js/blob/master/examples/remove-object.js)
172172
* [remove-incomplete-upload.js](https://github.com/minio/minio-js/blob/master/examples/remove-incomplete-upload.js)
173-
* [stat-object.js](https://github.com/minio/minio-js/blob/master/examples/stat-object.js)
173+
* [stat-object.mjs](https://github.com/minio/minio-js/blob/master/examples/stat-object.mjs)
174174

175175
#### 完整示例 : Presigned操作
176176
* [presigned-getobject.js](https://github.com/minio/minio-js/blob/master/examples/presigned-getobject.js)

docs/API.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,23 +1274,15 @@ Gets metadata of an object.
12741274
**Example**
12751275

12761276
```js
1277-
minioClient.statObject('mybucket', 'photo.jpg', function (err, stat) {
1278-
if (err) {
1279-
return console.log(err)
1280-
}
1281-
console.log(stat)
1282-
})
1277+
const stat = await minioClient.statObject('mybucket', 'photo.jpg')
1278+
console.log(stat)
12831279
```
12841280

12851281
**Example stat on a version of an object**
12861282

12871283
```js
1288-
minioClient.statObject('mybucket', 'photo.jpg', { versionId: 'my-versionId' }, function (err, stat) {
1289-
if (err) {
1290-
return console.log(err)
1291-
}
1292-
console.log(stat)
1293-
})
1284+
const stat = await minioClient.statObject('mybucket', 'photo.jpg', { versionId: 'my-versionId' })
1285+
console.log(stat)
12941286
```
12951287

12961288
<a name="removeObject"></a>
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@
1717
// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname
1818
// and my-objectname are dummy values, please replace them with original values.
1919

20-
var Minio = require('minio')
20+
import * as Minio from 'minio'
2121

22-
var s3Client = new Minio.Client({
22+
const s3Client = new Minio.Client({
2323
endPoint: 's3.amazonaws.com',
2424
accessKey: 'YOUR-ACCESSKEYID',
2525
secretKey: 'YOUR-SECRETACCESSKEY',
2626
})
2727
// Get stat information for my-objectname.
28-
s3Client.statObject('my-bucketname', 'my-objectname', function (e, stat) {
29-
if (e) {
30-
return console.log(e)
31-
}
28+
try {
29+
const stat = await s3Client.statObject('my-bucketname', 'my-objectname')
3230
console.log(stat)
33-
})
31+
} catch (e) {
32+
console.log(e)
33+
}
3434

3535
// Get stat information for a specific version of 'my-objectname'
3636
//Bucket must be versioning enabled.
37-
s3Client.statObject('my-bucketname', 'my-objectname', { versionId: 'my-uuid' }, function (e, stat) {
38-
if (e) {
39-
return console.log(e)
40-
}
37+
try {
38+
const stat = await s3Client.statObject('my-bucketname', 'my-objectname', { versionId: 'my-uuid' })
4139
console.log(stat)
42-
})
40+
} catch (e) {
41+
console.log(e)
42+
}

src/internal/client.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { DEFAULT_REGION } from '../helpers.ts'
1212
import { signV4 } from '../signing.ts'
1313
import { Extensions } from './extensions.ts'
1414
import {
15+
extractMetadata,
16+
getVersionId,
1517
isAmazonEndpoint,
1618
isBoolean,
1719
isDefined,
@@ -26,6 +28,7 @@ import {
2628
isValidPort,
2729
isVirtualHostStyle,
2830
makeDateLong,
31+
sanitizeETag,
2932
toSha256,
3033
uriEscape,
3134
uriResourceEscape,
@@ -34,7 +37,16 @@ import { request } from './request.ts'
3437
import { drainResponse, readAsString } from './response.ts'
3538
import type { Region } from './s3-endpoints.ts'
3639
import { getS3Endpoint } from './s3-endpoints.ts'
37-
import type { Binary, BucketItemFromList, IRequest, RequestHeaders, Transport } from './type.ts'
40+
import type {
41+
Binary,
42+
BucketItemFromList,
43+
BucketItemStat,
44+
IRequest,
45+
RequestHeaders,
46+
ResponseHeader,
47+
Transport,
48+
} from './type.ts'
49+
import type { StatObjectOpts } from './type.ts'
3850
import type { UploadedPart } from './xml-parser.ts'
3951
import * as xmlParsers from './xml-parser.ts'
4052

@@ -779,6 +791,34 @@ export class TypedClient {
779791
delete this.regionMap[bucketName]
780792
}
781793

794+
/**
795+
* Stat information of the object.
796+
*/
797+
async statObject(bucketName: string, objectName: string, statOpts: StatObjectOpts = {}): Promise<BucketItemStat> {
798+
if (!isValidBucketName(bucketName)) {
799+
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
800+
}
801+
if (!isValidObjectName(objectName)) {
802+
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
803+
}
804+
805+
if (!isObject(statOpts)) {
806+
throw new errors.InvalidArgumentError('statOpts should be of type "object"')
807+
}
808+
809+
const query = qs.stringify(statOpts)
810+
const method = 'HEAD'
811+
const res = await this.makeRequestAsyncOmit({ method, bucketName, objectName, query })
812+
813+
return {
814+
size: parseInt(res.headers['content-length'] as string),
815+
metaData: extractMetadata(res.headers as ResponseHeader),
816+
lastModified: new Date(res.headers['last-modified'] as string),
817+
versionId: getVersionId(res.headers as ResponseHeader),
818+
etag: sanitizeETag(res.headers.etag),
819+
}
820+
}
821+
782822
/**
783823
* Remove the specified object.
784824
* @deprecated use new promise style API

src/internal/type.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type * as http from 'node:http'
22
import type { Readable as ReadableStream } from 'node:stream'
33

4-
import type { MetadataItem } from '../minio'
5-
64
export type Binary = string | Buffer
75

86
// nodejs IncomingHttpHeaders is Record<string, string | string[]>, but it's actually this:
@@ -66,6 +64,11 @@ export interface IncompleteUploadedBucketItem {
6664
size: number
6765
}
6866

67+
export interface MetadataItem {
68+
Key: string
69+
Value: string
70+
}
71+
6972
export interface ItemBucketMetadataList {
7073
Items: MetadataItem[]
7174
}
@@ -111,3 +114,15 @@ export interface BucketStream<T> extends ReadableStream {
111114
// eslint-disable-next-line @typescript-eslint/no-explicit-any
112115
on(event: string | symbol, listener: (...args: any[]) => void): this
113116
}
117+
118+
export interface BucketItemStat {
119+
size: number
120+
etag: string
121+
lastModified: Date
122+
metaData: ItemBucketMetadata
123+
versionId?: string | null
124+
}
125+
126+
export type StatObjectOpts = {
127+
versionId?: string
128+
}

src/minio.d.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ import type {
2020
BucketItem,
2121
BucketItemCopy,
2222
BucketItemFromList,
23+
BucketItemStat,
2324
BucketItemWithMetadata,
2425
BucketStream,
2526
IncompleteUploadedBucketItem,
2627
ItemBucketMetadata,
2728
ItemBucketMetadataList,
29+
MetadataItem,
2830
} from './internal/type.ts'
2931

3032
export * from './helpers.ts'
@@ -34,12 +36,14 @@ export type {
3436
BucketItem,
3537
BucketItemCopy,
3638
BucketItemFromList,
39+
BucketItemStat,
3740
BucketItemWithMetadata,
3841
BucketStream,
3942
ClientOptions,
4043
IncompleteUploadedBucketItem,
4144
ItemBucketMetadata,
4245
ItemBucketMetadataList,
46+
MetadataItem,
4347
NoResultCallback,
4448
RemoveOptions,
4549
}
@@ -89,25 +93,13 @@ export type Encryption = EncryptionConfig | EmptyObject
8993
export type Retention = RetentionOptions | EmptyObject
9094
export type IsoDate = string
9195

92-
export interface BucketItemStat {
93-
size: number
94-
etag: string
95-
lastModified: Date
96-
metaData: ItemBucketMetadata
97-
}
98-
9996
export interface PostPolicyResult {
10097
postURL: string
10198
formData: {
10299
[key: string]: any
103100
}
104101
}
105102

106-
export interface MetadataItem {
107-
Key: string
108-
Value: string
109-
}
110-
111103
export interface UploadedObjectInfo {
112104
etag: string
113105
versionId: string | null
@@ -375,9 +367,6 @@ export class Client extends TypedClient {
375367
conditions: CopyConditions,
376368
): Promise<BucketItemCopy>
377369

378-
statObject(bucketName: string, objectName: string, callback: ResultCallback<BucketItemStat>): void
379-
statObject(bucketName: string, objectName: string): Promise<BucketItemStat>
380-
381370
removeObjects(bucketName: string, objectsList: string[], callback: NoResultCallback): void
382371
removeObjects(bucketName: string, objectsList: string[]): Promise<void>
383372

src/minio.js

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,61 +1048,6 @@ export class Client extends TypedClient {
10481048
return readStream
10491049
}
10501050

1051-
// Stat information of the object.
1052-
//
1053-
// __Arguments__
1054-
// * `bucketName` _string_: name of the bucket
1055-
// * `objectName` _string_: name of the object
1056-
// * `statOpts` _object_ : Version of the object in the form `{versionId:'my-uuid'}`. Default is `{}`. (optional).
1057-
// * `callback(err, stat)` _function_: `err` is not `null` in case of error, `stat` contains the object information:
1058-
// * `stat.size` _number_: size of the object
1059-
// * `stat.etag` _string_: etag of the object
1060-
// * `stat.metaData` _string_: MetaData of the object
1061-
// * `stat.lastModified` _Date_: modified time stamp
1062-
// * `stat.versionId` _string_: version id of the object if available
1063-
statObject(bucketName, objectName, statOpts = {}, cb) {
1064-
if (!isValidBucketName(bucketName)) {
1065-
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
1066-
}
1067-
if (!isValidObjectName(objectName)) {
1068-
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
1069-
}
1070-
// backward compatibility
1071-
if (isFunction(statOpts)) {
1072-
cb = statOpts
1073-
statOpts = {}
1074-
}
1075-
1076-
if (!isObject(statOpts)) {
1077-
throw new errors.InvalidArgumentError('statOpts should be of type "object"')
1078-
}
1079-
if (!isFunction(cb)) {
1080-
throw new TypeError('callback should be of type "function"')
1081-
}
1082-
1083-
var query = querystring.stringify(statOpts)
1084-
var method = 'HEAD'
1085-
this.makeRequest({ method, bucketName, objectName, query }, '', [200], '', true, (e, response) => {
1086-
if (e) {
1087-
return cb(e)
1088-
}
1089-
1090-
// We drain the socket so that the connection gets closed. Note that this
1091-
// is not expensive as the socket will not have any data.
1092-
response.on('data', () => {})
1093-
1094-
const result = {
1095-
size: +response.headers['content-length'],
1096-
metaData: extractMetadata(response.headers),
1097-
lastModified: new Date(response.headers['last-modified']),
1098-
versionId: getVersionId(response.headers),
1099-
etag: sanitizeETag(response.headers.etag),
1100-
}
1101-
1102-
cb(null, result)
1103-
})
1104-
}
1105-
11061051
// Remove all the objects residing in the objectsList.
11071052
//
11081053
// __Arguments__
@@ -2811,7 +2756,6 @@ Client.prototype.fGetObject = promisify(Client.prototype.fGetObject)
28112756
Client.prototype.putObject = promisify(Client.prototype.putObject)
28122757
Client.prototype.fPutObject = promisify(Client.prototype.fPutObject)
28132758
Client.prototype.copyObject = promisify(Client.prototype.copyObject)
2814-
Client.prototype.statObject = promisify(Client.prototype.statObject)
28152759
Client.prototype.removeObjects = promisify(Client.prototype.removeObjects)
28162760

28172761
Client.prototype.presignedUrl = promisify(Client.prototype.presignedUrl)
@@ -2851,6 +2795,7 @@ Client.prototype.selectObjectContent = promisify(Client.prototype.selectObjectCo
28512795

28522796
// refactored API use promise internally
28532797
Client.prototype.removeObject = callbackify(Client.prototype.removeObject)
2798+
Client.prototype.statObject = callbackify(Client.prototype.statObject)
28542799
Client.prototype.removeBucket = callbackify(Client.prototype.removeBucket)
28552800
Client.prototype.listBuckets = callbackify(Client.prototype.listBuckets)
28562801
Client.prototype.removeBucketReplication = callbackify(Client.prototype.removeBucketReplication)

0 commit comments

Comments
 (0)