You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure FileShare调用ShareFileClient.UploadAsync报ResourceNotFound错误

根因分析

该404报错并非由待上传的新文件不存在触发,而是上传路径的上级目录uploaded/未在新部署的Azure文件共享中预先创建。
Azure Files SDK的ShareFileClient.UploadAsync方法不会自动创建路径中的层级父目录,只要路径任意一级父目录不存在,就会直接抛出ResourceNotFound异常。你此前的旧环境已提前建好uploaded目录,因此代码可正常运行,新存储账户的文件共享未提前创建对应目录就会触发该报错。

解决方案

快速验证方案

直接登录Azure门户,找到新存储账户下的unistad-files文件共享,手动创建uploaded目录,即可快速验证问题是否解决。

代码兼容方案

在原有代码中增加目录存在性校验逻辑,不存在时自动创建目录,避免后续换环境再出现同类问题,修改后的代码片段如下:

public async Task<bool> SaveFileUploaded(string directory, string file, Stream fileStream)
{
    // Get a reference to the directory
    ShareDirectoryClient directoryClient = _shareClient.GetDirectoryClient(directory);

    // 新增:校验目录是否存在,不存在则创建
    if (!await directoryClient.ExistsAsync())
    {
        await directoryClient.CreateAsync();
    }

    // Get a reference to a file object
    ShareFileClient destFileCLient = directoryClient.GetFileClient(file);

    fileStream.Position = 0;

    // Start the copy operation
    await destFileCLient.UploadAsync(fileStream);
    

    // Ensure that the file was uploaded
    if (await destFileCLient.ExistsAsync())
    {
        return true;
    }
    else
    {
        _log.LogError($"File {file} wasn't saved into the uploaded directory : {directory}.", file, directory);
        return false;
    }                              
}

额外排查点

如果按上述操作后仍有报错,可检查两项配置:

  • 确认新存储账户中名为unistad-files的文件共享已正常创建,无命名拼写错误
  • 确认代码使用的存储账户密钥、SAS令牌或身份凭证,拥有对应文件共享的读写、目录创建权限

内容的提问来源于stack exchange,提问作者Ricardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 19:39:05