Orchard Core Temporary File Storage - Prompt Templates
Relocate and Use Temporary File Storage
You are an Orchard Core expert. Generate configuration and code for Orchard Core's shared temporary file storage.
Guidelines
- Orchard Core 4.0 writes temporary files through
ITempDirectoryProvider(namespaceOrchardCore.FileStorage, packageOrchardCore.Infrastructure.Abstractions): in-progress media chunked uploads, TUS resumable uploads, deployment and recipe import/export archives (local and remote), workflow type import/export, recipes written to disk before tenant setup (tenant API recipe uploads), PDF text extraction for media indexing, and ClamAV buffering of non-seekable uploads. - With
Pathset, the defaultDefaultTempDirectoryProviderstores files under{Path}/{TenantName}/.... The tenant sub-directory is added automatically; never compose it yourself. Tenants cannot see or collide with each other's files. - Configure the base path with
OrchardCore:TempDirectory:Path(bound toTempDirectoryOptions). When it is omitted, the provider usesOrchardCore/TempunderEnvironment.SpecialFolder.LocalApplicationData({LocalApplicationData}/OrchardCore/Temp/{TenantName}), without a warning; that per-user folder must be private to the app identity. - If that location can't be determined or created, the provider falls back to a random
Directory.CreateTempSubdirectory("OrchardCore-")underPath.GetTempPath()and logs a warning with its path. That path changes when the provider is recreated, so configurePathwhen temporary files must survive tenant restarts or be shared by instances. - The provider never trusts a predictable tenant directory directly under a shared system temp folder (
/tmp/{TenantName},C:\Windows\Temp), which another local user could pre-create to read or replace files such as an importedRecipe.json. - On Unix-like systems, new directories get owner-only permissions (
700). - Why it exists: the OS temporary volume is often small (system disks, ephemeral container disks) and could fill up under concurrent large uploads. A configurable path can also be a shared volume, so an upload started on one instance can be completed on another.
- Point
Pathat a mounted file share (local disk, SAN/NAS, Azure Files over SMB, AWS EFS/FSx over NFS), never at object storage. Consumers need seekable local files (Seek/SetLength,ZipFile.ExtractToDirectory,PhysicalFileProvider). - TUS partial uploads go to a
TusUploadssub-directory of the same location.MediaOptions.TusTempPathwas removed. - Media cleanup such as the
TemporaryFileLifetimepurge runs against the configured path. - Module code that writes temporary files must inject
ITempDirectoryProviderinstead of callingPath.GetTempPath(). - Temporary storage does not scan files. Run user uploads through
FileCreationService(seeorchardcore-file-upload-security) before storing them permanently. - All C# classes must use the
sealedmodifier, except View Models.
Configuration
{
"OrchardCore": {
"TempDirectory": {
"Path": "/mnt/shared/octemp"
}
}
}
OrchardCore__TempDirectory__Path=/mnt/shared/octemp
The setting is global; every tenant gets its own sub-directory beneath it.
Mounting a File Share
Mount the share at the operating system or platform level, then set Path to the mount point.
# Linux or containers, Azure Files over SMB
sudo mkdir -p /mnt/octemp
sudo mount -t cifs //<account>.file.core.windows.net/<share> /mnt/octemp \
-o vers=3.0,username=<account>,password=<storage-key>,dir_mode=0777,file_mode=0777,serverino,nosharesock,actimeo=30
# Windows
New-SmbGlobalMapping -RemotePath "\\<account>.file.core.windows.net\<share>" -Credential $cred -Persistent $true -LocalPath Z:
{ "OrchardCore": { "TempDirectory": { "Path": "Z:\\octemp" } } }
- Azure App Service: Configuration → Path mappings → Azure Storage Mounts. Azure Container Apps: an
AzureFilevolume. SetPathto the mount path, for example/mounts/octemp. The platform manages the credentials and reconnection. - Make the mount writable by the application identity and keep it in the same region as the app.
DefaultTempDirectoryProvideruses plainSystem.IO, so any storage presented as a mounted filesystem path (local disk, SAN/NAS, SMB, NFS) works without code changes.
Multi-Instance TUS Uploads
With OrchardCore.Media.Tus, every chunk of an upload must reach storage that all instances share. Choose one:
- Set
OrchardCore:TempDirectory:Pathto a shared file share. - Enable sticky sessions (ARR affinity, ALB stickiness, HAProxy or NGINX sticky cookies).
- Enable Azure Media Storage (
OrchardCore.Media.Azure.Storage) or Amazon Media Storage (OrchardCore.Media.AmazonS3) together withOrchardCore.Media.Tus. Their startups replaceITusTempStorewithAzureBlobTusTempStoreorS3TusTempStore, so TUS partials go to object storage. Other temporary files still usePath.
Docker Compose with a shared volume:
services:
cms:
image: my-orchard-site
environment:
OrchardCore__TempDirectory__Path: /app/data/temp
volumes:
- octemp:/app/data/temp
volumes:
octemp:
# Use an NFS, Azure Files, or EFS backed volume driver for multiple instances.
Using ITempDirectoryProvider in a Module
| Member | Returns |
|---|---|
| GetRootDirectory() | The tenant-scoped root directory, created on demand. Use it as a base for tools that need a directory, such as the deployment TemporaryFileBuilder (OrchardCore.Deployment.Core). |
| GetTempFileName(string extension = null) | A unique file path (not created; the root directory is). The extension may include or omit the leading dot. |
| CreateTempSubdirectory(string prefix = null) | A newly created unique sub-directory, optionally prefixed, for extracting archives or a PhysicalFileProvider. |
using System.IO.Compression;
using Microsoft.AspNetCore.Http;
using OrchardCore.FileStorage;
namespace MyModule.Services;
public sealed class PackageImportService
{
private readonly ITempDirectoryProvider _tempDirectoryProvider;
public PackageImportService(ITempDirectoryProvider tempDirectoryProvider)
{
_tempDirectoryProvider = tempDirectoryProvider;
}
public async Task ImportAsync(IFormFile package, CancellationToken cancellationToken)
{
var archivePath = _tempDirectoryProvider.GetTempFileName(Path.GetExtension(package.FileName));
var extractPath = _tempDirectoryProvider.CreateTempSubdirectory("import-");
try
{
await using (var stream = File.Create(archivePath))
{
await package.CopyToAsync(stream, cancellationToken);
}
ZipFile.ExtractToDirectory(archivePath, extractPath);
// Process the extracted files.
}
finally
{
File.Delete(archivePath);
Directory.Delete(extractPath, recursive: true);
}
}
}
The provider only hands out paths. Create, write, and delete the files yourself; nothing tracks or expires them.
Replacing the Provider
DefaultTempDirectoryProvider is registered per tenant with TryAddSingleton. A replacement must still return real, seekable local filesystem paths that support directory enumeration, so there is no blob or S3 implementation.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OrchardCore.FileStorage;
using OrchardCore.Modules;
namespace MyModule;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
// ScratchVolumeTempDirectoryProvider is your own ITempDirectoryProvider implementation.
services.Replace(ServiceDescriptor.Singleton<ITempDirectoryProvider, ScratchVolumeTempDirectoryProvider>());
}
}
Upgrading from 3.x
- Before: temporary files were written to
Path.GetTempPath(), and TUS usedMediaOptions.TusTempPath. - After: set
OrchardCore:TempDirectory:Pathonce for all consumers and remove anyTusTempPathconfiguration. TUS partials move to{Path}/{TenantName}/TusUploads. - Replace
Path.GetTempPath()andPath.GetTempFileName()calls in custom modules withITempDirectoryProvider. - Without
Path, temporary files no longer go toPath.GetTempPath()/{TenantName}but to{LocalApplicationData}/OrchardCore/Temp/{TenantName}(or a random system temp subdirectory, with a logged warning). Grant the app identity a loaded user profile, or setPath, on hosts such as IIS app pools without a profile.
Troubleshooting
| Symptom | Check |
|---|---|
| Disk full during concurrent large uploads | Move Path to a larger volume. |
| TUS upload fails when load-balanced | Use a shared Path, sticky sessions, or a blob/S3 ITusTempStore. |
| Deployment import fails on a blob-mounted path | Object storage cannot provide seekable files; use Azure Files or EFS/FSx. |
| Permission denied creating temp files | Grant the app identity write access to the mount. |
| Warning about a unique temporary directory at startup | Local application data is unavailable to the app identity; set OrchardCore:TempDirectory:Path. |