选项1:字符串pathString = @"D:\Test";
原因是应用程序代码使用此身份基本只读访问权限到操作系统驱动器(D:\ 驱动器)。
参考 :Azure 应用服务上的操作系统功能 https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-available-operating-system-functionality#file-access
选项 2:Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
桌面文件夹格式为“%systemdrive%\users\%username%\Desktop”。根据Kudu环境页面,我们可以发现systemdrive变量值为'D:'。用户名变量值为“RD0003FF2AE3CC$”。由于 Azure Web App 在称为沙箱的安全环境中运行,因此用户“RD0003FF2AE3CC$”是一个不存在的虚拟用户。为了证明这一点,我们可以从 Kudu 调试控制台窗口找到所有用户名。这是我在 Kudu 中找到的用户。
D:\Users>dir
Directory of D:\Users
04/01/2017 11:36 PM <DIR> .NET v2.0
04/01/2017 11:36 PM <DIR> .NET v2.0 Classic
04/01/2017 11:36 PM <DIR> Classic .NET AppPool
06/01/2017 07:32 AM <DIR> OnStartAdmin
06/01/2017 07:27 AM <DIR> Public
06/01/2017 07:48 AM <DIR> SiteStorageAdmin
选项 3:System.IO.Path.GetTempPath()
每个 Azure Web App 都有一个临时的本地目录 (D:\local)。当该运行不再在虚拟机上运行时,该文件夹中的内容将被删除。该目录是存储应用程序临时数据的地方。不建议您的 Web 应用程序使用此文件夹。
参考 :Azure Web 应用程序沙箱 https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#file-system-restrictionsconsiderations
我们建议您在 Web 应用程序文件夹的根目录 (D:\home\site\wwwroot) 创建一个临时文件夹,并用它来存储临时数据。
string tempFolder = Server.MapPath("~/TEMP");
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
我的要求是将 blob 内容下载到用户计算机中
有两种方法可以实现您的要求。
首先,您可以获取 blob 内容并将其保存到内存流中。之后,您可以使用以下代码将此数据传输到客户端。
public ActionResult Download()
{
string blobName = "abc.png";
string containerName = "mycontainer";
string connectionString = "";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
MemoryStream ms = new MemoryStream();
// Save blob contents to a file.
blockBlob.DownloadToStream(ms);
ms.Position = 0;
return File(ms, "application/octet-stream", blobName);
}
如果 blob 大小非常大,您可以使用 SAS 生成 URL 并重定向到该 URL。客户端将直接从 blob 服务器下载文件。
public ActionResult Download()
{
string blobName = "abc.png";
string containerName = "mycontainer";
string connectionString = "";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
var sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
var sasBlobToken = blockBlob.GetSharedAccessSignature(sasConstraints);
var sasUrl = blockBlob.Uri + sasBlobToken;
return Redirect(sasUrl);
}