{"record":{"id":"cc27e3bbe9864b9b","repo":"OrchardCMS/OrchardCore","slug":"cannot-create-file-path-s3-service-threw-an-exception-ex","errorCode":null,"errorMessage":"Cannot create file '{path}', S3 service threw an exception: {ex.Message}","messagePattern":"Cannot create file '(.+?)', S3 service threw an exception: (.+?)","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.AmazonS3/AwsFileStorage.cs","lineNumber":305,"sourceCode":"                    throw new ExistsFileStoreException($\"Cannot create file '{path}' because it already exists.\");\n                }\n            }\n\n            var response = await _amazonS3Client.PutObjectAsync(new PutObjectRequest\n            {\n                BucketName = _options.BucketName,\n                Key = this.Combine(_basePrefix, path),\n                InputStream = inputStream,\n            });\n\n            if (!response.IsSuccessful())\n            {\n                throw new FileStoreException($\"Cannot create file '{path}'\");\n            }\n        }\n        catch (AmazonS3Exception ex)\n        {\n            throw new FileStoreException($\"Cannot create file '{path}', S3 service threw an exception: {ex.Message}\");\n        }\n\n        return path;\n    }\n\n    private static string NormalizePrefix(string prefix)\n    {\n        prefix = prefix.Trim('/') + '/';\n        if (prefix.Length == 1)\n        {\n            return string.Empty;\n        }\n\n        return prefix;\n    }\n}\n","sourceCodeStart":287,"sourceCodeEnd":322,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.AmazonS3/AwsFileStorage.cs#L287-L322","documentation":"AwsFileStore.CreateFileFromStreamAsync wraps any AmazonS3Exception raised while checking for an existing key or uploading the object (PutObjectAsync) into a FileStoreException with this message. It means the S3 service itself rejected or failed the write, as opposed to the file merely already existing (that raises ExistsFileStoreException) or an unsuccessful response. The underlying S3 error message is appended to help diagnose the real cause.","triggerScenarios":"Calling CreateFileFromStreamAsync when PutObjectAsync throws AmazonS3Exception — e.g. bucket does not exist, wrong credentials/region, insufficient s3:PutObject permission, key length over 1024 chars, invalid key characters, request timeout, or the pre-check ListObjectsV2Async fails.","commonSituations":"Misconfigured BucketName or region in the S3 options; IAM policy missing PutObject on the bucket/prefix; expired or wrong access/secret keys; VPC/firewall blocking the S3 endpoint; uploading to a path with invalid characters; bucket deleted or renamed after config was written.","solutions":["Verify BucketName, region and credentials in the AmazonS3Options — read the inner ex.Message for the specific S3 error (e.g. NoSuchBucket, AccessDenied, InvalidAccessKeyId).","Check the IAM policy grants s3:PutObject (and s3:ListBucket for the overwrite pre-check) on the bucket and prefix.","Confirm the bucket exists in the configured region and the app can reach the S3 endpoint (network/proxy/firewall).","Validate the target path is a valid S3 key: no leading/conforming invalid characters and length ≤ 1024 bytes."],"exampleFix":"// before\nawait fileStore.CreateFileFromStreamAsync(\"logs/app?.txt\", stream);\n// after\nvar safePath = \"logs/app-2026.txt\"; // valid S3 key, no invalid characters\nawait fileStore.CreateFileFromStreamAsync(safePath, stream);","handlingStrategy":"try-catch","validationCode":"if (string.IsNullOrWhiteSpace(path) || path.Length > 1024)\n{\n    throw new ArgumentException(\"Invalid S3 key\", nameof(path));\n}\nif (!overwrite && await fileStore.GetFileInfoAsync(path) != null)\n{\n    return; // already exists\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await fileStore.CreateFileFromStreamAsync(path, stream, overwrite);\n}\ncatch (FileStoreException ex)\n{\n    // ex.InnerException is AmazonS3Exception: inspect its ErrorCode\n    logger.LogError(ex.InnerException, \"S3 write failed for {Path}: {Code}\",\n        path, (ex.InnerException as AmazonS3Exception)?.ErrorCode);\n}","preventionTips":["Log the inner AmazonS3Exception ErrorCode — it names the exact S3 fault (NoSuchBucket, AccessDenied, etc.).","Validate keys: length ≤ 1024, no invalid characters, no leading '/'.","Verify bucket, region and IAM PutObject/ListBucket permissions at startup with a health check.","Use overwrite=true only when replacement is intended; pre-check existence otherwise."],"tags":["aws","s3","file-storage","file-write-failed","network"],"backgroundTag":"file-write-failed","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}