简体   繁体   English

powershell 上传blob到azure blob存储集内容类型

[英]powershell upload blob to azure blob storage set content type

I am making a ReST api call and uploading the JSON payload to azure blob storage using the below powershell.我正在调用 ReST api 并使用以下 powershell 将 JSON 有效负载上传到 azure blob 存储。

PoSh:波什:

        $Params = @{"URI" = 'https://myapiexample.com/api/data/'}
         
        $Result = Invoke-RestMethod @Params | ConvertTo-Json
        
        $context=New-AzStorageContext -StorageAccountName "mystorage" -StorageAccountKey ""
        
        $container=Get-AzStorageContainer -Name "input" -Context $context
        
        $content = [system.Text.Encoding]::UTF8.GetBytes($Result)
        $blobpath = "azblob/mydata.json"
        
        $container.CloudBlobContainer.GetBlockBlobReference($blobpath).UploadFromByteArray($content,0,$content.Length)
    
    $container.CloudBlobContainer.GetBlockBlobReference($blobpath).Properties.ContentType = "application/json"

$container.CloudBlobContainer.GetBlockBlobReference($blobpath).SetProperties()

I notice that when the blob is stored in azure blob storage the content-type is application/octet-stream whereas I would want it to be application/json.我注意到当 blob 存储在 azure blob 存储中时,内容类型是 application/octet-stream 而我希望它是 application/json。 The code I use is not working as expected.我使用的代码没有按预期工作。

After reproducing from my end, I could able to achieve your requirement using Set-AzStorageBlobContent .从我这边复制后,我可以使用Set-AzStorageBlobContent来满足您的要求。 Below is the complete code that is working for me that sets the content type of the blob.下面是为我工作的完整代码,用于设置 blob 的内容类型。

$url = "<YOUR_URL>"
$dest = "samplex.json"
Invoke-WebRequest -Uri $url -OutFile $dest
$context = New-AzStorageContext -StorageAccountName "<YOUR_ACCOUNT_NAME>" -StorageAccountKey "<YOUR_ACCOUNT_KEY>"
Set-AzStorageBlobContent -Context $context -Container "containers" -Blob "mydata.json" -File $dest -Properties @{"ContentType" = "application/json"};

RESULTS:结果:

在此处输入图像描述

在此处输入图像描述

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

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