Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create objectstorage-helper for reading a CSV from the object storage bucket #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/objectstorage/lib/objectstorage-helper.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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');