Using Rust packages from a Motoko canister

My project is currently written entirely in Motoko. There is an SDK I would like to use (specifically the presigned function, to generate a presigned URL so my users can upload files directly to CDNs that support the S3 Object Storage interface), but the SDK isn’t available in Motoko. There is a Rust version, however.

I would like to avoid setting up a lambda on AWS just to generate this URL, so I’m thinking I could create a Rust canister that uses the SDK, add it to my project alongside my Motoko canisters, and have my Motoko canister call into that Rust canister directly when it needs to generate the URL.

Is this the right way to go about things? Are there examples you could point me to where a Motoko canister communicates with a Rust canister in the same project?

1 Like

It’s something that is being investigated, how to integrate Motoko with Rust code
But right now the way to do it is just separating the motoko and rust canisters and make calls between them

It is a bit more subtle than that. See this thread Exploration: calling Rust functions from Motoko.

If you use this and composite queries, because if the way motoko works, it is like it is on your canister. Performance should be roughly the same unless you are passing a ton of data across.

Reading through the thread linked above, it looks like the best way for me to proceed, at least at the current time, is to use Canpack. I’m getting node errors running the canpack command, so I’ve filed an issue there, but I’m reaching out to this community to see if anyone has had success lately using Canpack to expose a Rust crate’s functions to a Motoko canister, or if there’s an alternative best-practice by now.

Resolved the issue in this PR. The most common approach is to create a separate Rust canister and then call methods from Motoko. Canpack is essentially a wrapper which generates the Rust canister from a config file. Note that this only works for libraries which export a top-level canpack! macro. Here is more information for anyone reading this who is interested.