OrchardCMS/OrchardCore · error · FileStoreException
Cannot create file ' ' because it already exists.
Error message
Cannot create file '{path}' because it already exists. What it means
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.
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.
Example fix
// before
await fileStore.CreateFileFromStreamAsync("media/logo.png", stream);
// FileStoreException if logo.png exists
// after
var existing = await fileStore.GetFileInfoAsync("media/logo.png");
var name = existing != null ? $"media/logo-{DateTime.UtcNow:yyyyMMddHHmmss}.png" : "media/logo.png";
await fileStore.CreateFileFromStreamAsync(name, stream); Defensive patterns
Strategy: validation
Validate before calling
if (await fileStore.GetFileInfoAsync(path) != null)
{
// pick a unique name or set overwrite: true
path = $"{baseName}-{Guid.NewGuid():N}{extension}";
} Try / catch
try
{
await fileStore.CreateFileFromStreamAsync(path, stream, overwrite: false);
}
catch (FileStoreException)
{
// file already exists — rename or overwrite
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Cannot create directory because the path
- Cannot copy file ' ' because a file already exists in the…
- Cannot copy file ' ' to ' '.
- Cannot get file stream because the file
- Cannot create file ' ' because it already exists.
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/fec1eacee2aa908b.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStore.cs:667
}
}
// Reduces the need to call blob.FetchAttributes, and blob.ExistsAsync,
// as Azure Storage Library will perform these actions on OpenReadAsync().
public Task<Stream> GetFileStreamAsync(IFileStoreEntry fileStoreEntry)
{
return GetFileStreamAsync(fileStoreEntry.Path);
}
public async Task<string> CreateFileFromStreamAsync(string path, Stream inputStream, bool overwrite = false)
{
try
{
var blob = GetBlobReference(path);
if (!overwrite && await blob.ExistsAsync())
{
throw new FileStoreException($"Cannot create file '{path}' because it already exists.");
}
_contentTypeProvider.TryGetContentType(path, out var contentType);
var headers = new BlobHttpHeaders
{
ContentType = contentType ?? MediaTypeNames.Application.Octet,
};
await blob.UploadAsync(inputStream, headers);
return path;
}
catch (FileStoreException)
{
throw;
}
catch (Exception ex)View on GitHub (pinned to 4306c0717f)