Having trouble with uploading files to AWS S3 through Lambda

My goal is to upload files to S3 using Lambda functions. The code I am working with scrapes data from the web and creates a CSV file.

The original code (without AWS, just scraping and creating CSV) has been tested and works fine. I made slight modifications to make it work with Lambda and S3.

I have created an S3 bucket, Lambda layer, and IAM role (AmazonS3FullAccess), which I attached to Lambda.

I believe I have completed all necessary steps. When I click the test button, it shows "Success." However, when I check my S3 bucket, there is nothing there.

Is there something I might have missed? Thank you.

Here's my Lambda code:

const request = require('request');
const Iconv = require('iconv').Iconv;
const cheerio = require('cheerio');
const ObjectsToCsv = require('objects-to-csv');

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const today = new Date;
const date_string = `${today.getMonth()}/${today.getDate()}`;
const BUCKET_NAME = 'naverfinance';

exports.handler = async (event) => {
  
    class Stock {
      constructor(
        name, rank, price, change_inValue, change_inPercent, updown) {
        this.name = name;
        this.rank = rank;
        this.price = price;
        this.change_inValue = change_inValue;
        this.change_inPercent = change_inPercent;
        this.updown = updown;
      }
    }
    
    
    request({ url: 'https://finance.naver.com/sise/sise_quant.nhn', encoding: null }, function(response, body) {

        const euckr_to_utf8 = new Iconv('euc-kr', 'utf8');
        const koreanHTML = euckr_to_utf8.convert(body).toString();
        const $ = cheerio.load(koreanHTML);
        
        let name, rank, price, change_inValue, change_inPercent, updown;
    
        const table = $('.type_2').children('tbody').children('tr'); 
        
        for(let i = 1; i < table.length; ++i) {
            const tr = table[i];
            if(tr.children.length === 1) {
                continue;
            }
            
            const tds =  tr.children;
            for(let j = 0; j < tds.length; ++j) {
                const td = tds[j];
                if(td.type == 'text') {
                    continue;
                }
                
                const td_data = td.children;            
        
                if(j == 1) {
                    rank = td_data[0].data;
                }
                
                else if(j == 3) {
                    name = td_data[0].children[0].data;
                }
                
                else if(j == 5) {
                    price = parseInt(td_data[0].data.replace(/[,]/g,''));
                }
                
                else if(j == 7) {       
                    let data = 0;
                    
                    if(td_data[2].children != undefined) {
                        data = parseInt(td_data[2].children[0].data.replace(/[\n\t,]/g,''));
                    }
    
                    change_inValue = data;
    
                }
                
                else if(j == 9) {
                    change_inPercent = td_data[1].children[0].data.replace(/[\n\t%]/g,'');
                    
                    if (change_inPercent.indexOf('.') !== -1) { //decimal
                        change_inPercent = parseFloat(change_inPercent.replace(/,/g,''));
                    }
                
                    else { //integer
                        change_inPercent = parseInt(change_inPercent.replace(/,/g,''));
                    }
                    
                    if(change_inPercent < 0) {
                        updown = 'Decrease';
                        change_inValue = -change_inValue;
                    }
                    else if(change_inPercent > 0) {
                        updown = 'Increase';
                        change_inValue = change_inValue;
                    }
                    else {
                        updown = 'No Change';
                        change_inValue = 0;
                    }               
                    const stock = new Stock(
                        name, rank, price, change_inValue, change_inPercent, updown);
                    
                    s3.putObject({
                      Bucket: BUCKET_NAME,
                      Key: date_string,
                      ContentType: 'text/plain',
                      Body : 'test' //new ObjectsToCsv(stock).toString()
                    })
                }
            }
        }
    });
};

after removing async.

Response:
{
  "errorType": "Error",
  "errorMessage": "Bad argument.",
  "trace": [
    "Error: Bad argument.",
    "    at convert (/opt/nodejs/node_modules/iconv/index.js:103:11)",
    "    at Iconv.convert (/opt/nodejs/node_modules/iconv/index.js:63:12)",
    "    at Request._callback (/var/task/index.js:28:42)",
    "    at Request.self.callback (/opt/nodejs/node_modules/request/request.js:185:22)",
    "    at Request.emit (events.js:315:20)",
    "    at Request.EventEmitter.emit (domain.js:482:12)",
    "    at Request.<anonymous> (/opt/nodejs/node_modules/request/request.js:1154:10)",
    "    at Request.emit (events.js:315:20)",
    "    at Request.EventEmitter.emit (domain.js:482:12)",
    "    at IncomingMessage.<anonymous> (/opt/nodejs/node_modules/request/request.js:1076:12)"
  ]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: The specified schema for the model "superheroes" is missing and has not been registered. Please ensure the schema is properly defined and registered

After updating my server with nodemon, I encountered the following error: C:\Users\mikae\Desktop\Project\node-express-swig-mongo\node_modules\mongoose\lib\index.js:523 throw new mongoose.Error.MissingSchem ...

Response in JSON format from a solitary request

Trying to use async.parallel to fetch JSON data from two different tables in a single URL Encountering issues while debugging the error Current Scenario: There is a database with two tables Attempting to convert JSON response from both tables usin ...

React | What could be preventing the defaultValue of the input from updating?

I am working with a stateful component that makes a CEP promise to fetch data from post offices. This data is retrieved when the Zip input contains 9 characters - 8 numbers and a '-' - and returns an object with the desired information. Below is ...

Guide on using multer to retrieve files from digital ocean spaces

I successfully uploaded PDF files to Digital Ocean Spaces using a Node.js app. However, I am struggling with accessing these files and displaying them to the user. The code snippet below is from a tutorial on object storage file upload, but it does not pro ...

IBM Watson Conversation and Angular Integration

After recently diving into Angular, I am eager to incorporate a Watson conversation bot into my angular module. Unfortunately, I'm facing an issue with including a library in Angular. To retrieve the Watson answer, I am utilizing botkit-middleware-wat ...

When there is an error or no matching HTTP method, Next.js API routes will provide a default response

Currently, I am diving into the world of API Routes in Next.js where each path is structured like this: import { NextApiRequest, NextApiResponse } from "next"; export default async (req: NextApiRequest, res: NextApiResponse) => { const { qu ...

Regular Expression: The condition is met if the first character is either "%" or an alphanumeric character

When working with nodejs, I am using the following regular expression pattern: (/^(>|<|>=|<=|!=|%)?[a-z0-9 ]+?(%)*$/i This pattern is used to match only alphanumeric strings, allowing optional special characters as prefix and suffix. It works ...

Utilizing GraphicsMagick with Node.js to Extract Page Frames from Multi-Page TIF Files

I am currently working with a JavaScript script that can successfully convert a single page TIF file to JPEG. However, I am facing difficulties in determining whether "GraphicsMagick For Node" (https://github.com/aheckmann/gm) has the capability to extra ...

What is the most effective method for setting up numerous instances in AWS to run a unique Next.js application?

In my Next.js app, I am utilizing custom server options and AWS CodePipeline for CI/CD. The pipeline includes CodeBuild to build the app and AWS CodeDeploy to deploy it to all instances in an autoscaling group. This deployment is configured with CodeDeploy ...

Unable to render pages with ng-view in Angular.js

I am facing an issue with my Angular.js application where the pages are not loading when using ng-view. When I type the URL http://localhost:8888/dashboard, the pages should be displayed. Here is an explanation of my code: view/dashboard.html: <!DO ...

Limiting capture group options in Cucumber for dynamic parameters: A comprehensive guide

Currently, I am working on creating a test automation step-definition using Cucumber and WebdriverJS. The scenario involves validating certain actions based on different service methods. Scenario Outline : Validate functionality for different service metho ...

Is sendFile causing an error due to an invalid JSON format?

Whenever I try to send a file to a client for download, I encounter an exception saying that the JSON is invalid. Is there a better way to send the file, perhaps using res.download and setting the content as JSON? I want to avoid using AngularJS FileSaver ...

Is there a way to retrieve and gather all data from various scopes and then access them collectively?

I attempted to scrape information from a website using Node.JS + Cheerio + Axios. I was able to retrieve all the necessary data, but encountered an issue with returning the data from different scopes in order to receive it. Currently, I can only obtain the ...

What is preventing the use of this promise syntax for sending expressions?

Typically, when using Promise syntax, the following code snippets will result in the same outcome: // This is Syntax A - it works properly getUser(id).then((user) => console.log(user) // Syntax B - also works fine getUser(id).then(console.log) However ...

Node.js configuration for setting the maximum size of old space

As someone who is new to working with nodejs and mongodb, I encountered an issue while attempting to read about 100000 records from my mongodb using a nodejs application. Upon trying to retrieve the 100000 records, I came across the following error message ...

Is there a way to restrict the amount of RAM Nextjs uses during development?

I am currently working on a project using Nexjs with @mui/material. There is an ongoing issue regarding memory usage in Nextjs, which can be found on this GitHub link. Whenever I run the development server for a period of time, my laptop's RAM gets ...

Trouble removing old version of create-react-app with npm global uninstallation procedure

After taking a long break, I decided to create a new React application today. However, when I used the command npx create-react-app, an error popped up: The version of `create-react-app` you are running is 4.0.3, which is not the latest release (5.0.0). G ...

WebdriverIO: encountering the error message "Cannot read property length of undefined" while setting up cucumber and running tests

For a while now, I've been trying to dive into testing using the webdriverIO cucumber setup. I discovered some documentation and an example on the webdriverIO website that I'm attempting to implement: https://github.com/webdriverio/webdriverio/tr ...

What is the best way to streamline the sharing of functionality across my expressjs routes?

Let's say I have a project in Express.js with two routes: users.js and profiles.js. users.js: var express = require('express'); var router = express.Router(); /* GET users listing. */ router.get('/', function(req, res, next) { ...

Having trouble extracting a JSON object from a POST request in Express v4 with the help of body-parser?

Currently, I am delving into the world of server-side code and learning about Node.js and Express. However, I am facing some challenges when it comes to receiving and parsing a JSON object sent from a POST request. Despite checking various resources (linke ...