Error when calling axios

Hi team,

I am getting the error:

     at null.callAxios (/tmp/__pdg__/dist/code/21efee1458d2dfcfa11a243b4b8d16ce2e894d8573eb41d1a6ab4f8f8cad6ea2/node_modules/.pnpm/@pipedream+platform@1.5.1/node_modules/@pipedream/platform/dist/axios.js:88:24)
    at Object.run (file:///tmp/__pdg__/dist/code/21efee1458d2dfcfa11a243b4b8d16ce2e894d8573eb41d1a6ab4f8f8cad6ea2/component.mjs:33:32)
    at null.executeComponent (/var/task/launch_worker.js:229:53)
    at MessagePort.messageHandler (/var/task/launch_worker.js:726:28)

in the following Pipedream step:

import { axios } from "@pipedream/platform";
import AWS from "aws-sdk";

export default defineComponent({
  props: {
    aws: {
      type: "app",
      app: "aws",
    },
  },
  async run({ steps, $ }) {
    const { accessKeyId, secretAccessKey } = this.aws.$auth;

    const S3_BUCKET_NAME = 'pacayadigital-creativelive';
    const S3_REGION = 'us-west-2';
    const s3 = new AWS.S3({
      accessKeyId: accessKeyId,
      secretAccessKey: secretAccessKey,
      region: S3_REGION,
    });

    const assets = steps.trigger.event.fields.Assets;
    let imageData = [];

    for (let i = 0; i < assets.length; i++) {
      try {
        let fileName = assets[i].filename.split(".")[0];
        let imageKey = 'campaigns/' + fileName;
        let imageUrl = assets[i].url;

        console.log('URL: ', imageUrl)

        const response = await axios({
        method: 'GET',
        url: imageUrl,
        responseType: 'arraybuffer',
      });

        if (response.status === 200) {
          const imageBuffer = response.data;
          const putObjectResponse = await s3.putObject({
            Bucket: S3_BUCKET_NAME,
            Key: imageKey,
            Body: Buffer.from(imageBuffer),
            ContentType: 'image/jpeg',
          }).promise();

          if (putObjectResponse) {
            imageData.push(imageKey);
            console.log(`Image uploaded to S3: s3://${S3_BUCKET_NAME}/${imageKey}`);
          } else {
            console.error('Error uploading image to S3');
          }
        } else {
          console.log('Response status: ', response.status);
          console.error('Failed to download the image from the URL.');
        }
      } catch (error) {
        console.error('[Some other error] Code is broken:', error);
      }
    }

    for (let i = 0; i < imageData.length; i++) {
      try {
        const imageUrl = `https://${S3_BUCKET_NAME}.s3.${S3_REGION}.amazonaws.com/${imageData[i]}.jpg`;
        console.log('Public S3 Image URL:', imageUrl);
      } catch (error) {
        console.error('Error retrieving public S3 URL:', error);
      }
    }

    return imageData;
  },
});

I’ve been troubleshooting this with Pi for a little while now and can’t seem to figure out what might be the issue other than it possibly being a bug?

Hi @drake,

I’m sorry you’re experiencing this issue. It seems like the error is related to the axios call. In your code, you’re using the regular axios import, but you should use the axios constructor from the @pipedream/platform package. Please update the axios call in your code like this:

const response = await axios(this, {
  method: 'GET',
  url: imageUrl,
  responseType: 'arraybuffer',
});

If you still face any issues or need further assistance, please don’t hesitate to ask or visit https://pipedream.com/support for more support options.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

Hi @pi - thanks! Your code fixed the error, but now I am getting an undefined response status from the GET request. Any idea why that might be happening?

Hi @drake,

Hi! I’m sorry to hear that you’re still experiencing issues. It’s possible that the response status is not being set correctly or there’s an issue with the request itself. To help you further, I’d need more information about the response you’re receiving.

Could you please share the complete response object or any error messages you’re encountering? This will help me better understand the issue and provide a more accurate solution.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

hi @pi - sure, the response is:

<Buffer ff d8 ff e1 00 18 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 00 00 00 00 00 00 00 00 ff ec 00 11 44 75 63 6b 79 00 01 00 04 00 00 00 64 00 00 ff e1 03 ... 282694 more bytes>

Hi @drake,

Hi there,

It looks like you’re receiving a Buffer object as a response. If you want to upload this Buffer to AWS S3, you can use the following code snippet inside your Pipedream component:

const imageBuffer = response.data;
const putObjectResponse = await s3.putObject({
  Bucket: S3_BUCKET_NAME,
  Key: imageKey,
  Body: imageBuffer,
  ContentType: 'image/jpeg',
}).promise();

if (putObjectResponse) {
  console.log(`Image uploaded to S3: s3://${S3_BUCKET_NAME}/${imageKey}`);
} else {
  console.error('Error uploading image to S3');
}

This code snippet assumes you have already configured the AWS S3 client (s3) and have the S3_BUCKET_NAME and imageKey variables set. The response.data should be the Buffer object you received.

If you need further assistance, please let me know or visit https://pipedream.com/support for more support options.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.