Skip to content

Commit

Permalink
bucketExists: Use a bool to indicate if a bucket exists or not
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vadmeste committed Feb 8, 2018
1 parent 3bb114f commit 56c21cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
14 changes: 7 additions & 7 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ minioClient.listBuckets(function(err, buckets) {
```

<a name="bucketExists"></a>
#### bucketExists(bucketName[, callback])
#### bucketExists(bucketName, callback)

Checks if a bucket exists.

Expand All @@ -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.')
})
```

Expand Down
10 changes: 6 additions & 4 deletions examples/bucket-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
8 changes: 7 additions & 1 deletion src/main/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 56c21cb

Please sign in to comment.