Skip to content

Commit

Permalink
refactor getBucketVersioning and setBucketVersioning
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx committed Nov 17, 2023
1 parent d8473f9 commit 0817ba9
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 136 deletions.
36 changes: 12 additions & 24 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,47 +462,35 @@ Get Versioning state of a Bucket

**Parameters**

| Param | Type | Description |
| -------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `callback(err, res)` | _function_ | Callback is called with `err` in case of error. `res` is the response object. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------ | -------- | ------------------- |
| `bucketName` | _string_ | Name of the bucket. |

**Example**

```js
minioClient.getBucketVersioning('bucketname', function (err, res) {
if (err) {
return console.log(err)
}
console.log(res)
console.log('Success')
})
const versionInfo = await minioClient.getBucketVersioning('bucketname')
console.log('Success ', versionInfo)
```

<a name="setBucketVersioning"></a>

### setBucketVersioning(bucketName, versioningConfig, callback)
### setBucketVersioning(bucketName, versioningConfig)

Set Versioning state on a Bucket

**Parameters**

| Param | Type | Description |
| ------------------ | ---------- | -------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `versioningConfig` | _object_ | Versioning Configuration e.g: `{Status:"Enabled"}` |
| `callback(err)` | _function_ | Callback is called with `err` in case of error. |
| Param | Type | Description |
| ------------------ | -------- | -------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `versioningConfig` | _object_ | Versioning Configuration e.g: `{Status:"Enabled"}` |

**Example**

```js
var versioningConfig = { Status: 'Enabled' }
minioClient.setBucketVersioning('bucketname', versioningConfig, function (err) {
if (err) {
return console.log(err)
}
console.log('Success')
})
const versioningConfig = { Status: 'Enabled' }
await minioClient.setBucketVersioning('bucketname', versioningConfig)
```

<a name="setBucketReplication"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@
// 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',
})

s3Client.getBucketVersioning('my-bucketname', function (err, res) {
if (err) {
return console.log(err)
}
console.log(res)
console.log('Success')
})
const versionInfo = await s3Client.getBucketVersioning('my-bucketname')
console.log('Bucket versioning info ', versionInfo)
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
// 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',
})

var versioningStateConfig = { Status: 'Enabled' }
const versioningStateConfig = { Status: 'Enabled' }

s3Client.setBucketVersioning('my-bucket', versioningStateConfig, function (error) {
if (error) {
return console.log(error)
}
console.log('Success')
})
await s3Client.setBucketVersioning('my-bucket', versioningStateConfig)
console.log('Success')
33 changes: 33 additions & 0 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
Binary,
BucketItemFromList,
BucketItemStat,
BucketVersioningConfiguration,
GetObjectLegalHoldOptions,
IRequest,
ObjectLockConfigParam,
Expand Down Expand Up @@ -1380,4 +1381,36 @@ export class TypedClient {

await this.makeRequestAsyncOmit({ method, bucketName, query, headers }, payload)
}

async getBucketVersioning(bucketName: string): Promise<void> {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
const method = 'GET'
const query = 'versioning'

const httpRes = await this.makeRequestAsync({ method, bucketName, query })
const xmlResult = await readAsString(httpRes)
return await xmlParsers.parseBucketVersioningConfig(xmlResult)
}

async setBucketVersioning(bucketName: string, versionConfig: BucketVersioningConfiguration): Promise<void> {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!Object.keys(versionConfig).length) {
throw new errors.InvalidArgumentError('versionConfig should be of type "object"')
}

const method = 'PUT'
const query = 'versioning'
const builder = new xml2js.Builder({
rootName: 'VersioningConfiguration',
renderOpts: { pretty: false },
headless: true,
})
const payload = builder.buildObject(versionConfig)

await this.makeRequestAsyncOmit({ method, bucketName, query }, payload)
}
}
8 changes: 8 additions & 0 deletions src/internal/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,11 @@ export type ObjectLockConfigParam = {
}
| EmptyObject
}

export type VersioningEnabled = 'Enabled'
export type VersioningSuspended = 'Suspended'

export type BucketVersioningConfiguration = {
Status: VersioningEnabled | VersioningSuspended
// TODO add ExcludedPrefixes, ExcludeFolders which are part of MinIO's extension, as an enhancement.
}
5 changes: 5 additions & 0 deletions src/internal/xml-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,8 @@ export function parseObjectLockConfig(xml: string): ObjectLockInfo {

return lockConfigResult
}

export function parseBucketVersioningConfig(xml: string) {
const xmlObj = parseXml(xml)
return xmlObj.VersioningConfiguration
}
6 changes: 0 additions & 6 deletions src/minio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,6 @@ export class Client extends TypedClient {
recursive?: boolean,
): BucketStream<IncompleteUploadedBucketItem>

getBucketVersioning(bucketName: string, callback: ResultCallback<VersioningConfig>): void
getBucketVersioning(bucketName: string): Promise<VersioningConfig>

setBucketVersioning(bucketName: string, versioningConfig: any, callback: NoResultCallback): void
setBucketVersioning(bucketName: string, versioningConfig: any): Promise<void>

setBucketTagging(bucketName: string, tags: TagList, callback: NoResultCallback): void
setBucketTagging(bucketName: string, tags: TagList): Promise<void>

Expand Down
55 changes: 3 additions & 52 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -1509,56 +1509,6 @@ export class Client extends TypedClient {
return listener
}

getBucketVersioning(bucketName, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!isFunction(cb)) {
throw new errors.InvalidArgumentError('callback should be of type "function"')
}
var method = 'GET'
var query = 'versioning'

this.makeRequest({ method, bucketName, query }, '', [200], '', true, (e, response) => {
if (e) {
return cb(e)
}

let versionConfig = Buffer.from('')
pipesetup(response, transformers.bucketVersioningTransformer())
.on('data', (data) => {
versionConfig = data
})
.on('error', cb)
.on('end', () => {
cb(null, versionConfig)
})
})
}

setBucketVersioning(bucketName, versionConfig, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!Object.keys(versionConfig).length) {
throw new errors.InvalidArgumentError('versionConfig should be of type "object"')
}
if (!isFunction(cb)) {
throw new TypeError('callback should be of type "function"')
}

var method = 'PUT'
var query = 'versioning'
var builder = new xml2js.Builder({
rootName: 'VersioningConfiguration',
renderOpts: { pretty: false },
headless: true,
})
var payload = builder.buildObject(versionConfig)

this.makeRequest({ method, bucketName, query }, payload, [200], '', false, cb)
}

/** To set Tags on a bucket or object based on the params
* __Arguments__
* taggingParams _object_ Which contains the following properties
Expand Down Expand Up @@ -2250,8 +2200,6 @@ Client.prototype.removeAllBucketNotification = promisify(Client.prototype.remove
Client.prototype.getBucketPolicy = promisify(Client.prototype.getBucketPolicy)
Client.prototype.setBucketPolicy = promisify(Client.prototype.setBucketPolicy)
Client.prototype.removeIncompleteUpload = promisify(Client.prototype.removeIncompleteUpload)
Client.prototype.getBucketVersioning = promisify(Client.prototype.getBucketVersioning)
Client.prototype.setBucketVersioning = promisify(Client.prototype.setBucketVersioning)
Client.prototype.setBucketTagging = promisify(Client.prototype.setBucketTagging)
Client.prototype.removeBucketTagging = promisify(Client.prototype.removeBucketTagging)
Client.prototype.setObjectTagging = promisify(Client.prototype.setObjectTagging)
Expand Down Expand Up @@ -2283,3 +2231,6 @@ Client.prototype.getObjectTagging = callbackify(Client.prototype.getObjectTaggin
Client.prototype.putObjectRetention = callbackify(Client.prototype.putObjectRetention)
Client.prototype.setObjectLockConfig = callbackify(Client.prototype.setObjectLockConfig)
Client.prototype.getObjectLockConfig = callbackify(Client.prototype.getObjectLockConfig)

Client.prototype.getBucketVersioning = callbackify(Client.prototype.getBucketVersioning)
Client.prototype.setBucketVersioning = callbackify(Client.prototype.setBucketVersioning)
4 changes: 0 additions & 4 deletions src/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ export function getNotificationTransformer() {
return new JSONParser()
}

export function bucketVersioningTransformer() {
return getConcater(xmlParsers.parseBucketVersioningConfig)
}

export function lifecycleTransformer() {
return getConcater(xmlParsers.parseLifecycleConfig)
}
Expand Down
4 changes: 0 additions & 4 deletions src/xml-parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,6 @@ export function parseListObjectsV2WithMetadata(xml) {
return result
}

export function parseBucketVersioningConfig(xml) {
var xmlObj = parseXml(xml)
return xmlObj.VersioningConfiguration
}
export function parseLifecycleConfig(xml) {
const xmlObj = parseXml(xml)
return xmlObj.LifecycleConfiguration
Expand Down
62 changes: 34 additions & 28 deletions tests/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,43 +835,49 @@ describe('Client', function () {
describe('Bucket Versioning APIs', () => {
describe('getBucketVersioning(bucket, callback)', () => {
it('should fail on null bucket', (done) => {
try {
client.getBucketVersioning(null, function () {})
} catch (e) {
done()
}
client.getBucketVersioning(null, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})

it('should fail on empty bucket', (done) => {
try {
client.getBucketVersioning('', function () {})
} catch (e) {
done()
}
client.getBucketVersioning('', function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})
})

describe('setBucketVersioning(bucket, versionConfig, callback)', () => {
it('should fail on null bucket', (done) => {
try {
client.setBucketVersioning(null, {}, function () {})
} catch (e) {
done()
}
client.setBucketVersioning(null, {}, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})

it('should fail on empty bucket', (done) => {
try {
client.setBucketVersioning('', {}, function () {})
} catch (e) {
done()
}
client.setBucketVersioning(null, {}, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})

it('should fail on empty versionConfig', (done) => {
try {
client.setBucketVersioning('', null, function () {})
} catch (e) {
done()
}
it('should fail on empty bucket', (done) => {
client.setBucketVersioning('', null, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})
})
})
Expand Down Expand Up @@ -965,7 +971,7 @@ describe('Client', function () {
it('should fail on invalid bucket', (done) => {
client.getObjectTagging('nv', null).then(
() => done(new Error('callback should receive error')),
(err) => done(),
() => done(),
)
})
it('should fail on null object', async () => {
Expand Down

0 comments on commit 0817ba9

Please sign in to comment.