#fds

1 messages · Page 1 of 1 (latest)

hexed crowBOT
#

Hi @signal flame. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The knowledge sources don’t explicitly talk about a generic AWS S3 client, but there are two relevant hints:

  1. The R2 component uses the S3 API and its client is created once and reused inside the component code, not per-call. [S3 signed urls]
  2. Component “class-based clients” are typically instantiated once (or at least outside hot paths) and then reused, passing them into queries/mutations/actions. [Class clients]

From these, the recommended pattern in Convex is:

  • Yes, you can declare a long‑lived client object (like an S3 SDK client) outside individual function handlers, so you don’t create a new one on every invocation.
  • You’d usually wrap it in a class or helper and construct that once at module scope, then call its methods from your Convex functions, similar to:
// convex/s3Client.ts
import { S3Client } from "@aws-sdk/client-s3";

export const s3 = new S3Client({
  region: process.env.AWS_REGION!,
  // credentials etc.
});

and then import and reuse s3 from your actions/queries.

However, the docs I have don’t spell out lifecycle/connection-pooling details for S3 specifically, so I can’t confirm any Convex-specific constraints beyond this general pattern.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@signal flame Kapa.ai is still learning and improving, please let me know how I did by reacting below.

signal flame
#

@hexed crow how do you use it in actionh

hexed crowBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You’d use it in an action the same way you’d use any helper or client object: import it at module scope and call it from the action handler.

For example, following the pattern of helpers and external clients used in actions: [Actions; Splitting helpers]

// convex/myAction.ts
"use node";

import { action } from "./_generated/server";
// import your pre-created S3 client helper
import { s3 } from "./s3Client";

export const uploadSomething = action({
  args: {},
  handler: async (ctx, args) => {
    // use the shared S3 client here
    // e.g. await s3.send(new PutObjectCommand({ ... }));
    // then maybe write to Convex via ctx.runMutation(...)
  },
});

Key points from the docs that apply here:

  • Actions can run in Node.js by adding "use node" at the top of the file, which is required if you’re using Node-only SDKs. [Choosing runtime]
  • You can freely call TypeScript helpers or class-based clients from inside actions; only the ctx object is special. [Splitting helpers; Class clients]

The docs don’t show S3 specifically, but this is the documented pattern for using external libraries and helpers in actions.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: