服务结构访问unc驱动器以存储文件.有可能吗



我有一个服务(.net核心(在服务结构(自托管(中运行。我试着在互联网上搜索,寻找一些资源来帮助我构建一个可能的解决方案,但到目前为止,未能获得可靠的来源。

我脑子里有两个可能的想法:1( 像对待任何其他驱动器一样对待unc驱动器,并使用StreamReader和Streamwriter。

2( 将文件装载为本地驱动器(以某种方式(,并使用基于此的StreamReader和Streamwrier

到目前为止,我还不完全理解第二个。

它能访问unc驱动器来存储和检索文件吗?我该怎么做呢?

将unc驱动器安装为本地驱动器可能是最简单的。

您可以编写装载到文件共享的脚本。将凭据放入配置中。将脚本作为服务的安装入口点运行。确保脚本运行幂等。

示例脚本:

$resourceGroupName = "<your-resource-group-name>"
$storageAccountName = "<your-storage-account-name>"
$fileShareName = "<your-file-share-name>"
# These commands require you to be logged into your Azure account, run Login-AzAccount if you haven't
# already logged in.
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
$storageAccountKeys = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName
$fileShare = Get-AzStorageShare -Context $storageAccount.Context | Where-Object { 
$_.Name -eq $fileShareName -and $_.IsSnapshot -eq $false
}
if ($fileShare -eq $null) {
throw [System.Exception]::new("Azure file share not found")
}
# The value given to the root parameter of the New-PSDrive cmdlet is the host address for the storage account, 
# <storage-account>.file.core.windows.net for Azure Public Regions. $fileShare.StorageUri.PrimaryUri.Host is 
# used because non-Public Azure regions, such as sovereign clouds or Azure Stack deployments, will have different 
# hosts for Azure file shares (and other storage resources).
$password = ConvertTo-SecureString -String $storageAccountKeys[0].Value -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList "AZURE$($storageAccount.StorageAccountName)", $password
New-PSDrive -Name <desired-drive-letter> -PSProvider FileSystem -Root "\$($fileShare.StorageUri.PrimaryUri.Host)$($fileShare.Name)" -Credential $credential -Persist

从那一刻起,你可以随心所欲地使用硬盘。例如,通过使用System.IO.File.ReadAllText和{所需的驱动器号}。

相关内容

最新更新