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

refactor: bucketExists #1223

Merged
merged 18 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
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 @@
}
}

/**
* 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 Expand Up @@ -1500,7 +1521,7 @@

const chunkier = new BlockStream2({ size: partSize, zeroPadding: false })

const [_, o] = await Promise.all([

Check warning on line 1524 in src/internal/client.ts

View workflow job for this annotation

GitHub Actions / lint

'_' is assigned a value but never used
new Promise((resolve, reject) => {
body.pipe(chunkier).on('error', reject)
chunkier.on('end', resolve).on('error', reject)
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
30 changes: 3 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,13 @@ 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.removeObject = callbackify(Client.prototype.removeObject)
kaankabalak marked this conversation as resolved.
Show resolved Hide resolved
trim21 marked this conversation as resolved.
Show resolved Hide resolved
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 +1809,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
Loading