From 45c34bc2ef690f3ff6d6908413a9a5376d066276 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Tue, 23 Jan 2024 23:06:48 +0530 Subject: [PATCH] Create objectstorage-helper --- lib/objectstorage/lib/objectstorage-helper.ts | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/objectstorage/lib/objectstorage-helper.ts diff --git a/lib/objectstorage/lib/objectstorage-helper.ts b/lib/objectstorage/lib/objectstorage-helper.ts new file mode 100644 index 0000000000..b0f6eb88dc --- /dev/null +++ b/lib/objectstorage/lib/objectstorage-helper.ts @@ -0,0 +1,55 @@ + +// Import ObjectStorageClient as default export +import {ObjectStorageClient } from "./client"; +import * as requests from "./request"; + +// Rest of the imports remain the same +import * as fs from 'fs'; +import * as csv from 'fast-csv'; + + +export class ObjectStorageHelper { + private objectStorageClient: ObjectStorageClient; + + constructor() { + // Initialize the Object Storage client (you might want to pass credentials, etc.) + this.objectStorageClient = new ObjectStorageClient({}); + } + + public async readAndParseCsvFile( + namespaceName: string, + bucketName: string, + objectName: string + ): Promise { + try { + const getObjectRequest: requests.GetObjectRequest = { + namespaceName: namespaceName, + bucketName: bucketName, + objectName: objectName, + }; + + const response = await this.objectStorageClient.getObject(getObjectRequest); + + // Check if response contains a readable stream + if (response.value) { + // Pipe the readable stream from the OCI response to the CSV parser + response.value.pipe(csv.parse({ headers: true })) + .on('data', (row) => { + // Process each row of the CSV file + console.log(row); + }) + .on('end', () => { + console.log('CSV file parsing complete.'); + }); + } else { + console.log('No content in the response.'); + } + } catch (error) { + console.error('Error reading or parsing CSV file:', error); + } + } + } + + // Example usage + // const objectStorageHelper = new ObjectStorageHelper(); + // objectStorageHelper.readAndParseCsvFile('your-namespace', 'your-bucket-name', 'your-csv-file.csv'); \ No newline at end of file