Skip to content

Commit

Permalink
migrate copyObject api to ts (minio#1289)
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx committed May 20, 2024
1 parent da24d11 commit 8be0a48
Show file tree
Hide file tree
Showing 14 changed files with 567 additions and 628 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
35 changes: 10 additions & 25 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1089,32 +1089,25 @@ minioClient.fPutObject('mybucket', '40mbfile', file, metaData, function (err, ob

<a name="copyObject"></a>

### copyObject(bucketName, objectName, sourceObject, conditions[, callback])
### copyObject(targetBucketName, targetObjectName, sourceBucketNameAndObjectName [,conditions])

Copy a source object into a new object in the specified bucket.

**Parameters**

| Param | Type | Description |
| ------------------------------------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `sourceObject` | _string_ | Path of the file to be copied. |
| `conditions` | _CopyConditions_ | Conditions to be satisfied before allowing object copy. |
| `callback(err, {etag, lastModified})` | _function_ | Non-null `err` indicates error, `etag` _string_ and lastModified _Date_ are the etag and the last modified date of the object newly copied. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------------------------- | ---------------- | ------------------------------------------------------- |
| `targetBucketName` | _string_ | Name of the bucket. |
| `targetObjectName` | _string_ | Name of the object. |
| `sourceBucketNameAndObjectName` | _string_ | Path of the file to be copied. |
| `conditions` | _CopyConditions_ | Conditions to be satisfied before allowing object copy. |

**Example**

```js
const conds = new Minio.CopyConditions()
conds.setMatchETag('bd891862ea3e22c93ed53a098218791d')
minioClient.copyObject('mybucket', 'newobject', '/mybucket/srcobject', conds, function (e, data) {
if (e) {
return console.log(e)
}
console.log('Successfully copied the object:')
console.log('etag = ' + data.etag + ', lastModified = ' + data.lastModified)
})
await minioClient.copyObject('mybucket', 'newobject', '/mybucket/srcobject', conds)
```

<a name="statObject"></a>
Expand Down Expand Up @@ -1490,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 @@ -1500,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 @@ -1534,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)
16 changes: 2 additions & 14 deletions examples/copy-object.js → examples/copy-object.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

Expand All @@ -29,16 +29,4 @@ const s3Client = new Minio.Client({
const conds = new Minio.CopyConditions()
conds.setMatchETag('bd891862ea3e22c93ed53a098218791d')

s3Client.copyObject(
'my-bucketname',
'my-objectname',
'/my-src-bucketname/my-src-objectname',
conds,
function (e, data) {
if (e) {
return console.log(e)
}
console.log('Successfully copied the object:')
console.log('etag = ' + data.etag + ', lastModified = ' + data.lastModified)
},
)
await s3Client.copyObject('my-target-bucketname', 'my-target-objectname', '/my-src-bucketname/my-src-objectname', conds)
Loading

0 comments on commit 8be0a48

Please sign in to comment.