1

I am tried to create a JS function which will upload a file in S3 bucket and then invoke that function from a karate feature file. But I am getting an error whatever i try to do.

js failed:
>>>>
01: read('D:\Personal\NTB Demo\UploadDemo\src\test\java\examples\users\s3-utils.js')
<<<<
org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:62 Invalid hex digit
read('D:\Personal\NTB Demo\UploadDemo\src\test\java\examples\users\s3-utils.js')
                                                              ^

Is there any possible solution or any suggestion regarding this task.

const AWS = require('aws-sdk');
const fs = require('fs');

async function uploadImageToS3(bucketName, prefix, filePath) {
  // AWS configuration
  const accessKeyId = 'your_access_key';
  const secretAccessKey = 'your_secret_key';
  const region = 'your_region';

  // Create S3 client
  const s3 = new AWS.S3({
    accessKeyId,
    secretAccessKey,
    region,
  });

  // Read file content
  const fileContent = fs.readFileSync(filePath);

  // Generate a unique key with the prefix and file name
  const key = prefix + 'image.jpg';

  // Upload file to S3 bucket
  const params = {
    Bucket: bucketName,
    Key: key,
    Body: fileContent,
  };

  try {
    await s3.upload(params).promise();
    return 'OK';
  } catch (error) {
    console.error('Error uploading image:', error);
    return 'Error';
  }
}

module.exports = {
  uploadImageToS3,
};

My JS file looks something like this.

and I am invoking this file from a karate feature file

Feature: Upload Image to S3 Bucket

Background:
  * def s3Utils = read('s3-utils.js')
  * def bucketName = 'your_bucket_name'
  * def prefix = 'your_prefix/'
  * def filePath = 'path_to_your_file/image.jpg'

Scenario: Upload Image to S3 Bucket with Prefix
  * def result = karate.call('s3Utils.uploadImageToS3', bucketName, prefix, filePath)
  * print 'Result:', result

  • refer https://stackoverflow.com/a/52100077/143475 - in short, don't use JS, use the AWS Java SDK and you will be able to do it. else, try building a CLI that you can call – Peter Thomas Jul 10 '23 at 12:20

0 Answers0