Worker and temp picture for Twitter
Hello, 'workers' is a fantastic thing, but I have a problem that has been bothering me for days, and I haven’t been able to solve it. I have a function that needs to send a tweet to a certain network. First, I need to send a picture in the standard way and it should return a mediaID. Since the picture is at a URL, I've been taking it into a buffer but the Twitter API 1.1 simply refuses to send it. What’s even worse, when I try the function on a system where I have the possibility to have the picture on the filesystem, it works without a problem. In desperation, I also sent it to R2 and tried from there, but without success. Does anyone have any solution to at least transfer it to some temporary place on the filesystem before sending??? Thanks
5 Replies
Workers have no filesystem, they're v8 isolates, a pretty high level abstraction, no node.js or anything.
What's your code? It could also be the Twitter API blocking Cloudflare IPs, but not sure. Depends what erro it returns
The last thing I did was this, however, there were hundreds of ideas and versions before that... and error
(log) Starting media upload to Twitter...
(log) Preparing FormData...
(log) Making fetch call for media upload...
(log) Fetch call completed.
(log) HTTP Status: 400
(log) Received data: {"request":"/1.1/media/upload.json","error":"media type unrecognized."}
(error) Failed to upload media to Twitter
Is there a reason why you're putting it into r2, and taking it out?
the mdn is a good source for JS Docs:
https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
form.append's third argument is just the file name, not an object or anything. It also only accepts a string or Blob (or any subclass of blob, like File), anything else is stringified
Unless you want to keep the r2 logic as a backup, you could get rid of all that and just pass the arrayBuffer through. You can create a Blob using it:
https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob
and then we can use that blob to append to the form data
Should hopefully clear up the media type unrecognized error, since you're properly giving it a type
My idea was to leave the picture in R2 as a backup, before this code I have a part of the code that writes the URL into my D1 database and then after that, we go with this and set images in R2. I changed the function call a bit but still without success. I think I will give up on the Worker and switch to Vercel but I really wanted to keep the server on Cloudflare because I really like the service 😦
My idea was to leave the picture in R2 as a backup, before this code I have a part of the code that writes the URL into my D1 database and then after thatThat's fine
I changed the function call a bit but still without successYou didn't change to what I suggested, if that's what you're saying. Still same broken code as before, and adding the base64 stuff doesn't help, you'd want to use the media_data param if you wanted to use base64, not formdata, but there's overhead so I would still recommend just using blobs/etc as I showed above Twitter API docs detail this: https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload