【问题标题】:Service fabric accessing unc drive to store a file. is it possible?访问 unc 驱动器以存储文件的服务结构。是否可以?
【发布时间】:2019-11-20 16:32:12
【问题描述】:

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

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

2) 将文件挂载为本地驱动器(不知何故)并使用基于this的StreamReader和Streamwrier

目前我还没有完全理解第二个。

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

【问题讨论】:

  • 添加上下文:我们使用 SDK 使用 azure 文件共享,直到另一端的软件由于某种原因无法连接它,他们不得不使用更传统的网络驱动器。跨度>

标签: c# azure microservices azure-service-fabric unc


【解决方案1】:

将 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}。

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2022-01-18
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多