{"record":{"id":"fec1eacee2aa908b","repo":"OrchardCMS/OrchardCore","slug":"cannot-create-file-path-because-it-already-exists-fec1ea","errorCode":null,"errorMessage":"Cannot create file '{path}' because it already exists.","messagePattern":"Cannot create file '(.+?)' because it already exists\\.","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStore.cs","lineNumber":667,"sourceCode":"        }\n    }\n\n    // Reduces the need to call blob.FetchAttributes, and blob.ExistsAsync,\n    // as Azure Storage Library will perform these actions on OpenReadAsync().\n    public Task<Stream> GetFileStreamAsync(IFileStoreEntry fileStoreEntry)\n    {\n        return GetFileStreamAsync(fileStoreEntry.Path);\n    }\n\n    public async Task<string> CreateFileFromStreamAsync(string path, Stream inputStream, bool overwrite = false)\n    {\n        try\n        {\n            var blob = GetBlobReference(path);\n\n            if (!overwrite && await blob.ExistsAsync())\n            {\n                throw new FileStoreException($\"Cannot create file '{path}' because it already exists.\");\n            }\n\n            _contentTypeProvider.TryGetContentType(path, out var contentType);\n\n            var headers = new BlobHttpHeaders\n            {\n                ContentType = contentType ?? MediaTypeNames.Application.Octet,\n            };\n\n            await blob.UploadAsync(inputStream, headers);\n\n            return path;\n        }\n        catch (FileStoreException)\n        {\n            throw;\n        }\n        catch (Exception ex)","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStore.cs#L649-L685","documentation":"CreateFileFromStreamAsync validates that the destination blob does not already exist when overwrite=false, throwing this FileStoreException. It is a pre-condition check done client-side via ExistsAsync before any upload is attempted.","triggerScenarios":"Calling IFileStore.CreateFileFromStreamAsync(path, stream, overwrite: false) when a blob already exists at that path. Also occurs implicitly in CreateFileFromStream callers like media upload flows that pick an existing file name.","commonSituations":"Re-uploading media with the same filename without renaming; two users/tenants importing files with identical names concurrently; running an import twice; restoring content into a container that already holds the target blob.","solutions":["Pass overwrite: true if replacing the existing file is intended.","Check existence first with GetFileInfoAsync(path) and generate a unique name (append a number/timestamp) when the file exists.","Ensure media upload flows configured with uniqueness settings append unique suffixes to filenames.","Namespace paths by content item or folder to reduce name collisions."],"exampleFix":"// before\nawait fileStore.CreateFileFromStreamAsync(\"media/logo.png\", stream);\n// FileStoreException if logo.png exists\n// after\nvar existing = await fileStore.GetFileInfoAsync(\"media/logo.png\");\nvar name = existing != null ? $\"media/logo-{DateTime.UtcNow:yyyyMMddHHmmss}.png\" : \"media/logo.png\";\nawait fileStore.CreateFileFromStreamAsync(name, stream);","handlingStrategy":"validation","validationCode":"if (await fileStore.GetFileInfoAsync(path) != null)\n{\n    // pick a unique name or set overwrite: true\n    path = $\"{baseName}-{Guid.NewGuid():N}{extension}\";\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await fileStore.CreateFileFromStreamAsync(path, stream, overwrite: false);\n}\ncatch (FileStoreException)\n{\n    // file already exists — rename or overwrite\n}","preventionTips":["Check existence before creating with overwrite=false","Generate unique filenames on upload (timestamp/suffix)","Enable uniqueness rules in media upload settings","Namespace paths per content item/folder"],"tags":["azure-blob-storage","file-already-exists","file-storage"],"backgroundTag":"file-already-exists","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}