Skip to content

Commit

Permalink
refactor composeObject to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx committed May 20, 2024
1 parent dd57ec4 commit 7250173
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 487 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ The complete API Reference is available here:
- [remove-object-tagging.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-object-tagging.js)
- [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)
- [compose-object.mjs](https://github.com/minio/minio-js/blob/master/examples/compose-object.js)
- [select-object-content.mjs](https://github.com/minio/minio-js/blob/master/examples/select-object-content.mjs)

#### Presigned Operations
Expand Down
12 changes: 2 additions & 10 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ const legalholdStatus = await minioClient.setObjectLegalHold('bucketName', 'obje

<a name="composeObject"></a>

### composeObject(destObjConfig, sourceObjectList [, callback])
### composeObject(destObjConfig, sourceObjectList)

Compose an object from parts

Expand All @@ -1493,7 +1493,6 @@ Compose an object from parts
| ------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `destObjConfig` | _object_ | Destination Object configuration of the type [CopyDestinationOptions](https://github.com/minio/minio-js/blob/master/src/helpers.js) |
| `sourceObjectList` | _object[]_ | Array of object(parts) source to compose into an object. Each part configuration should be of type [CopySourceOptions](https://github.com/minio/minio-js/blob/master/src/helpers.js) |
| `callback(err)` | _function_ | Callback function is called with non `null` value in case of error. If no callback is passed, a `Promise` is returned. |

**Example 1**

Expand Down Expand Up @@ -1527,14 +1526,7 @@ const destOption = new minio.CopyDestinationOptions({
})

//using Promise style.
const composePromise = minioClient.composeObject(destOption, sourceList)
composePromise
.then((result) => {
console.log('Success...')
})
.catch((e) => {
console.log('error', e)
})
await minioClient.composeObject(destOption, sourceList)
```

<a name="selectObjectContent"></a>
Expand Down
136 changes: 0 additions & 136 deletions examples/compose-object-test-example.js

This file was deleted.

126 changes: 126 additions & 0 deletions examples/compose-object-test-example.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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, my-bucketname and my-objectname
// are dummy values, please replace them with original values.
import fs from 'node:fs'
import os from 'node:os'

import * as Minio from 'minio'
import splitFile from 'split-file'

const s3Client = new Minio.Client({
endPoint: 'localhost',
accessKey: 'minio',
secretKey: 'minio123',
useSSL: false,
port: 22000,
//partSize: 5 * 1024 * 1024
})

const oneMB = 1024 * 1024

// Create a bucket prior to running: mc mb local/source-bucket
const sampleRunComposeObject = async () => {
const tmpDir = os.tmpdir()

const bucketName = 'source-bucket'
// generate 100 MB buffer and write to a file.
const local100mbFileToBeSplitAndComposed = Buffer.alloc(100 * oneMB, 0)

const composedObjName = '_100-mb-file-to-test-compose'
const tmpSubDir = `${tmpDir}/compose`
const fileToSplit = `${tmpSubDir}/${composedObjName}`
const partObjNameList = []

fs.mkdir(tmpSubDir, { recursive: true }, function (err) {
if (err) {
console.log(err)
} else {
console.log('New Temp directory successfully created.')
}
})

try {
fs.writeFileSync(fileToSplit, local100mbFileToBeSplitAndComposed)
console.log('Written 100 MB File ')
// 100 MB split into 26 MB part size. ( just to test unequal parts ). But change as required.

const names = await splitFile.splitFileBySize(fileToSplit, 26 * oneMB)

console.log('Split and write 100 MB File(s) ', names)
const putPartRequests = names.map((partFileName) => {
const partObjName = partFileName.slice((tmpSubDir + '/').length)
partObjNameList.push(partObjName)
return s3Client.fPutObject(bucketName, partObjName, partFileName, {})
})
await Promise.all(putPartRequests)

console.log('Uploaded part Files: ', names)
const sourcePartObjList = partObjNameList.map((partObjName) => {
return new Minio.CopySourceOptions({
Bucket: bucketName,
Object: partObjName,
})
})

const destObjConfig = new Minio.CopyDestinationOptions({
Bucket: bucketName,
Object: composedObjName,
})

try {
const result = await s3Client.composeObject(destObjConfig, sourcePartObjList)
console.log(result)
console.log('Composed to a single file: ', composedObjName)
} catch (err) {
console.log('Composed to a single file: ', composedObjName)

/** Begin Clean up ***/
// To verify that the parts are uploaded properly, comment the below code blocks and verify
const sourcePartObjList = partObjNameList.map((partObjName) => {
return s3Client.removeObject(bucketName, partObjName)
})

Promise.all(sourcePartObjList)
.then(() => {
console.log('Removed source parts: ')

// Uncomment to remove the composed object itself. commented for verification.
/*
s3Client.removeObject(bucketName, composedObjName).then(()=>{
console.log("Clean up: Removed the composed Object ")
}).catch(()=>{
console.log("Error removing composed object", er)
})
*/
})
.catch((er) => {
console.log('Error removing parts used in composing', er)
})

/** End Clean up **/

// Clean up generated parts locally
fs.rmSync(tmpSubDir, { recursive: true, force: true })
console.log('Clean up temp parts directory : ')
}
} catch (e) {
console.log('Error Creating local files ', e)
}
}

sampleRunComposeObject()
11 changes: 2 additions & 9 deletions examples/compose-object.js → examples/compose-object.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const sourceList = [

const destOption = new Minio.CopyDestinationOptions({
Bucket: bucketName,
Object: '100MB.zip',
Object: 'object-name',
/** Other possible options */
/* Encryption:{
type:Helpers.ENCRYPTION_TYPES.KMS,
Expand All @@ -67,11 +67,4 @@ const destOption = new Minio.CopyDestinationOptions({
*/
})

const composePromise = s3Client.composeObject(destOption, sourceList)
composePromise
.then((result) => {
console.log('ComposeObject Success...', result)
})
.catch((e) => {
console.log('composeObject Promise Error', e)
})
await s3Client.composeObject(destOption, sourceList)
Loading

0 comments on commit 7250173

Please sign in to comment.