admin管理员组文章数量:1313144
I need to use an URL to import a media to Twitter. But I can't figure out how to use an URL without having to download the image, save it temporarily on the server and deleted after...
I'm using Google Cloud to store the images that's why I can't access it directly.
That's what I'm trying to do :
var data = require('fs').readFileSync('.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);
}
});
}
});
But I get this error :
ERROR: { Error: ENOENT: no such file or directory, open '/...
Any idea on how I should do it?
I need to use an URL to import a media to Twitter. But I can't figure out how to use an URL without having to download the image, save it temporarily on the server and deleted after...
I'm using Google Cloud to store the images that's why I can't access it directly.
That's what I'm trying to do :
var data = require('fs').readFileSync('https://www.example./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);
}
});
}
});
But I get this error :
ERROR: { Error: ENOENT: no such file or directory, open 'https://storage.googleapis./...
Any idea on how I should do it?
Share Improve this question asked Feb 2, 2018 at 16:04 PhilPhil 631 silver badge5 bronze badges1 Answer
Reset to default 7The 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./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/en/docs/guides/blocking-vs-non-blocking/).
本文标签: javascriptreadFileSync from an URL for Twitter medianodejsStack Overflow
版权声明:本文标题:javascript - readFileSync from an URL for Twitter media - node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741942982a2406249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论