Skip to content

Commit

Permalink
refactor removeBucket (minio#1177)
Browse files Browse the repository at this point in the history
* init

* fix test

* fix test

* Update API.md

* Update API.md

* Update API.md

* Update API.md

* update example
  • Loading branch information
trim21 authored Jul 10, 2023
1 parent 6cf40a7 commit f6a34ca
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ The full API Reference is available here.
- [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)
- [make-bucket.js](https://github.com/minio/minio-js/blob/master/examples/make-bucket.js)
- [remove-bucket.js](https://github.com/minio/minio-js/blob/master/examples/remove-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)
- [get-bucket-versioning.js](https://github.com/minio/minio-js/blob/master/examples/get-bucket-versioning.js)
- [set-bucket-versioning.js](https://github.com/minio/minio-js/blob/master/examples/set-bucket-versioning.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 @@ -156,7 +156,7 @@ mc ls play/europetrip/
* [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)
* [make-bucket.js](https://github.com/minio/minio-js/blob/master/examples/make-bucket.js)
* [remove-bucket.js](https://github.com/minio/minio-js/blob/master/examples/remove-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
10 changes: 6 additions & 4 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var s3Client = new Minio.Client({
})
```

### _Note_: The below examples rely on top level await .
### _Note_: The below examples may rely on top level await.

## 2. Bucket operations

Expand Down Expand Up @@ -255,10 +255,12 @@ Removes a bucket.
**Example**

```js
minioClient.removeBucket('mybucket', function (err) {
if (err) return console.log('unable to remove bucket.')
try {
await minioClient.removeBucket('mybucket')
console.log('Bucket removed successfully.')
})
} catch (err) {
console.log('unable to remove bucket.')
}
```

<a name="listObjects"></a>
Expand Down
14 changes: 7 additions & 7 deletions examples/remove-bucket.js → examples/remove-bucket.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname
// are dummy values, please replace them with original values.

var Minio = require('minio')
import * as Minio from 'minio'

var s3Client = new Minio.Client({
const s3Client = new Minio.Client({
endPoint: 's3.amazonaws.com',
accessKey: 'YOUR-ACCESSKEYID',
secretKey: 'YOUR-SECRETACCESSKEY',
})

// Remove a bucket name my-bucketname.
// This operation will only work if your bucket is empty.
s3Client.removeBucket('my-bucketname', function (e) {
if (e) {
return console.log(e)
}
try {
await s3Client.removeBucket('my-bucketname')
console.log('Success')
})
} catch (e) {
return console.log(e)
}
16 changes: 16 additions & 0 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,22 @@ export class TypedClient {
)
}

async removeBucket(bucketName: string): Promise<void>

/**
* @deprecated use promise style API
*/
removeBucket(bucketName: string, callback: NoResultCallback): void

async removeBucket(bucketName: string): Promise<void> {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
const method = 'DELETE'
await this.makeRequestAsyncOmit({ method, bucketName }, '', [204])
delete this.regionMap[bucketName]
}

/**
* Remove the specified object.
* @deprecated use new promise style API
Expand Down
3 changes: 0 additions & 3 deletions src/minio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ export class Client extends TypedClient {
bucketExists(bucketName: string, callback: ResultCallback<boolean>): void
bucketExists(bucketName: string): Promise<boolean>

removeBucket(bucketName: string, callback: NoResultCallback): void
removeBucket(bucketName: string): Promise<void>

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

listObjectsV2(bucketName: string, prefix?: string, recursive?: boolean, startAfter?: string): BucketStream<BucketItem>
Expand Down
24 changes: 1 addition & 23 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,28 +308,6 @@ export class Client extends TypedClient {
})
}

// Remove a bucket.
//
// __Arguments__
// * `bucketName` _string_ : name of the bucket
// * `callback(err)` _function_ : `err` is `null` if the bucket is removed successfully.
removeBucket(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 = 'DELETE'
this.makeRequest({ method, bucketName }, '', [204], '', false, (e) => {
// If the bucket was successfully removed, remove the region map entry.
if (!e) {
delete this.regionMap[bucketName]
}
cb(e)
})
}

// Remove the partially uploaded object.
//
// __Arguments__
Expand Down Expand Up @@ -2835,7 +2813,6 @@ export class Client extends TypedClient {
// Promisify various public-facing APIs on the Client module.
Client.prototype.makeBucket = promisify(Client.prototype.makeBucket)
Client.prototype.bucketExists = promisify(Client.prototype.bucketExists)
Client.prototype.removeBucket = promisify(Client.prototype.removeBucket)

Client.prototype.getObject = promisify(Client.prototype.getObject)
Client.prototype.getPartialObject = promisify(Client.prototype.getPartialObject)
Expand Down Expand Up @@ -2884,4 +2861,5 @@ Client.prototype.selectObjectContent = promisify(Client.prototype.selectObjectCo

// refactored API use promise internally
Client.prototype.removeObject = callbackify(Client.prototype.removeObject)
Client.prototype.removeBucket = callbackify(Client.prototype.removeBucket)
Client.prototype.listBuckets = callbackify(Client.prototype.listBuckets)
50 changes: 25 additions & 25 deletions tests/functional/functional-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('functional tests', function () {
}

before((done) => client.makeBucket(bucketName, server_region, done))
after((done) => client.removeBucket(bucketName, done))
after(() => client.removeBucket(bucketName))

if (traceStream) {
after(() => {
Expand Down Expand Up @@ -1920,7 +1920,7 @@ describe('functional tests', function () {
// Isolate the bucket/object for easy debugging and tracking.
const versionedBucketName = 'minio-js-test-version-' + uuid.v4()
before((done) => client.makeBucket(versionedBucketName, '', done))
after((done) => client.removeBucket(versionedBucketName, done))
after(() => client.removeBucket(versionedBucketName))

describe('Versioning Steps test', function () {
step('Check if versioning is enabled on a bucket', (done) => {
Expand Down Expand Up @@ -1981,7 +1981,7 @@ describe('functional tests', function () {
: Buffer.alloc(100 * 1024, 0)

before((done) => client.makeBucket(versionedBucketName, '', done))
after((done) => client.removeBucket(versionedBucketName, done))
after(() => client.removeBucket(versionedBucketName))

describe('Versioning Steps test', function () {
let versionId
Expand Down Expand Up @@ -2056,7 +2056,7 @@ describe('functional tests', function () {
: Buffer.alloc(100 * 1024, 0)

before((done) => client.makeBucket(versionedBucketName, '', done))
after((done) => client.removeBucket(versionedBucketName, done))
after(() => client.removeBucket(versionedBucketName))

describe('Versioning Test for getObject, getPartialObject, putObject, removeObject with versionId support', function () {
let versionId = null
Expand Down Expand Up @@ -2217,7 +2217,7 @@ describe('functional tests', function () {
})
}),
)
after((done) => client.removeBucket(versionedBucketName, done))
after(() => client.removeBucket(versionedBucketName))

step(
`putObject(bucketName, objectName, stream, size, metaData, callback)_bucketName:${versionedBucketName}, stream:1b, size:1_Create ${listObjectsNum} objects`,
Expand Down Expand Up @@ -2320,7 +2320,7 @@ describe('functional tests', function () {
: Buffer.alloc(100 * 1024, 0)

before((done) => client.makeBucket(versionedBucketName, '', done))
after((done) => client.removeBucket(versionedBucketName, done))
after(() => client.removeBucket(versionedBucketName))

describe('Test for removal of multiple versions', function () {
let isVersioningSupported = false
Expand Down Expand Up @@ -2419,7 +2419,7 @@ describe('functional tests', function () {
// Isolate the bucket/object for easy debugging and tracking.
const tagsBucketName = 'minio-js-test-tags-' + uuid.v4()
before((done) => client.makeBucket(tagsBucketName, '', done))
after((done) => client.removeBucket(tagsBucketName, done))
after(() => client.removeBucket(tagsBucketName))

describe('set, get and remove Tags on a bucket', function () {
step(`Set tags on a bucket_bucketName:${tagsBucketName}`, (done) => {
Expand Down Expand Up @@ -2465,7 +2465,7 @@ describe('functional tests', function () {
// Isolate the bucket/object for easy debugging and tracking.
const tagsBucketName = 'minio-js-test-tags-' + uuid.v4()
before((done) => client.makeBucket(tagsBucketName, '', done))
after((done) => client.removeBucket(tagsBucketName, done))
after(() => client.removeBucket(tagsBucketName))

const tagObjName = 'datafile-100-kB'
const tagObject = Buffer.alloc(100 * 1024, 0)
Expand Down Expand Up @@ -2530,7 +2530,7 @@ describe('functional tests', function () {
// Isolate the bucket/object for easy debugging and tracking.
const tagsVersionedBucketName = 'minio-js-test-tags-version-' + uuid.v4()
before((done) => client.makeBucket(tagsVersionedBucketName, '', done))
after((done) => client.removeBucket(tagsVersionedBucketName, done))
after(() => client.removeBucket(tagsVersionedBucketName))

const tagObjName = 'datafile-100-kB'
const tagObject = Buffer.alloc(100 * 1024, 0)
Expand Down Expand Up @@ -2637,7 +2637,7 @@ describe('functional tests', function () {
describe('Bucket Lifecycle API', () => {
const bucketName = 'minio-js-test-lifecycle-' + uuid.v4()
before((done) => client.makeBucket(bucketName, '', done))
after((done) => client.removeBucket(bucketName, done))
after(() => client.removeBucket(bucketName))

describe('Set, Get Lifecycle config Tests', function () {
step(`Set lifecycle config on a bucket:_bucketName:${bucketName}`, (done) => {
Expand Down Expand Up @@ -2725,7 +2725,7 @@ describe('functional tests', function () {
})
}),
)
after((done) => client.removeBucket(versionedBucketName, done))
after(() => client.removeBucket(versionedBucketName))

step(
`presignedPutObject(bucketName, objectName, expires=1000, cb)_bucketName:${versionedBucketName} ${versionedPresignObjName} _version:1`,
Expand Down Expand Up @@ -3191,7 +3191,7 @@ describe('functional tests', function () {
// this is not supported in gateway mode.
const encBucketName = 'minio-js-test-bucket-enc-' + uuid.v4()
before((done) => client.makeBucket(encBucketName, '', done))
after((done) => client.removeBucket(encBucketName, done))
after(() => client.removeBucket(encBucketName))

const encObjName = 'datafile-100-kB'
const encObjFileContent = Buffer.alloc(100 * 1024, 0)
Expand Down Expand Up @@ -3495,7 +3495,7 @@ describe('functional tests', function () {
// Isolate the bucket/object for easy debugging and tracking.
const bucketNameForSpCharObjects = 'minio-js-test-obj-spwpre-' + uuid.v4()
before((done) => client.makeBucket(bucketNameForSpCharObjects, '', done))
after((done) => client.removeBucket(bucketNameForSpCharObjects, done))
after(() => client.removeBucket(bucketNameForSpCharObjects))

// Reference:: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
// Host OS compatible File name characters/ file names.
Expand Down Expand Up @@ -3624,7 +3624,7 @@ describe('functional tests', function () {
// Isolate the bucket/object for easy debugging and tracking.
const bucketNameForSpCharObjects = 'minio-js-test-obj-spnpre-' + uuid.v4()
before((done) => client.makeBucket(bucketNameForSpCharObjects, '', done))
after((done) => client.removeBucket(bucketNameForSpCharObjects, done))
after(() => client.removeBucket(bucketNameForSpCharObjects))

// Reference:: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
let objectNameSpecialChars = "äöüex ®©µÄÆÐÕæŒƕƩDž 01000000 0x40 \u0040 amȡȹɆple&0a!-_.*'()&$@=;:+,?<>.pdf"
Expand Down Expand Up @@ -3765,7 +3765,7 @@ describe('functional tests', function () {
// Run only in local environment.
const bucketName = 'minio-js-test-assume-role' + uuid.v4()
before((done) => client.makeBucket(bucketName, '', done))
after((done) => client.removeBucket(bucketName, done))
after(() => client.removeBucket(bucketName))

const objName = 'datafile-100-kB'
const objContent = Buffer.alloc(100 * 1024, 0)
Expand Down Expand Up @@ -3820,7 +3820,7 @@ describe('functional tests', function () {
const bucketToTestMultipart = 'minio-js-test-put-multiuv-' + uuid.v4()

before((done) => client.makeBucket(bucketToTestMultipart, '', done))
after((done) => client.removeBucket(bucketToTestMultipart, done))
after(() => client.removeBucket(bucketToTestMultipart))

// Non multipart Test
step(
Expand Down Expand Up @@ -3909,7 +3909,7 @@ describe('functional tests', function () {
})
}),
)
after((done) => client.removeBucket(bucketToTestMultipart, done))
after(() => client.removeBucket(bucketToTestMultipart))

// Non multipart Test
step(
Expand Down Expand Up @@ -4010,7 +4010,7 @@ describe('functional tests', function () {
var _100mbFileToBeSplitAndComposed = Buffer.alloc(100 * 1024 * 1024, 0)
let composeObjectTestBucket = 'minio-js-test-compose-obj-' + uuid.v4()
before((done) => client.makeBucket(composeObjectTestBucket, '', done))
after((done) => client.removeBucket(composeObjectTestBucket, done))
after(() => client.removeBucket(composeObjectTestBucket))

const composedObjName = '_100-mb-file-to-test-compose'
const tmpSubDir = `${tmpDir}/compose`
Expand Down Expand Up @@ -4146,7 +4146,7 @@ describe('functional tests', function () {
// Isolate the bucket/object for easy debugging and tracking.
const bucketNameForSpCharObjects = 'minio-js-test-obj-sppre' + uuid.v4()
before((done) => client.makeBucket(bucketNameForSpCharObjects, '', done))
after((done) => client.removeBucket(bucketNameForSpCharObjects, done))
after(() => client.removeBucket(bucketNameForSpCharObjects))

const specialCharPrefix = 'SpecialMenùäöüexPrefix/'

Expand Down Expand Up @@ -4278,7 +4278,7 @@ describe('functional tests', function () {
const spBucketName = 'minio-js-test-lin-sppre' + uuid.v4()

before((done) => client.makeBucket(spBucketName, '', done))
after((done) => client.removeBucket(spBucketName, done))
after(() => client.removeBucket(spBucketName))

step(
`initiateNewMultipartUpload(bucketName, objectName, metaData, cb)_bucketName:${spBucketName}, objectName:${spObjWithPrefix}, metaData:${metaData}`,
Expand Down Expand Up @@ -4355,7 +4355,7 @@ describe('functional tests', function () {
const selObject = 'SelectObjectContent'
// Isolate the bucket/object for easy debugging and tracking.
before((done) => client.makeBucket(selObjContentBucket, '', done))
after((done) => client.removeBucket(selObjContentBucket, done))
after(() => client.removeBucket(selObjContentBucket))

step(
`putObject(bucketName, objectName, stream)_bucketName:${selObjContentBucket}, objectName:${selObject}, stream:csv`,
Expand Down Expand Up @@ -4432,7 +4432,7 @@ describe('functional tests', function () {
const fdObject = dataDir ? fs.readFileSync(dataDir + '/' + fdObjectName) : Buffer.alloc(100 * 1024, 0)

before((done) => client.makeBucket(fdWithVerBucket, '', done))
after((done) => client.removeBucket(fdWithVerBucket, done))
after(() => client.removeBucket(fdWithVerBucket))

describe('Test for force removal of multiple versions', function () {
let isVersioningSupported = false
Expand Down Expand Up @@ -4525,7 +4525,7 @@ describe('functional tests', function () {
const fdPrefixObject = dataDir ? fs.readFileSync(dataDir + '/datafile-100-kB') : Buffer.alloc(100 * 1024, 0)

before((done) => client.makeBucket(fdPrefixBucketName, '', done))
after((done) => client.removeBucket(fdPrefixBucketName, done))
after(() => client.removeBucket(fdPrefixBucketName))

describe('Test for removal of multiple versions', function () {
let isVersioningSupported = false
Expand Down Expand Up @@ -4620,7 +4620,7 @@ describe('functional tests', function () {
: Buffer.alloc(100 * 1024, 0)

before((done) => client.makeBucket(versionedBucketName, '', done))
after((done) => client.removeBucket(versionedBucketName, done))
after(() => client.removeBucket(versionedBucketName))

describe('Test force removal of an object', function () {
step(
Expand Down Expand Up @@ -4670,7 +4670,7 @@ describe('functional tests', function () {
const fdObject = dataDir ? fs.readFileSync(dataDir + '/datafile-100-kB') : Buffer.alloc(100 * 1024, 0)

before((done) => client.makeBucket(fdPrefixBucket, '', done))
after((done) => client.removeBucket(fdPrefixBucket, done))
after(() => client.removeBucket(fdPrefixBucket))

describe('Test force removal of a prefix', function () {
step(
Expand Down

0 comments on commit f6a34ca

Please sign in to comment.