forked from minio/minio-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate copyObject api to ts (minio#1289)
- Loading branch information
1 parent
da24d11
commit 8be0a48
Showing
14 changed files
with
567 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.