From 56c21cb57adee19e8fcea3396d4f44a3fa20b861 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 8 Feb 2018 14:19:59 +0100 Subject: [PATCH] bucketExists: Use a bool to indicate if a bucket exists or not This is a breaking API change. bucketExists API will require a callback with this signature cb(err, bool). This is a better way to indicate whether a bucket exists or not. --- docs/API.md | 14 +++++++------- examples/bucket-exists.js | 10 ++++++---- src/main/minio.js | 8 +++++++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/docs/API.md b/docs/API.md index 6e343192f..107f3301a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -162,7 +162,7 @@ minioClient.listBuckets(function(err, buckets) { ``` -#### bucketExists(bucketName[, callback]) +#### bucketExists(bucketName, callback) Checks if a bucket exists. @@ -173,19 +173,19 @@ __Parameters__ | Param | Type | Description | |---|---|---| | `bucketName` | _string_ | Name of the bucket. | -| `callback(err)` | _function_ | `err` is `null` if the bucket exists. `err.code` is `NoSuchBucket` in case the bucket does not exist. If no callback is passed, a `Promise` is returned. | +| `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. | __Example__ ```js -minioClient.bucketExists('mybucket', function(err) { +minioClient.bucketExists('mybucket', function(err, exists) { if (err) { - if (err.code == 'NoSuchBucket') return console.log("bucket does not exist.") - return console.log(err) + return console.log(err) + } + if (exists) { + return console.log('Bucket exists.') } - // if err is null it indicates that the bucket exists. - console.log('Bucket exists.') }) ``` diff --git a/examples/bucket-exists.js b/examples/bucket-exists.js index 34387239f..c6b415dbf 100644 --- a/examples/bucket-exists.js +++ b/examples/bucket-exists.js @@ -26,9 +26,11 @@ var s3Client = new Minio.Client({ secretKey: 'YOUR-SECRETACCESSKEY' }) -s3Client.bucketExists('my-bucketname', function(e) { - if (e) { - return console.log(e) +s3Client.bucketExists('my-bucketname', function(err, exists) { + if (err) { + return console.log(err) + } + if (exists) { + console.log("Bucket exists.") } - console.log("True") }) diff --git a/src/main/minio.js b/src/main/minio.js index 16cdf25e6..30d9cb7eb 100644 --- a/src/main/minio.js +++ b/src/main/minio.js @@ -647,7 +647,13 @@ export class Client { throw new TypeError('callback should be of type "function"') } var method = 'HEAD' - this.makeRequest({method, bucketName}, '', 200, '', false, cb) + 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 a bucket.