【问题标题】:readFileSync from an URL for Twitter media - node.js从 Twitter 媒体的 URL 读取文件同步 - node.js
【发布时间】:2018-09-21 03:07:18
【问题描述】:

我需要使用 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/...

知道我应该怎么做吗?

【问题讨论】:

    标签: javascript node.js firebase google-cloud-storage fs


    【解决方案1】:

    您遇到的问题是您尝试使用“文件系统 (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 请求,我强烈建议你尽量避免因为阻塞(https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/)的同步请求。

    【讨论】:

    • 我尝试使用“请求”,但遇到编码问题。这节省了我很多时间。谢谢,伙计
    猜你喜欢
    • 2016-03-17
    • 2013-04-05
    • 2016-03-12
    • 2021-06-11
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多