繁体   English   中英

使用 Azure 模块在 PowerShell 中使用 Azure 函数将文件上传到 blob 存储

[英]upload file to blob storage with Azure functions in PowerShell using Azure module

要求是使用 Az 模块通过 PowerShell 中的 Azure 函数将文件存储在存储帐户中。 请帮忙。

$todaydate = Get-Date -Format MM-dd-yy
$LogFull = "AzureScan-$todaydate.log" 
$LogItem = New-Item -ItemType File -Name $LogFull
"  Text to write" | Out-File -FilePath $LogFull -Append

首先,您需要弄清楚函数的输入以及如何处理它。 如果您只想在每次执行 HTTP 触发的 Azure 函数时将文件写入 blob 存储,那么这很简单。

在通过 Azure Functions 使用 blob 存储时,有许多元素会发挥作用,但是你需要了解这些元素才能开发出有效的解决方案。

管理身份

可以为 Azure Functions 分配一个标识,以便您可以授予对 FunctionApp 本身的访问权限,而不必以用户身份进行身份验证。 这意味着您无需处理函数的身份验证方面即可访问存储帐户内容,您只需授予 FunctionApp 相关权限即可读取/写入/删除 blob 或存储内容。

AzureAD 中有许多内置的 RBAC 角色,您可以授予这些角色以访问存储帐户和 Blob 等。

您可以在此处找到有关 RBAC 权限的文档: https : //docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#storage

可以在此处找到有关如何在您的 functionApp 上激活托管标识的文档: https : //docs.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=dotnet#add-系统分配身份

存储帐户

以编程方式访问存储帐户内容取决于权限,但您可以使用与存储帐户关联的访问密钥,这些访问密钥在存储帐户级别提供访问权限

您可以在此处阅读有关访问密钥的信息: https : //docs.microsoft.com/en-us/azure/storage/common/storage-account-keys-manage?tabs=azure-portal#view-account- access-keys

请记住,应该采用最低权限访问,如果您泄露了您的密钥,那么有人可以访问您的数据。

PowerShell 命令

以编程方式访问存储帐户和写入 blob 数据所需的 PowerShell 命令总结如下

# Variables required - Fill these out
$storageAccountName = '<Insert Storage Account Here'
$containerName = '<Insert StorageContainer Name Here>'

# Set the context to the subscription you want to use
# If your functionApp has access to more than one subscription it will load the first subscription by default.
# Possibly a good habit to be explicit about context.
Set-AzContext -Subscription $subscription

# Get the Storage Account Key to authenticate
$storAccKeys = Get-AzStorageAccountKey -ResourceGroupName 'Storage-ResourceGroup' -Name $storageAccountName
$primaryKey = $storAccKeys | Where-Object keyname -eq 'key1' | Select-Object -ExpandProperty value

# Create a Storage Context which will be used in the subsequent commands
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $primaryKey

# Attempt to create a container in the storage account. Handle Error appropriately.
try {
    New-AzStorageContainer -Name $containerName -Context $storageContext -ErrorAction Stop
}
catch [Microsoft.WindowsAzure.Commands.Storage.Common.ResourceAlreadyExistException] {
    Write-Output ('Container {0} already exists in Storage Account {1}' -f $containerName, $storageAccountName)
    # Throw Here if you want it to fail instead.
}
catch {
    throw $_
}

# Upload your file here. This may vary depending on your function input and how you plan to have your functionApp work.
Set-AzStorageBlobContent -Container $containerName -File ".\PlanningData" -Blob "Planning2015"

您可以在此处查看有关Set-AzStorageBlobContent的文档以获取示例: https : Set-AzStorageBlobContent examples

通常,尽管您需要将文件上传到 blob 存储,但不能直接写入 blob 存储中的文件。

如果您需要阅读有关 Azure Functions 方面的更多信息,请参阅快速入门指南: https : //docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-电源外壳

或者 MS 文档的开发人员参考非常详细: https : //docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell?tabs=portal

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM