我有一个在Service Fabric(自托管)中运行的服务(.net核心)。我尝试在Internet上搜索以找到一些资源来帮助我构建可能的解决方案,但到目前为止未能获得可靠的可靠资源。
我在脑海中有两个想法:1)与其他驱动器一样对待unc驱动器,并使用StreamReader和Streamwriter。
2)将文件作为本地驱动器挂载(以某种方式使用,并基于this使用StreamReader和Streamwrier
到目前为止,我还不完全理解第二个。
是否能够访问unc驱动器来存储和检索文件?我将如何去做?
将unc驱动器安装为本地驱动器可能是最简单的。
您可以script挂载到文件共享。将凭据放入配置中。将脚本作为服务的setup entry point运行。确保脚本运行idempotent。
示例脚本:
$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
和{desired-drive-letter}。