Is there a way similar to S3 to limit the upload size for a multipart upload client-side in R2? In A

Is there a way similar to S3 to limit the upload size for a multipart upload client-side in R2? In AWS there seems to be a way to limit the size with an IAM role, which R2 doesn't have. Maybe I'm missing an option..

My current solution is to create the multipart upload and then generating the presigned URLs something like this:
// Size is sent over by the client. It is checked against the maximum allowed file size on the server
const chunkCount = Math.ceil(size / FILE_CHUNK_SIZE_BYTES);
const fileKey = "my/r2/file_key";
const uploadSessionId = "R2_MULTIPART_SESSION_ID";
const presignedUrls = [];

for(let x = 1; x <= chunkCount; x++) {
  presignedUrls.push(await r2.getPresignedChunkUrl(fileKey, uploadSessionId, x, (60 * 60 * 24), x < chunkCount ? FILE_CHUNK_SIZE_BYTES : (size % FILE_CHUNK_SIZE_BYTES)));
}


async getPresignedChunkUrl(key: string, uploadSessionId: string, partNumber: number, expiresInSec: number, contentLength: number) {
    try {
      const command = new UploadPartCommand({
        Bucket: this.bucket,
        Key: key,
        UploadId: uploadSessionId,
        PartNumber: partNumber,
        ContentLength: contentLength
      });

      return await getSignedUrl(this.client, command, {expiresIn: expiresInSec});
    }
    catch (e: any) {
      console.error(e?.message);
    }
    return null;
  }

Every chunk has the FILE_CHUNK_SIZE_BYTES as the ContentLength except the last one which is the remainder of bytes.

It works but it feels clumsy.
Was this page helpful?