您如何使用 rest 将 .csv 文件放入存储帐户?

how do you use rest to put a .csv file onto a storage account?

如何使用此处的resenter代码以.csv格式将数据上传到存储帐户

  1. 获取令牌

  2. 使用 invoke-rest 方法获取请求。

  3. 将 csv 格式的数据导出到存储帐户

    $Request = 调用-RestMethod @ParamRequest

    Request.value.properties |导出 csv -path $path -NoTypeInformation

API: https://docs.microsoft.com/en-us/rest/api/consumption/usage-details/list

不是真正的 PowerShell 专家 :) 但本质上的想法是为 blob 创建共享访问签名 (SAS) URL,然后使用该 SAS URL 将内容直接上传到Azure 存储。

这是我想出的:

$accountName = "storage-account-name"
$accountKey = "storage-account-key"
$containerName = "blob-container-name"
$blobName = "blob-name.csv"

# Get storage context
$context = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey

# Get Shared Access Signature (SAS) Token expiration time. I have set it to expire after 1 hour.
$sasExpiry = (Get-Date).AddHours(1).ToUniversalTime()

# Get a SAS Token with "write" permission that will expire after one hour.
$sasToken =  New-AzStorageBlobSASToken -Context $context -Container $containerName -Blob $blobName -Permission "w" -ExpiryTime $sasExpiry

# Create a SAS URL
$sasUrl = "https://$accountName.blob.core.windows.net/$containerName/$blobName$sasToken"

# Set request headers
$headers = @{"x-ms-blob-type":"BlockBlob"}

# Set request content (body)

$body = "This is the content I wish to upload"

#Invoke "Put Blob" REST API

Invoke-RestMethod -Method "PUT" -Uri $sasUrl -Body $body -Headers $headers -Content-Type "text/csv"