Skip to content

Commit

Permalink
Add Bucket replication APIs (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx authored Jun 2, 2021
1 parent 789723e commit d44419e
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ The full API Reference is available here.
* [remove-bucket-lifecycle.js](https://github.com/minio/minio-js/blob/master/examples/remove-bucket-lifecycle.js)
* [get-object-lock-config.js](https://github.com/minio/minio-js/blob/master/examples/get-object-lock-config.js)
* [set-object-lock-config.js](https://github.com/minio/minio-js/blob/master/examples/set-object-lock-config.js)
* [set-bucket-replication.js](https://github.com/minio/minio-js/blob/master/examples/set-bucket-replication.js)
* [get-bucket-replication.js](https://github.com/minio/minio-js/blob/master/examples/get-bucket-replication.js)
* [remove-bucket-replication.js](https://github.com/minio/minio-js/blob/master/examples/remove-bucket-replication.js)

#### Full Examples : File Object Operations
* [fput-object.js](https://github.com/minio/minio-js/blob/master/examples/fput-object.js)
Expand Down
99 changes: 99 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var s3Client = new Minio.Client({
|[`setBucketEncryption`](#setBucketEncryption) | |
|[`getBucketEncryption`](#getBucketEncryption) | |
|[`removeBucketEncryption`](#removeBucketEncryption) | |
| [`removeBucketLifecycle`](#removeBucketLifecycle) | |
| [`setBucketReplication`](#setBucketReplication)| |
| [`getBucketReplication`](#getBucketReplication)| |
| [`removeBucketReplication`](#removeBucketReplication)| |



Expand Down Expand Up @@ -473,6 +477,101 @@ minioClient.setBucketVersioning('bucketname',versioningConfig, function (err){
})
```


<a name="setBucketReplication"></a>
### setBucketReplication(bucketName, replicationConfig, callback)

Set replication config on a Bucket

__Parameters__


| Param | Type | Description |
| ---| ---|---|
| `bucketname` | _string_ | Name of the bucket. |
| `replicationConfig` | _object_ | replicationConfig Configuration as a JSON Object |
|`callback(err)` | _function_ | Callback is called with `err` in case of error.|

__Example__
```js
const arnFromMcCli = "arn:minio:replication::1277fcbe7df0bab76ab0c64cf7c45a0d27e01917ee5f11e913f3478417833660:destination"

var replicationConfig = {
role:arnFromMcCli,
rules:[{
"DeleteMarkerReplication": {
"Status": "Disabled"
},
"DeleteReplication": {
"Status": [
]
},
"Destination": {
"Bucket": "arn:aws:s3:::destination"
},
"Priority": "1",
"Status": "Enabled"
}]
}

minioClient.setBucketReplication('bucketname',replicationConfig, function (err){
if (err) {
return console.log(err)
}
console.log("Success")
})

```

<a name="getBucketReplication"></a>
### getBucketReplication(bucketName, callback)

Get replication config of a Bucket

__Parameters__


| Param | Type | Description |
| ---| ---|---|
| `bucketname` | _string_ | Name of the bucket. |
|`callback(err, replicationConfig)` | _function_ | Callback is called with `err` in case of error. else returns the info in`replicationConfig` ,which contains`{role: __string__, rules:__Array__ }`. |

__Example__
```js
minioClient.getBucketReplication('bucketname', function (err,replicationConfig){
if (err) {
return console.log(err)
}
console.log(replicationConfig)
})

```


<a name="removeBucketReplication"></a>
### removeBucketReplication(bucketName, callback)

Remove replication config of a Bucket

__Parameters__


| Param | Type | Description |
| ---| ---|---|
| `bucketname` | _string_ | Name of the bucket. |
|`callback(err)` | _function_ | Callback is called with `err` in case of error. |

__Example__
```js
minioClient.removeBucketReplication('bucketname', function (err,replicationConfig){
if (err) {
return console.log(err)
}
console.log("Success")
})

```

<a name="setBucketTagging"></a>
### setBucketTagging(bucketName, tags, callback)

Expand Down
33 changes: 33 additions & 0 deletions examples/get-bucket-replication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MinIO Javascript Library for Amazon S3 Compatible Cloud Storage, (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
// dummy values, please replace them with original values.

var Minio = require('minio')

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

s3Client.getBucketReplication('bucketname', function (err, replicationConfig){
if (err) {
return console.log(err)
}
console.log(replicationConfig)
})
33 changes: 33 additions & 0 deletions examples/remove-bucket-replication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MinIO Javascript Library for Amazon S3 Compatible Cloud Storage, (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
// dummy values, please replace them with original values.

var Minio = require('minio')

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

s3Client.removeBucketReplication('bucketname', function (err){
if (err) {
return console.log(err)
}
console.log("Success")
})
54 changes: 54 additions & 0 deletions examples/set-bucket-replication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MinIO Javascript Library for Amazon S3 Compatible Cloud Storage, (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
// dummy values, please replace them with original values.

var Minio = require('minio')

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


const arnFromMcCli = "arn:minio:replication::1277fcbe7df0bab76ab0c64cf7c45a0d27e01917ee5f11e913f3478417833660:destination"

var replicationConfig = {
role:arnFromMcCli,
rules:[{
"DeleteMarkerReplication": {
"Status": "Disabled"
},
"DeleteReplication": {
"Status": [
]
},
"Destination": {
"Bucket": "arn:aws:s3:::destination"
},
"Priority": "1",
"Status": "Enabled"
}]
}

s3Client.setBucketReplication('bucketname',replicationConfig, function (err){
if (err) {
return console.log(err)
}
console.log("Success")
})
114 changes: 96 additions & 18 deletions src/main/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -2860,6 +2860,81 @@ export class Client {
}


setBucketReplication(bucketName, replicationConfig={}, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!isObject(replicationConfig)) {
throw new errors.InvalidArgumentError('replicationConfig should be of type "object"')
} else {
if (_.isEmpty(replicationConfig.role)) {
throw new errors.InvalidArgumentError('Role cannot be empty')
}else if (replicationConfig.role && !isString(replicationConfig.role)) {
throw new errors.InvalidArgumentError('Invalid value for role', replicationConfig.role)
}
if (_.isEmpty(replicationConfig.rules)) {
throw new errors.InvalidArgumentError('Minimum one replication rule must be specified')
}
}
if (!isFunction(cb)) {
throw new TypeError('callback should be of type "function"')
}

const method = 'PUT'
let query = "replication"
const headers = {}

const replicationParamsConfig = {
ReplicationConfiguration: {
Role: replicationConfig.role,
Rule: replicationConfig.rules
}
}

const builder = new xml2js.Builder({ renderOpts:{'pretty':false},headless: true })

let payload = builder.buildObject(replicationParamsConfig)

const md5digest = Crypto.createHash('md5').update(payload).digest()
headers['Content-MD5'] = md5digest.toString('base64')

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

getBucketReplication(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"')
}
const method = 'GET'
const query = "replication"

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

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

removeBucketReplication(bucketName, cb){
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
const method = 'DELETE'
const query="replication"
this.makeRequest({method, bucketName, query}, '', 200, '', false, cb)
}

get extensions() {
if(!this.clientExtensions)
{
Expand Down Expand Up @@ -2895,24 +2970,27 @@ 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.getBucketTagging=promisify((Client.prototype.getBucketTagging))
Client.prototype.setObjectTagging=promisify((Client.prototype.setObjectTagging))
Client.prototype.removeObjectTagging=promisify((Client.prototype.removeObjectTagging))
Client.prototype.getObjectTagging=promisify((Client.prototype.getObjectTagging))
Client.prototype.setBucketLifecycle=promisify((Client.prototype.setBucketLifecycle))
Client.prototype.getBucketLifecycle=promisify((Client.prototype.getBucketLifecycle))
Client.prototype.removeBucketLifecycle=promisify((Client.prototype.removeBucketLifecycle))
Client.prototype.setObjectLockConfig=promisify((Client.prototype.setObjectLockConfig))
Client.prototype.getObjectLockConfig=promisify((Client.prototype.getObjectLockConfig))
Client.prototype.putObjectRetention =promisify((Client.prototype.putObjectRetention))
Client.prototype.getObjectRetention =promisify((Client.prototype.getObjectRetention))
Client.prototype.setBucketEncryption = promisify((Client.prototype.setBucketEncryption))
Client.prototype.getBucketEncryption = promisify((Client.prototype.getBucketEncryption))
Client.prototype.removeBucketEncryption = promisify((Client.prototype.removeBucketEncryption))
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.getBucketTagging=promisify(Client.prototype.getBucketTagging)
Client.prototype.setObjectTagging=promisify(Client.prototype.setObjectTagging)
Client.prototype.removeObjectTagging=promisify(Client.prototype.removeObjectTagging)
Client.prototype.getObjectTagging=promisify(Client.prototype.getObjectTagging)
Client.prototype.setBucketLifecycle=promisify(Client.prototype.setBucketLifecycle)
Client.prototype.getBucketLifecycle=promisify(Client.prototype.getBucketLifecycle)
Client.prototype.removeBucketLifecycle=promisify(Client.prototype.removeBucketLifecycle)
Client.prototype.setObjectLockConfig=promisify(Client.prototype.setObjectLockConfig)
Client.prototype.getObjectLockConfig=promisify(Client.prototype.getObjectLockConfig)
Client.prototype.putObjectRetention =promisify(Client.prototype.putObjectRetention)
Client.prototype.getObjectRetention =promisify(Client.prototype.getObjectRetention)
Client.prototype.setBucketEncryption = promisify(Client.prototype.setBucketEncryption)
Client.prototype.getBucketEncryption = promisify(Client.prototype.getBucketEncryption)
Client.prototype.removeBucketEncryption = promisify(Client.prototype.removeBucketEncryption)
Client.prototype.setBucketReplication =promisify(Client.prototype.setBucketReplication)
Client.prototype.getBucketReplication =promisify(Client.prototype.getBucketReplication)
Client.prototype.removeBucketReplication=promisify(Client.prototype.removeBucketReplication)

export class CopyConditions {
constructor() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,8 @@ export function objectRetentionTransformer(){
}
export function bucketEncryptionTransformer(){
return getConcater(xmlParsers.parseBucketEncryptionConfig)
}

export function replicationConfigTransformer(){
return getConcater(xmlParsers.parseReplicationConfig)
}
Loading

0 comments on commit d44419e

Please sign in to comment.