将文件保存到Blob存储

时间:2019-10-15 08:58:11

标签: json azure azure-storage-blobs azure-automation

我正在通过Azure自动化使用PowerShell运行API调用,我想将JSON文件直接保存到Blob存储中,而不必先将任何文件保存到本地计算机,然后将JSON文件传输到Blob存储中,因为此过程将被他人使用。是否可以将JSON文件直接保存到Blob存储而无需访问本地计算机?我很高兴更改实现将JSON文件直接保存到Blob存储的方法。

更新的API代码

$access_token ="Access_Token"
$URI =  "https://XXXXX"
$headers = @{“authorization” = “Bearer $access_token”} 
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$result = Invoke-RestMethod -Uri $URI -Headers $headers -ContentType $ContentType |ConvertTo-Json
$Result|ConvertFrom-Json| Select -ExpandProperty Forms

结果:

{
    "quiz": {
        "sport": {
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我认为我们不能将json文件直接导出到Blob存储,我们必须使用中间桥(临时存储)将文件传递到Blob存储。参考 Quickstart: Upload, download, and list blobs by using Azure PowerShell了解详情。

如果要在Azure DevOps管道中使用,则可以将json文件导出到代理中的暂存文件夹,然后使用Azure File Copy task将文件上传到Blog存储。

enter image description here

答案 1 :(得分:0)

您可以通过在Runbook中使用Azure Blob存储sdk来做到这一点。

首先,您需要将azure blob存储sdk中的Microsoft.WindowsAzure.Storage.dll导入为Runbook中的模块。请按照以下步骤操作:

1。获取Microsoft.WindowsAzure.Storage.dll。如果您不知道如何获取它,只需打开Visual Studio->创建.net框架控制台项目->然后右键单击控制台项目->管理Nuget程序包,然后搜索并下载天蓝色的blob存储sdk {{ 3}}

2。安装软件包后,构建控制台项目,然后在bin-> debug文件夹中,看到Microsoft.WindowsAzure.Storage.dll

3。将Microsoft.WindowsAzure.Storage.dll放入名为Microsoft.WindowsAzure.Storage.zip的zip文件中

4。转到azure门户->您的自动化帐户->在左窗格中,单击“模块”->添加模块->在步骤2中选择.zip文件。您需要等待几分钟模块完成上传(完成上传后,您可以找到 Microsoft.WindowsAzure.Storage模块的状态为可用),请参见以下屏幕截图:

WindowsAzure.Storage

其次,创建一个Powershell Runbook,并编写如下代码。在这里,我只是使用UploadText("your_string_text")方法上传一个字符串。 请注意,因为sdk提供了许多上传方法,因此您应该检查api的返回值是文本/流/字节格式,然后选择正确的上传方法,例如UploadFromStream(your_stream) / {{1 }}:

UploadFromByteArray(byte[] buffer, int index, int count)

然后,您可以运行Runbook,完成后,您可以看到在blob存储上创建的blob具有正确的内容。请注意,在运行期间,可能会出现错误,提示模块加载问题,但这并不重要。

测试结果blob是在blob存储上创建的。

enter image description here

如果要指定内容类型,只需添加以下代码行:Write-Output "start the test" Add-Type -Path "C:\Modules\User\Microsoft.WindowsAzure.Storage\Microsoft.WindowsAzure.Storage.dll" $access_token ="Access_Token" $URI = "https://XXXXX" $headers = @{“authorization” = “Bearer $access_token”} [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $result = Invoke-RestMethod -Uri $URI -Headers $headers -ContentType $ContentType |ConvertTo-Json $blob_content_to_upload = $Result|ConvertFrom-Json| Select -ExpandProperty Forms $account_name = "xxx" $account_key = "xxx" $container_name = "test1" $blob_name = "testfile3.txt" Write-Output "start communicate with blob storage" $creds = New-Object "Microsoft.WindowsAzure.Storage.Auth.StorageCredentials" -ArgumentList $account_name,$account_key $storageAccount = New-Object "Microsoft.WindowsAzure.Storage.CloudStorageAccount" -ArgumentList $creds,$true $cloudBlobClient = $storageAccount.CreateCloudBlobClient() $cloudBlobContainer = $cloudBlobClient.GetContainerReference($container_name) $myblob = $cloudBlobContainer.GetBlockBlobReference($blob_name) #note that the sdk also provides other methods like UploadFromStream(your_stream) / UploadFromByteArray(byte[] buffer, int index, int count), you can choose the proper method for your purpose. $myblob.UploadText($blob_content_to_upload) Write-Output("***the test is completed***")

相关问题