I have to read a CSV file stored in IBM cloud object through IBM cloud function which I did using Nodejs. Now I have to apply any logic to that file and write it back through IBM cloud function.
How can I do that (code please)?
Underlining is the code to read CSV file from IBM cloud object storage using 'Read-file' action in the IBM Cloud function.
const COS = require('ibm-cos-sdk').S3;
async function main(params) {
const IAM_API_KEY = process.env['__OW_IAM_NAMESPACE_API_KEY']
const ENDPOINT = 's3.eu-gb.cloud-object-storage.appdomain.cloud'
const BUCKET = 'my-bucket'
const FILE = 'Sample.csv'
const config = {
endpoint: ENDPOINT,
apiKeyId: IAM_API_KEY
}
const cos = new COS(config);
const options = { Bucket: BUCKET }
async function getItem(bucketName, itemName) {
console.log(Retrieving item from bucket: ${bucketName}, key: ${itemName}
);
try{
const data = await cos.getObject({
Bucket: bucketName,
Key: itemName
}).promise()
if (data != null) {
console.log('File Contents: ' + Buffer.from(data.Body).toString())
console.log("hellllllo");
}
return data
}
catch(e){
console.error(`ERROR: ${e.code} - ${e.message}\n`)
}
}
console.log(await getItem(BUCKET,FILE))
}