DEV Community

Object Storage Service with Pre-Signed URLs Explained: Node.js Uploads for 3 Tenants

A signed URL is useful only after the application has decided exactly which tenant may write which training artifact, for how long, and under what retention rule. For a gaming service, I would keep the authorization decision in the API and let object storage handle the bytes.

That split gives players and build workers direct uploads without making the API a file proxy, while preserving an audit trail that can reproduce why an object was retained or deleted.

Short answer: use short-lived, single-object signed URLs, derive the object key from server-side tenant and job identifiers, and store the retention decision beside the artifact metadata. Never accept a client-supplied path as an authorization boundary. Measure upload latency, rejected requests, orphaned objects, and policy reconciliation time before committing to an implementation.

The experiment: what did the simple upload path get wrong?

The tempting design is one upload endpoint: receive a multipart file, write it from the game service, and return a download link later. It is easy to explain. It also couples API capacity to the size and duration of every training artifact. A 2 GB replay bundle and a 40 KB avatar now compete for the same request workers.

The experiment constraint is reproducibility. Given tenantId, trainingJobId, and a policy version, an operator should be able to reconstruct the object key, its intended expiry, and the deletion deadline. A random client-provided filename cannot provide that.

A signed URL improves the data path, but it does not decide who gets a URL. Keep the URL disposable. I use three paths in the design: upload, read, and lifecycle reconciliation. They share metadata, not credentials. The API authorizes the first two; a scheduled worker checks the third against the recorded policy version.

That separation is the important result. The trade-off is an extra metadata transaction and a reconciliation worker, but those costs buy a durable explanation for every object that enters or leaves training. A direct bucket upload with no intent record is easier to ship and harder to audit after a job is retried, a tenant is suspended, or a retention rule changes halfway through a run.

Before copying it, measure the maximum artifact size, concurrent uploads per tenant, URL issuance latency, and the time between policy changes and cleanup. Those numbers determine queue pressure and retry behavior better than a storage feature checklist.

Which object storage service supports pre-signed URLs for both paths?

The boundary is the authorization record, not the bucket name and not a folder-looking prefix. A useful record has an immutable tenant identifier, job identifier, object key, content length limit, media type, policy version, creation time, and retention deadline. The object store metadata can repeat some fields, but the database record is the control-plane source.

A client may propose a filename for display. It must not choose the final key. For example, the service can construct tenants/{tenantId}/jobs/{jobId}/artifacts/{artifactId} after checking that the caller owns the job. The artifact ID should be generated by the service. A path traversal string, a second tenant ID, or a changed job ID then has no effect on the authorization decision.

The URL should grant one operation on one object for a short window. Treat it as a bearer credential: anyone holding it can use it within the granted scope and expiry. Do not put it in logs, analytics query strings, chat messages, or exception text. A download URL is not a substitute for checking the requester when the file is sensitive; issue it only after the API performs that check. Authorization belongs before signing.

A small TypeScript contract

The storage adapter below is intentionally boring. Each provider-specific implementation maps these methods to its own signing API, while the application retains the same security contract.

type ArtifactIntent = {
  tenantId: string;
  trainingJobId: string;
  artifactId: string;
  sizeBytes: number;
  contentType: string;
  policyVersion: string;
  retainUntil: Date;
};

type SignedObject = {
  url: string;
  expiresAt: Date;
};

interface ObjectSigner {
  signUpload(input: { key: string; contentType: string; maxBytes: number; expiresInSeconds: number; }): Promise<SignedObject>;
  signDownload(input: { key: string; expiresInSeconds: number; }): Promise<SignedObject>;
}

function artifactKey(intent: ArtifactIntent): string {
  return `tenants/${intent.tenantId}/jobs/${intent.trainingJobId}/artifacts/${intent.artifactId}`;
}

async function createUploadIntent(
  intent: ArtifactIntent,
  signer: ObjectSigner,
): Promise<SignedObject> {
  if (!Number.isSafeInteger(intent.sizeBytes) || intent.sizeBytes <= 0) {
    throw new Error("invalid artifact size");
  }
  const key = artifactKey(intent);
  return signer.signUpload({
    key,
    contentType: intent.contentType,
    maxBytes: intent.sizeBytes,
    expiresInSeconds: 300,
  });
}

The sizeBytes check here is an application guard, not proof that the client uploaded the declared number of bytes. The completion step should inspect the stored object, compare size and content type with the intent, and mark the artifact ready only after that comparison. A failed or abandoned upload remains an orphan candidate, not a valid training input.

For downloads, authorize the current caller against the stored tenant and job record before signing. For uploads, authorize the job before signing and bind the expected content type and size to the intent. Do not reuse an upload URL for a download or stretch one URL across several objects. That is the trade-off.

How should retention survive retries and policy changes?

Retention needs a versioned rule, because a deadline calculated only at upload time becomes opaque when the policy changes. Store the policy version and the resulting retainUntil value with the artifact. A reconciler can then distinguish three cases:

  • the artifact is still within its recorded deadline,
  • the current policy extends it,
  • or the object is past the applicable deadline and may be deleted.

Deletion should be idempotent. A retry after a timeout must not create a new artifact record, and a repeated delete should converge on the same state. Keep an audit event for the decision, including tenant, artifact ID, policy version, actor or worker identity, and decision time. Do not record signed URLs in that event.

The worker should list candidates from the metadata store, then verify the object key from the record before deleting. A broad prefix delete is operationally convenient and a poor tenant-isolation boundary. If a tenant is suspended, the API can stop issuing new URLs immediately; lifecycle cleanup still needs its own authorization and review path.

What should be tested before production?

The high-value tests are cross-tenant tests. Create an upload intent for tenant A, replace the tenant and job identifiers in the request, and verify that no URL is issued. Repeat with a valid URL but a different content type, an oversized body, an expired URL, and a second completion request. Then test a download after the caller loses access to the job.

Property-based tests are useful for key construction: generated identifiers must remain inside the expected tenant namespace, and the same intent must always produce the same key. Add an integration test for a failed upload followed by reconciliation. The useful metric is not only success rate; it is how quickly invalid or abandoned artifacts stop being eligible for training.

The decision rule is narrow. Pick an object storage implementation whose signing mechanism can enforce the required operation, expiry, and object identity; whose lifecycle behavior can be observed; and whose credentials can be scoped away from arbitrary tenant paths. Keep the adapter replaceable.

This approach is a poor fit for tiny, public assets where a CDN cache and immutable URLs are enough; it adds authorization and metadata work that those assets do not need. For private training artifacts, the extra control is the point. Vendor lock-in is less threatening when policy, metadata, tests, and keys belong to your application.

Further reading

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.