从Twitter媒体的URL读取readFileSync-node.js [英] readFileSync from an URL for Twitter media - node.js

查看:48
本文介绍了从Twitter媒体的URL读取readFileSync-node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用URL将媒体导入Twitter.但是我无法弄清楚如何使用URL,而不必下载图像,将其临时保存在服务器上,然后在...之后将其删除...

我正在使用Google Cloud存储图像,这就是为什么我无法直接访问它.

这就是我想要做的:

var data = require('fs').readFileSync('https://www.example.com/image.png');

//Create the Twitter client
const client = new Twitter({
  consumer_key: process.env.consumer_key_dev,
  consumer_secret: process.env.consumer_secret_dev,
  access_token_key: user.val().access.token,
  access_token_secret: user.val().access.secret
});

// Make post request on media endpoint. Pass file data as media parameter
return client.post('media/upload', {media: data}, function(error, media, response) {

  if (!error) {

    // If successful, a media object will be returned.
    console.log(media);

    // Lets tweet it
    var status = {
      status: sendTweet,
      media_ids: media.media_id_string // Pass the media id string
    }

    return client.post('statuses/update', status, function(error, tweet, response) {
      if (!error) {
        console.log(tweet);
      }
    });

  }
});

但是我得到这个错误:

ERROR: { Error: ENOENT: no such file or directory, open 'https://storage.googleapis.com/...

关于我应该怎么做的任何想法?

解决方案

这里的问题是,您尝试使用文件系统(fs)"模块访问文件系统中以外的位置.

您可以使用" http "或" https 模块.用法示例如下:

 const https = require('https');

function getImage(url, callback) {
    https.get(url, res => {
        // Initialise an array
        const bufs = [];

        // Add the data to the buffer collection
        res.on('data', function (chunk) {
            bufs.push(chunk)
        });

        // This signifies the end of a request
        res.on('end', function () {
            // We can join all of the 'chunks' of the image together
            const data = Buffer.concat(bufs);

            // Then we can call our callback.
            callback(null, data);
        });
    })
    // Inform the callback of the error.
    .on('error', callback);
}

// Then you 'get' your image like so:
getImage('https://www.example.com/image.png', function (err, data) {
    // Handle the error if there was an error getting the image.
    if (err) {
        throw new Error(err);
    }

    // Now you can use the rest of your code here:
    const client = new Twitter({ ... });
    // etc.
    // I've removed the rest because of size.
    return client.post(
        'media/upload',
        {media: data},
        function(error, media, response) {
            // ...
        }
    );
})
 

您不能同步执行http请求,因此我强烈建议您避免由于阻塞而避免同步请求( 解决方案

The problem you have here is that trying you're using the 'File System (fs)' module to access a location not in your file system.

You would solve the getting of the image using the 'http' or 'https' modules. An example of usage would be:

const https = require('https');

function getImage(url, callback) {
    https.get(url, res => {
        // Initialise an array
        const bufs = [];

        // Add the data to the buffer collection
        res.on('data', function (chunk) {
            bufs.push(chunk)
        });

        // This signifies the end of a request
        res.on('end', function () {
            // We can join all of the 'chunks' of the image together
            const data = Buffer.concat(bufs);

            // Then we can call our callback.
            callback(null, data);
        });
    })
    // Inform the callback of the error.
    .on('error', callback);
}

// Then you 'get' your image like so:
getImage('https://www.example.com/image.png', function (err, data) {
    // Handle the error if there was an error getting the image.
    if (err) {
        throw new Error(err);
    }

    // Now you can use the rest of your code here:
    const client = new Twitter({ ... });
    // etc.
    // I've removed the rest because of size.
    return client.post(
        'media/upload',
        {media: data},
        function(error, media, response) {
            // ...
        }
    );
})

You cannot do http requests synchronously, and I strongly advise you try to avoid synchronous requests because of blocking (https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/).

这篇关于从Twitter媒体的URL读取readFileSync-node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆