Skip to content

Commit

Permalink
refactor getObjectRetention to ts (minio#1284)
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx authored May 7, 2024
1 parent e61a9e2 commit 6b91636
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 174 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ The complete API Reference is available here:
- [remove-object.js](https://github.com/minio/minio-js/blob/master/examples/remove-object.js)
- [remove-incomplete-upload.js](https://github.com/minio/minio-js/blob/master/examples/remove-incomplete-upload.js)
- [stat-object.mjs](https://github.com/minio/minio-js/blob/master/examples/stat-object.mjs)
- [get-object-retention.js](https://github.com/minio/minio-js/blob/master/examples/get-object-retention.js)
- [put-object-retention.js](https://github.com/minio/minio-js/blob/master/examples/put-object-retention.js)
- [get-object-retention.mjs](https://github.com/minio/minio-js/blob/master/examples/get-object-retention.mjs)
- [put-object-retention.mjs](https://github.com/minio/minio-js/blob/master/examples/put-object-retention.mjs)
- [put-object-tagging.mjs](https://github.com/minio/minio-js/blob/master/examples/put-object-tagging.js)
- [get-object-tagging.mjs](https://github.com/minio/minio-js/blob/master/examples/get-object-tagging.mjs)
- [remove-object-tagging.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-object-tagging.js)
- [set-object-legal-hold.js](https://github.com/minio/minio-js/blob/master/examples/set-object-legalhold.mjs)
- [get-object-legal-hold.js](https://github.com/minio/minio-js/blob/master/examples/get-object-legal-hold.mjs)
- [set-object-legal-hold.mjs](https://github.com/minio/minio-js/blob/master/examples/set-object-legalhold.mjs)
- [get-object-legal-hold.mjs](https://github.com/minio/minio-js/blob/master/examples/get-object-legal-hold.mjs)
- [compose-object.js](https://github.com/minio/minio-js/blob/master/examples/compose-object.js)
- [select-object-content.js](https://github.com/minio/minio-js/blob/master/examples/select-object-content.mjs)
- [select-object-content.mjs](https://github.com/minio/minio-js/blob/master/examples/select-object-content.mjs)

#### Presigned Operations

Expand Down
30 changes: 17 additions & 13 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1331,28 +1331,32 @@ await minioClient.putObjectRetention(bucketName, objectName, {

<a name="getObjectRetention"></a>

### getObjectRetention(bucketName, objectName [, getOpts] [, callback])
### getObjectRetention(bucketName, objectName [, getOpts])

Get retention config of an object

**Parameters**

| Param | Type | Description |
| -------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `getOpts` | _object_ | Options for retention like : `{ versionId:"my-versionId" }` Default is `{}` (Optional) |
| `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. |
| `objectName` | _string_ | Name of the object. |
| `getOpts` | _object_ | Options for retention like : `{ versionId:"my-versionId" }` Default is `{}` (Optional) |

**Example**
**Example 1**

```js
minioClient.getObjectRetention('bucketname', 'bucketname', { versionId: 'my-versionId' }, function (err, res) {
if (err) {
return console.log(err)
}
console.log(res)
const retentionInfo = await minioClient.getObjectRetention('bucketname', 'objectname')
console.log(retentionInfo)
```

**Example 2**

```js
const retInfoForVersionId = await minioClient.getObjectRetention('bucketname', 'objectname', {
versionId: 'my-versionId',
})
console.log(retInfoForVersionId)
```

<a name="setObjectTagging"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,5 @@ const objectName = 'my-object'

const versionId = 'my-versionId'

const objRetPromise = s3Client.getObjectRetention(bucketName, objectName, { versionId: versionId })
objRetPromise
.then((data) => {
console.log('Success', data)
})
.catch((e) => {
console.log(' Error', e)
})
const retentionInfo = await s3Client.getObjectRetention(bucketName, objectName, { versionId: versionId })
console.log(retentionInfo)
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ const s3Client = new Minio.Client({

const bucketName = 'my-bucket'
const objectName = 'my-object'
const versionId = 'my-versionId'
const mode = 'COMPLIANCE'

const expirationDate = new Date()
expirationDate.setDate(expirationDate.getDate() + 1)
expirationDate.setDate(expirationDate.getDate() + 2)
expirationDate.setUTCHours(0, 0, 0, 0) //Should be start of the day.(midnight)
const versionId = 'my-versionId'

await s3Client.putObjectRetention(bucketName, objectName, {
mode: 'GOVERNANCE',
mode: mode,
retainUntilDate: expirationDate.toISOString(),
versionId: versionId,
})
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import type {
BucketVersioningConfiguration,
EncryptionConfig,
GetObjectLegalHoldOptions,
GetObjectRetentionOpts,
IncompleteUploadedBucketItem,
IRequest,
ItemBucketMetadata,
Expand All @@ -69,6 +70,7 @@ import type {
ObjectLockConfigParam,
ObjectLockInfo,
ObjectMetaData,
ObjectRetentionInfo,
PutObjectLegalHoldOptions,
PutTaggingParams,
RemoveTaggingParams,
Expand Down Expand Up @@ -2400,4 +2402,31 @@ export class TypedClient {

await this.makeRequestAsyncOmit({ method, bucketName, query }, '', [204])
}

async getObjectRetention(
bucketName: string,
objectName: string,
getOpts?: GetObjectRetentionOpts,
): Promise<ObjectRetentionInfo | null | undefined> {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!isValidObjectName(objectName)) {
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
}
if (getOpts && !isObject(getOpts)) {
throw new errors.InvalidArgumentError('getOpts should be of type "object"')
} else if (getOpts?.versionId && !isString(getOpts.versionId)) {
throw new errors.InvalidArgumentError('versionId should be of type "string"')
}

const method = 'GET'
let query = 'retention'
if (getOpts?.versionId) {
query += `&versionId=${getOpts.versionId}`
}
const res = await this.makeRequestAsync({ method, bucketName, objectName, query })
const body = await readAsString(res)
return xmlParsers.parseObjectRetentionConfig(body)
}
}
9 changes: 9 additions & 0 deletions src/internal/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,12 @@ export type EncryptionRule = {
export type EncryptionConfig = {
Rule: EncryptionRule[]
}

export type GetObjectRetentionOpts = {
versionId: string
}

export type ObjectRetentionInfo = {
mode: RETENTION_MODES
retainUntilDate: string
}
9 changes: 9 additions & 0 deletions src/internal/xml-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,12 @@ export function parseLifecycleConfig(xml: string) {
export function parseBucketEncryptionConfig(xml: string) {
return parseXml(xml)
}

export function parseObjectRetentionConfig(xml: string) {
const xmlObj = parseXml(xml)
const retentionConfig = xmlObj.Retention
return {
mode: retentionConfig.Mode,
retainUntilDate: retentionConfig.RetainUntilDate,
}
}
10 changes: 0 additions & 10 deletions src/minio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import type {
SelectProgress,
SourceSelectionCriteria,
Tag,
VersionIdentificator,
} from './internal/type.ts'
import type { NotificationConfig, NotificationEvent, NotificationPoller } from './notification.ts'

Expand Down Expand Up @@ -167,15 +166,6 @@ export class Client extends TypedClient {

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

getObjectRetention(
bucketName: string,
objectName: string,
options: VersionIdentificator,
callback: ResultCallback<Retention>,
): void
getObjectRetention(bucketName: string, objectName: string, options: VersionIdentificator): Promise<Retention>

composeObject(
destObjConfig: CopyDestinationOptions,
sourceObjList: CopySourceOptions[],
Expand Down
40 changes: 1 addition & 39 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,44 +879,6 @@ export class Client extends TypedClient {
return listener
}

getObjectRetention(bucketName, objectName, getOpts, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!isValidObjectName(objectName)) {
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
}
if (!isObject(getOpts)) {
throw new errors.InvalidArgumentError('callback should be of type "object"')
} else if (getOpts.versionId && !isString(getOpts.versionId)) {
throw new errors.InvalidArgumentError('VersionID should be of type "string"')
}
if (cb && !isFunction(cb)) {
throw new errors.InvalidArgumentError('callback should be of type "function"')
}
const method = 'GET'
let query = 'retention'
if (getOpts.versionId) {
query += `&versionId=${getOpts.versionId}`
}

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

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

/**
* Internal method to upload a part during compose object.
* @param partConfig __object__ contains the following.
Expand Down Expand Up @@ -1149,7 +1111,6 @@ Client.prototype.getBucketNotification = promisify(Client.prototype.getBucketNot
Client.prototype.setBucketNotification = promisify(Client.prototype.setBucketNotification)
Client.prototype.removeAllBucketNotification = promisify(Client.prototype.removeAllBucketNotification)
Client.prototype.removeIncompleteUpload = promisify(Client.prototype.removeIncompleteUpload)
Client.prototype.getObjectRetention = promisify(Client.prototype.getObjectRetention)
Client.prototype.composeObject = promisify(Client.prototype.composeObject)

// refactored API use promise internally
Expand Down Expand Up @@ -1191,3 +1152,4 @@ Client.prototype.removeBucketLifecycle = callbackify(Client.prototype.removeBuck
Client.prototype.setBucketEncryption = callbackify(Client.prototype.setBucketEncryption)
Client.prototype.getBucketEncryption = callbackify(Client.prototype.getBucketEncryption)
Client.prototype.removeBucketEncryption = callbackify(Client.prototype.removeBucketEncryption)
Client.prototype.getObjectRetention = callbackify(Client.prototype.getObjectRetention)
4 changes: 0 additions & 4 deletions src/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ export function getBucketNotificationTransformer() {
return getConcater(xmlParsers.parseBucketNotification)
}

export function objectRetentionTransformer() {
return getConcater(xmlParsers.parseObjectRetentionConfig)
}

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

export function parseObjectRetentionConfig(xml) {
const xmlObj = parseXml(xml)
const retentionConfig = xmlObj.Retention

return {
mode: retentionConfig.Mode,
retainUntilDate: retentionConfig.RetainUntilDate,
}
}

export function parseObjectLegalHoldConfig(xml) {
const xmlObj = parseXml(xml)
return xmlObj.LegalHold
Expand Down
Loading

0 comments on commit 6b91636

Please sign in to comment.