{"record":{"id":"fb0120f088ff8694","repo":"fullstackhero/dotnet-starter-kit","slug":"file-exceeds-max-size-of-rules-maxsizeinmb-mb-fb0120","errorCode":null,"errorMessage":"File exceeds max size of {rules.MaxSizeInMB} MB.","messagePattern":"File exceeds max size of (.+?) MB\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BuildingBlocks/Storage/S3/S3StorageService.cs","lineNumber":56,"sourceCode":"            throw new InvalidOperationException(\"Storage:S3:Bucket is required when using S3 storage.\");\n        }\n    }\n\n    public async Task<string> UploadAsync<T>(FileUploadRequest request, FileType fileType, CancellationToken cancellationToken = default) where T : class\n    {\n        ArgumentNullException.ThrowIfNull(request);\n\n        var rules = FileTypeMetadata.GetRules(fileType);\n        var extension = Path.GetExtension(request.FileName);\n\n        if (string.IsNullOrWhiteSpace(extension) || !rules.AllowedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))\n        {\n            throw new InvalidOperationException($\"File type '{extension}' is not allowed. Allowed: {string.Join(\", \", rules.AllowedExtensions)}\");\n        }\n\n        if (request.Data.Count > rules.MaxSizeInMB * 1024 * 1024)\n        {\n            throw new InvalidOperationException($\"File exceeds max size of {rules.MaxSizeInMB} MB.\");\n        }\n\n        var key = BuildKey<T>(SanitizeFileName(request.FileName));\n\n        using var stream = new MemoryStream([.. request.Data]);\n\n        var putRequest = new PutObjectRequest\n        {\n            BucketName = _options.Bucket,\n            Key = key,\n            InputStream = stream,\n            ContentType = request.ContentType\n        };\n\n        // Rely on bucket policy for public access; do not set ACLs to avoid conflicts with ACL-disabled buckets.\n        await _s3.PutObjectAsync(putRequest, cancellationToken).ConfigureAwait(false);\n        if (_logger.IsEnabled(LogLevel.Information))\n        {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/fullstackhero/dotnet-starter-kit/blob/3f2959e683e9f83f13e55e1678c9119f63c7e8e5/src/BuildingBlocks/Storage/S3/S3StorageService.cs#L38-L74","documentation":"S3StorageService.UploadAsync enforces the per-FileType maximum size (rules.MaxSizeInMB) by comparing request.Data.Count to the cap in bytes, throwing InvalidOperationException before the S3 PutObject. This prevents oversized multipart bodies and S3 storage cost blowups.","triggerScenarios":"UploadAsync with request.Data byte count greater than rules.MaxSizeInMB * 1024 * 1024 for the FileType.","commonSituations":"Large media uploads; S3 provider switched in while limits are lower than local; ETL/bulk import pushing whole datasets as one file; client-side pre-upload size checks not in place.","solutions":["Compress or split the payload before upload.","Increase rules.MaxSizeInMB for the FileType if the policy allows it.","Use S3 multipart upload / presigned direct upload for very large files.","Enforce the same size limit in the frontend before calling the API."],"exampleFix":"// before\nawait s3.UploadAsync(new UploadRequest { FileName = name, FileType = FileType.Image, Data = allBytes });\n\n// after\nvar rules = FileTypeMetadata.GetRules(FileType.Image);\nvar maxBytes = (long)rules.MaxSizeInMB * 1024 * 1024;\nif (allBytes.Length > maxBytes) return Results.Problem(\"Image too large\", statusCode: 413);\nawait s3.UploadAsync(new UploadRequest { FileName = name, FileType = FileType.Image, Data = allBytes });","handlingStrategy":"validation","validationCode":"var rules = FileTypeMetadata.GetRules(fileType);\nvar maxBytes = (long)rules.MaxSizeInMB * 1024 * 1024;\nif (data.Length > maxBytes)\n    throw new ArgumentException($\"File exceeds max size of {rules.MaxSizeInMB} MB.\");","typeGuard":"bool IsWithinSizeLimit(long byteCount, FileTypeRules rules) => byteCount <= (long)rules.MaxSizeInMB * 1024 * 1024;","tryCatchPattern":"try {\n    await s3Storage.UploadAsync(request);\n} catch (InvalidOperationException ex) when (ex.Message.Contains(\"exceeds max size\")) {\n    return Results.Problem(ex.Message, statusCode: 413);\n}","preventionTips":["Check Content-Length at the endpoint level and reject oversized requests before buffering.","Use presigned direct-to-S3 uploads for very large files so the API never holds the bytes.","Keep client-side and server-side size limits in sync."],"tags":["storage","s3","file-size","validation"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"3f2959e683e9f83f13e55e1678c9119f63c7e8e5","analyzedAt":"2026-09-15T22:20:53.684Z","contentChangedAt":"2026-09-15T22:20:53.684Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}