Error uploading file to IPFS via Infura Endpoint

I’m trying to POST an uploaded image to IPFS via the Infura endpoint. I’ve tried multiple different ways of attacking this but I think the issue comes back to the way files are saved in Bubble. Is there a way to POST a file to an API?

My JS code:
const request = require(‘request’)
const fs = require(‘fs’)
const imageData = fs.readFileSync(properties.file)
const options = {
method: ‘POST’,
url: ‘https://ipfs.infura.io:5001/api/v0/add’,
headers: {
‘Content-Type’: ‘application/octet-stream’,
‘Authorization’: AUTH
},
body: imageData
}
request(options, (error, response, body) => {
if (error) {
console.error(error)
} else {
console.log(body)
return body
}
})
return {
url: JSON.stringify(request)
}

I was able to successfully POST the file with Postman using a form-data body. This was the response
{
“Name”: “white_sneaker.jpg”,
“Hash”: “QmYZmAxWUxTmmjAn5oSybB5LMLtzn7mCLv8MC3Ax8xUuJS”,
“Size”: “40070”
}

Error Message
Error: ENOENT: no such file or directory, open 'https://s3.amazonaws.com/appforest_uf/<id>/white_sneaker.jpg' at Object.openSync (fs.js:498:3) at Object.readFileSync (fs.js:394:35) at eval (eval at build_function (/var/task/index.js:53:12), <anonymous>:12:26) at /var/task/index.js:519:17 at run_fn (/var/task/u.js:645:18)

where are you running this code?

fs is made for operations on filesystem, not to fetch remote resources

I’m creating this in the javascript plugin workflow tab

<id>

Is not parameterized?

It is. I just exlcuded the unique id generated for brevity

I assume you are working on a server side action in a plugin.

The fs module is intended for interacting with the file system.
readFileSync supports URL but only with the file:// protocol.

From the error it looks like you are trying to read https://s3.amazonaws.com/appforest_uf/<id>/white_sneaker.jpg, that is not a file in the file system.

If you need to fetch remote resources you need to use context.request or another library.

Got it. Is there any documentation on how to use context.request? I’m new to javascript and trying to pull together code from what I’ve been reading online.

simple example:

const { statusCode, statusMessage, body } = context.request(
	"https://example.com",
	{
		method: "GET", // your method here
		headers: {
			//your headers here
		},
	}
);

The editor says they are using a node request, the current version used for bubble backend actions is 14, so you can find more documentation here HTTP | Node.js v14.21.2 Documentation

edit: updated the url

Thanks. I’ll try this and revert back with any questions

Do you know why I would get an Unauthorized response when using the code provide versus a successful response when using Postman with the same credentials?


have you added the Content-Type header?

I did and still getting the same error. I ended up just using the API connector for now since that’s basically what I’m trying to do. I’ll share what I did for others that want to post to IPFS.

For others.

I used the Bubble API Connector plugin to post a file to IPFS via the Infura endpoint. On the left hand side I clicked Add another API and filled in the details below. Set the POST to an Action so you can use it in a workflow.


Searched for the API call in the workflow editor
Screenshot 2022-12-22 110427
Added a static file but a dynamic link will work

Set the state to see the result but you can store it on a “thing”

1 Like

if you only need an api call the api connector is definetly better and easier

I see you are using basic auth.
In the request you should use the auth option instead of the headers.
From the docs:

auth Basic authentication i.e. 'user:password' to compute an Authorization header.

That might be the source of the issue I’ll test it later and report back.

I tried using auth with user:password and still got an error project id required. I might return to this at a different date, but I will stick with the API Connector for now.