Skip to content

Commit

Permalink
refactor: bucketExists (#1223)
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Nov 30, 2023
1 parent cbf83dd commit 5573400
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 8 additions & 13 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,28 +212,23 @@ try {

<a name="bucketExists"></a>

#### bucketExists(bucketName[, callback])
#### async bucketExists(bucketName): Promise<boolean>

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

<a name="removeBucket"></a>
Expand Down
12 changes: 2 additions & 10 deletions docs/zh_CN/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ minioClient.listBuckets(function(err, buckets) {
```

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

验证存储桶是否存在。

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

<a name="removeBucket"></a>
Expand Down
12 changes: 4 additions & 8 deletions examples/bucket-exists.js → examples/bucket-exists.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
21 changes: 21 additions & 0 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,27 @@ export class TypedClient {
}
}

/**
* To check if a bucket already exists.
*/
async bucketExists(bucketName: string): Promise<boolean> {
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<void>

/**
Expand Down
3 changes: 0 additions & 3 deletions src/minio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ export class TargetConfig {

// Exports from library
export class Client extends TypedClient {
bucketExists(bucketName: string, callback: ResultCallback<boolean>): void
bucketExists(bucketName: string): Promise<boolean>

listObjects(bucketName: string, prefix?: string, recursive?: boolean): BucketStream<BucketItem>

listObjectsV2(bucketName: string, prefix?: string, recursive?: boolean, startAfter?: string): BucketStream<BucketItem>
Expand Down
29 changes: 2 additions & 27 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5573400

Please sign in to comment.