Is it possible to fetch the AWS S3 Uploaded Image File Data and pass that file data through Twitter API from Node JS?

Viewed 140

I have a Scheduler where I can post scheduled tweets and threads and it will post through my twitter account the whole backend is based on Node JS.

So right now for Posting scheduled Tweets and Tweet threads with images I'm using mongoDB, storing the base64 string data, but I don't want to store such a big data to mongo, is it possible to upload images to AWS S3 first and then when time matches as Scheduled time that Image will be fetched again to my Node Server and pass it through the Twitter API?

In this way I will have to just store the AWS S3 image link to my MongoDB and get rid of a big string data.

API

        // THREAD SCHEDULER API
    app.post('/TweetThread/:id', async (req, res) => {
        const ID = req.params.id;
        const { texts, dateTimeGMT } = req.body;
        const filter = { _id: ObjectId(ID) };
        const findUser = await userCollection.findOne(filter);
        // GET TIMEZONE FROM INSTANT REQ
        const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

        const threadSchData = {
            texts,
            date: new Date(dateTimeGMT).toLocaleDateString(),
            time: new Date(dateTimeGMT).toLocaleTimeString(),
            timezone: timeZone,
            token: findUser.token,
            token_secret: findUser.token_secret,
            screen_name: findUser.screen_api
        }

        const result = await threadDataCollection.insertOne(threadSchData);
        res.json(result);
    });

Scheduler

        // TWITTER THREAD SCHEDULER
    const threadSch = cron.schedule('*/1 * * * *', async () => {
        const cursor = threadDataCollection.find({});
        const threadsArray = await cursor.toArray();
        // console.log(threadsArray);

        threadsArray.forEach(thread => {
            // Twitter Thread CONFIG
            const configThread = {
                consumer_key: process.env.CONSUMER_KEY,
                consumer_secret: process.env.CONSUMER_SECRET,
                access_token: thread.token,
                access_token_secret: thread.token_secret
            };
            // INSERTING THREAD DATA TO THREAD FUNCTION
            async function tweetThread() {
                const t = new TwitThread(configThread);
                await t.tweetThread(thread.texts);
            }

            // Calculating The Time According to Time ZONE
            let scheduledTime = new Date(thread.date + ' ' + thread.time);
            let now = new Date();
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            let day = now.getDate();
            let hour = now.getHours();
            let minute = now.getMinutes();
            let new_date = new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + '00');

            const countingTime = scheduledTime.toLocaleString("en-US", { timeZone: thread.timezone });
            const serverTime = new_date.toLocaleString("en-US", { timeZone: thread.timezone });
            // POSTING THREAD WHEN TIME MATCHED
            if (countingTime === serverTime) {
                tweetThread();
                console.log('Posted Thread Once');
                console.log('TRUE');
            }

        });

    });
    threadSch.start();

This is the request body where in media_data field I'm passing the string data from front-end

This is the request body where in media_data field I'm passing the string data from front-end

1 Answers

you need credentials to connect to aws from command line. Then you have only to run from the image path aws s3 cp your_file_name s3:\\your_bucket_name\your_file_name . Then save this your_file_name to MongoDB and upload it from here. There are more tricks to use cloudformation and to make s3 public so you can only point to the file from your html page. If you need help with any of the steps please write me.

Related