Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bucketExists: Use a bool to indicate if a bucket exists or not #666

Merged
merged 1 commit into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minio",
"version": "4.0.1",
"version": "5.0.0",
"description": "S3 Compatible Cloud Storage client",
"main": "./dist/main/minio.js",
"scripts": {
Expand Down
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
4 changes: 2 additions & 2 deletions src/test/functional/functional-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ describe('functional tests', function() {
describe('bucketExists', () => {
step(`bucketExists(bucketName, cb)_bucketName:${bucketName}_`, done => client.bucketExists(bucketName, done))
step(`bucketExists(bucketName, cb)_bucketName:${bucketName}random_`, done => {
client.bucketExists(bucketName + 'random', (e) => {
if (e.code === 'NoSuchBucket') return done()
client.bucketExists(bucketName + 'random', (e, exists) => {
if (e === null && !exists) return done()
done(new Error())
})
})
Expand Down