【问题标题】:How to to list files, delete files, write files, in an Azure blob container using Powershell and Shared Access Signature如何使用 Powershell 和共享访问签名在 Azure Blob 容器中列出文件、删除文件、写入文件
【发布时间】:2021-04-25 04:11:16
【问题描述】:

如何使用 Powershell 和共享访问签名在 Azure Blob 容器中列出文件、删除文件、写入文件

我在 blob 下创建了一个容器,并创建了一个具有完全访问权限的共享访问签名 (SAS)。 我想列出所有 *.csv 文件并首先删除它们,然后遍历我们所有的订阅并使用下面的行生成使用信息

Get-AzConsumptionUsageDetail -StartDate 2019-01-01 -EndDate $EndOfYear -IncludeMeterDetails | Export-Csv -Path "$URI$Subscription.csv"

如何列出/删除该容器中的所有 csv 文件,并让该 powershell 脚本将使用详细信息写入该容器。

【问题讨论】:

    标签: azure powershell containers blob


    【解决方案1】:

    您还应该提供storage account name

    那么第一步就是使用New-AzStorageContext cmdlet 生成上下文。示例如下:

    $sas_token="?sv=2019-12-12&ss=bfqt&srt=sco&sp=rwdlacupx&se=2021-01-21T09:46:06Z&st=2021-01-21T01:46:06Z&spr=https&sig=xxxx"
    
    $account_name = "your_storage_account_name"
    
    #generate the context
    $context = New-AzStorageContext -StorageAccountName $account_name -SasToken $sas_token
    

    接下来,您可以使用 Get-AzStorageBlob cmdlet 列出以 .csv 结尾的 blob。示例如下:

    #list blobs
    $myblobs = Get-AzStorageBlob -Container "your_container_name" -Blob *.csv -Context $context
    

    接下来,您可以使用Remove-AzStorageBlob cmdlet 删除这些 .csv blob:

    #delete these blobs
    $myblobs | Remove-AzStorageBlob -Context $context
    

    最后一个,当你通过Get-AzConsumptionUsageDetail -StartDate 2019-01-01 -EndDate $EndOfYear -IncludeMeterDetails | Export-Csv -Path "$URI$Subscription.csv"获取使用信息时,此时你就知道了Subscription.csv文件的路径。然后您可以使用Set-AzStorageBlobContent cmdlet 将此 .csv 文件上传到 blob 存储:

    #upload blob
    Set-AzStorageBlobContent -Container "the container name" -File "the file path, like c:\myfolder\dddd.csv" -Blob "specify the blob name, like usage.csv" -Context $context
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2020-09-04
      • 2021-10-13
      • 2021-05-02
      • 1970-01-01
      • 2021-05-08
      相关资源
      最近更新 更多