From 55734002e24c6e35ebfef8a532734a41d30a44da Mon Sep 17 00:00:00 2001 From: Trim21 Date: Thu, 30 Nov 2023 10:00:17 +0800 Subject: [PATCH] refactor: `bucketExists` (#1223) --- README.md | 2 +- README_zh_CN.md | 2 +- docs/API.md | 21 +++++--------- docs/zh_CN/API.md | 12 ++------ .../{bucket-exists.js => bucket-exists.mjs} | 12 +++----- src/internal/client.ts | 21 ++++++++++++++ src/minio.d.ts | 3 -- src/minio.js | 29 ++----------------- 8 files changed, 39 insertions(+), 63 deletions(-) rename examples/{bucket-exists.js => bucket-exists.mjs} (85%) diff --git a/README.md b/README.md index 2a6471fe..59480b27 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ The full API Reference is available here. - [list-objects.js](https://github.com/minio/minio-js/blob/master/examples/list-objects.js) - [list-objects-v2.js](https://github.com/minio/minio-js/blob/master/examples/list-objects-v2.js) - [list-objects-v2-with-metadata.js](https://github.com/minio/minio-js/blob/master/examples/list-objects-v2-with-metadata.js) (Extension) -- [bucket-exists.js](https://github.com/minio/minio-js/blob/master/examples/bucket-exists.js) +- [bucket-exists.mjs](https://github.com/minio/minio-js/blob/master/examples/bucket-exists.mjs) - [make-bucket.js](https://github.com/minio/minio-js/blob/master/examples/make-bucket.js) - [remove-bucket.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-bucket.mjs) - [list-incomplete-uploads.js](https://github.com/minio/minio-js/blob/master/examples/list-incomplete-uploads.js) diff --git a/README_zh_CN.md b/README_zh_CN.md index 34928c07..0de9c642 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -154,7 +154,7 @@ mc ls play/europetrip/ * [list-buckets.js](https://github.com/minio/minio-js/blob/master/examples/list-buckets.js) * [list-objects.js](https://github.com/minio/minio-js/blob/master/examples/list-objects.js) * [list-objects-v2.js](https://github.com/minio/minio-js/blob/master/examples/list-objects-v2.js) -* [bucket-exists.js](https://github.com/minio/minio-js/blob/master/examples/bucket-exists.js) +* [bucket-exists.mjs](https://github.com/minio/minio-js/blob/master/examples/bucket-exists.mjs) * [make-bucket.js](https://github.com/minio/minio-js/blob/master/examples/make-bucket.js) * [remove-bucket.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-bucket.mjs) * [list-incomplete-uploads.js](https://github.com/minio/minio-js/blob/master/examples/list-incomplete-uploads.js) diff --git a/docs/API.md b/docs/API.md index b6c29248..7170998f 100644 --- a/docs/API.md +++ b/docs/API.md @@ -212,28 +212,23 @@ try { -#### bucketExists(bucketName[, callback]) +#### async bucketExists(bucketName): Promise Checks if a bucket exists. **Parameters** -| Param | Type | Description | -| ----------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `bucketName` | _string_ | Name of the bucket. | -| `callback(err, exists)` | _function_ | `exists` is a boolean which indicates whether `bucketName` exists or not. `err` is set when an error occurs during the operation. If no callback is passed, a `Promise` is returned. | +| Param | Type | Description | +| ------------ | -------- | ------------------- | +| `bucketName` | _string_ | Name of the bucket. | **Example** ```js -minioClient.bucketExists('mybucket', function (err, exists) { - if (err) { - return console.log(err) - } - if (exists) { - return console.log('Bucket exists.') - } -}) +const exists = await minioClient.bucketExists('mybucket') +if (exists) { + return console.log('Bucket exists.') +} ``` diff --git a/docs/zh_CN/API.md b/docs/zh_CN/API.md index c15a3c8b..7d7d8c17 100644 --- a/docs/zh_CN/API.md +++ b/docs/zh_CN/API.md @@ -165,7 +165,7 @@ minioClient.listBuckets(function(err, buckets) { ``` -#### bucketExists(bucketName[, callback]) +#### async bucketExists(bucketName): Promise 验证存储桶是否存在。 @@ -176,20 +176,12 @@ __参数__ | 参数| 类型 | 描述 | |---|---|---| | `bucketName` | _string_ | 存储桶名称。 | -| `callback(err)` | _function_ | 如果存储桶存在的话`err`就是null,否则`err.code`是`NoSuchBucket`。如果没有传callback的话,则返回一个`Promise`对象。 | __示例__ ```js -minioClient.bucketExists('mybucket', function(err) { - if (err) { - if (err.code == 'NoSuchBucket') return console.log("bucket does not exist.") - return console.log(err) - } - // if err is null it indicates that the bucket exists. - console.log('Bucket exists.') -}) +await minioClient.bucketExists('mybucket') ``` diff --git a/examples/bucket-exists.js b/examples/bucket-exists.mjs similarity index 85% rename from examples/bucket-exists.js rename to examples/bucket-exists.mjs index 1a9d7481..fb6e5321 100644 --- a/examples/bucket-exists.js +++ b/examples/bucket-exists.mjs @@ -25,11 +25,7 @@ const s3Client = new Minio.Client({ secretKey: 'YOUR-SECRETACCESSKEY', }) -s3Client.bucketExists('my-bucketname', function (err, exists) { - if (err) { - return console.log(err) - } - if (exists) { - console.log('Bucket exists.') - } -}) +const exists = await s3Client.bucketExists('my-bucketname') +if (exists) { + console.log('Bucket exists.') +} diff --git a/src/internal/client.ts b/src/internal/client.ts index 0046c13b..41cd3413 100644 --- a/src/internal/client.ts +++ b/src/internal/client.ts @@ -895,6 +895,27 @@ export class TypedClient { } } + /** + * To check if a bucket already exists. + */ + async bucketExists(bucketName: string): Promise { + if (!isValidBucketName(bucketName)) { + throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) + } + const method = 'HEAD' + try { + await this.makeRequestAsyncOmit({ method, bucketName }) + } catch (err) { + // @ts-ignore + if (err.code === 'NoSuchBucket' || err.code === 'NotFound') { + return false + } + throw err + } + + return true + } + async removeBucket(bucketName: string): Promise /** diff --git a/src/minio.d.ts b/src/minio.d.ts index 3f1d35dd..590a2f19 100644 --- a/src/minio.d.ts +++ b/src/minio.d.ts @@ -219,9 +219,6 @@ export class TargetConfig { // Exports from library export class Client extends TypedClient { - bucketExists(bucketName: string, callback: ResultCallback): void - bucketExists(bucketName: string): Promise - listObjects(bucketName: string, prefix?: string, recursive?: boolean): BucketStream listObjectsV2(bucketName: string, prefix?: string, recursive?: boolean, startAfter?: string): BucketStream diff --git a/src/minio.js b/src/minio.js index f4d0a7d2..b0a698d1 100644 --- a/src/minio.js +++ b/src/minio.js @@ -91,30 +91,6 @@ export class Client extends TypedClient { this.userAgent = `${this.userAgent} ${appName}/${appVersion}` } - // To check if a bucket already exists. - // - // __Arguments__ - // * `bucketName` _string_ : name of the bucket - // * `callback(err)` _function_ : `err` is `null` if the bucket exists - bucketExists(bucketName, cb) { - if (!isValidBucketName(bucketName)) { - throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) - } - if (!isFunction(cb)) { - throw new TypeError('callback should be of type "function"') - } - var method = 'HEAD' - this.makeRequest({ method, bucketName }, '', [200], '', false, (err) => { - if (err) { - if (err.code == 'NoSuchBucket' || err.code == 'NotFound') { - return cb(null, false) - } - return cb(err) - } - cb(null, true) - }) - } - // Remove the partially uploaded object. // // __Arguments__ @@ -1784,8 +1760,6 @@ export class Client extends TypedClient { } // Promisify various public-facing APIs on the Client module. -Client.prototype.bucketExists = promisify(Client.prototype.bucketExists) - Client.prototype.getObject = promisify(Client.prototype.getObject) Client.prototype.getPartialObject = promisify(Client.prototype.getPartialObject) Client.prototype.fGetObject = promisify(Client.prototype.fGetObject) @@ -1817,10 +1791,12 @@ Client.prototype.selectObjectContent = promisify(Client.prototype.selectObjectCo // refactored API use promise internally Client.prototype.makeBucket = callbackify(Client.prototype.makeBucket) +Client.prototype.bucketExists = callbackify(Client.prototype.bucketExists) Client.prototype.removeBucket = callbackify(Client.prototype.removeBucket) Client.prototype.listBuckets = callbackify(Client.prototype.listBuckets) Client.prototype.statObject = callbackify(Client.prototype.statObject) +Client.prototype.putObjectRetention = callbackify(Client.prototype.putObjectRetention) Client.prototype.putObject = callbackify(Client.prototype.putObject) Client.prototype.fPutObject = callbackify(Client.prototype.fPutObject) Client.prototype.removeObject = callbackify(Client.prototype.removeObject) @@ -1832,7 +1808,6 @@ Client.prototype.getObjectLegalHold = callbackify(Client.prototype.getObjectLega Client.prototype.setObjectLegalHold = callbackify(Client.prototype.setObjectLegalHold) Client.prototype.getBucketTagging = callbackify(Client.prototype.getBucketTagging) Client.prototype.getObjectTagging = callbackify(Client.prototype.getObjectTagging) -Client.prototype.putObjectRetention = callbackify(Client.prototype.putObjectRetention) Client.prototype.setObjectLockConfig = callbackify(Client.prototype.setObjectLockConfig) Client.prototype.getObjectLockConfig = callbackify(Client.prototype.getObjectLockConfig) Client.prototype.setBucketPolicy = callbackify(Client.prototype.setBucketPolicy)