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

Time:10-02

how do you use resenter code heret to upload data to a storage account in the format of .csv

  1. Get token

  2. Get Request using invoke-rest method.

  3. export data in csv to a storage account

    $Request = Invoke-RestMethod @ParamRequest

    Request.value.properties | export-csv -path $path -NoTypeInformation

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

CodePudding user response:

Not really a PowerShell expert :) but essentially the idea is to create a Shared Access Signature (SAS) URL for the blob and then use that SAS URL for uploading the content directly into Azure Storage.

Here's what I came up with:

$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"
  • Related