Azure 成本管理 API 未返回 Blob 位置

问题描述 投票:0回答:1

我有一个 PowerShell 脚本,可以调用 Microsoft Azure API 进行成本管理。

我首先打电话:

https://management.azure.com/providers/Microsoft.Billing/enrollmentAccounts/*****5/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2023-08-01"

这将按预期返回 202:

状态代码:202
状态描述:已接受
内容:{}
原始内容:HTTP/1.1 202 已接受
编译指示:无缓存

然后我提取标题位置并进行轮询,直到得到 200

StatusCode        : 200
StatusDescription : OK
Content           :           {"id":"/providers/Microsoft.Billing/enrollmentAccounts/7****5/providers/Microsoft.CostManagement/costDetail   sOperationResults/ca4f*******-d8bff1dbf7c2","name":"ca4f85f****-d8bff1...
RawContent        : HTTP/1.1 200 OK
                Pragma: no-cache
                Vary: Accept-Encoding
                session-id: ae0a1b9b-2d21********6136a55bcb
                x-ms-request-id: d2f**********85-8d7f54a02fb7
                x-ms-correlation-request-id: 23b94e5d...
Forms             : {}
Headers           : {[Pragma, no-cache], [Vary, Accept-Encoding], [session-id, ae0a1b9b-2d21-4088-82f4-      106136a55bcb], [x-ms-request-id, d2f0ab18-e34c-*****4a02fb7]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 935

The problem is that I'm expecting a blob location to be returned but it isn't. 
Any guidance would be greatly appreciated. 

The blob location should be in the Manifest section 
i.e ......Manifest.blobs.blobLink


I 


I've tried different versions of the API be all return the same. 
I'm sure it's something I'm doing wrong.
azure azure-powershell azure-rest-api cost-management
1个回答
0
投票

根据此官方 MS 文档,范围设置为

enrollmentAccounts
的 Azure 成本管理 API 不包含请求中的任何 Blob 位置。如果你想检索 blob 资源的成本,你需要使用:-

'/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}'

其中 resourceGroupName 是包含 Blob 的资源组的名称。

我的调用成本管理 API 的 Powershell 示例脚本:-

$AppId = "xxxxxx-6d26a31435cb"
$AppSecret = "xxxxhhZ~bY."
$TokenURI = "https://login.microsoftonline.com/xxxxxx95/oauth2/token"
$Resource = "https://management.core.windows.net/"

$BodyRequest = "grant_type=client_credentials&client_id=$AppId&client_secret=$AppSecret&resource=$Resource"

$AccessToken = Invoke-RestMethod -Method Post -Uri $TokenURI -Body $BodyRequest -ContentType 'application/x-www-form-urlencoded'

$RequestURI = "https://management.azure.com/subscriptions/0151c365-f598-44d6-b4fd-e2b6e97cb2a7/resourceGroups/siliconstrg909-rg/providers/Microsoft.CostManagement/query?api-version=2023-03-01"

$Headers = @{
    "Authorization" = "Bearer " + $AccessToken.access_token
    "Content-Type" = "application/json"
}

# Updated request body structure
$UsageRequestBody = @{
    type = "Usage"
    timeframe = "MonthToDate"
    dataset = @{
        granularity = "Daily"
        aggregation = @{
            totalCost = @{
                name = "PreTaxCost"
                function = "Sum"
            }
        }
        grouping = @(
            @{
                type = "Dimension"
                name = "ResourceGroup"
            }
        )
    }
}

# Convert the updated payload to JSON
$UsageRequestBodyJson = $UsageRequestBody | ConvertTo-Json

# Make the request using Invoke-RestMethod
$UsageRequest = Invoke-RestMethod -Method Post -Uri $RequestURI -Headers $Headers -Body $UsageRequestBodyJson -ContentType 'application/json'

# Output the response
$UsageRequest.properties.rows
# Check for pagination
while ($UsageRequest.properties.nextLink) {
    $NextLink = $UsageRequest.properties.nextLink
    $UsageRequest = Invoke-RestMethod -Uri $NextLink -Headers $Headers -Method Get
    $UsageRequest.properties.rows
}

输出:-

enter image description here

在您的 enrollmentAccounts Rest API 中,尝试添加包含您的 blob 的 ResourceGroup name in value:-

$AppId = "xxxxxx26a31435cb"
$AppSecret = "xxxxxxxZ~bY."
$TokenURI = "https://login.microsoftonline.com/83xxxxxx92395/oauth2/token"
$Resource = "https://management.core.windows.net/"

$BodyRequest = "grant_type=client_credentials&client_id=$AppId&client_secret=$AppSecret&resource=$Resource"

$AccessToken = Invoke-RestMethod -Method Post -Uri $TokenURI -Body $BodyRequest -ContentType 'application/x-www-form-urlencoded'

$RequestURI = "https://management.azure.com/providers/Microsoft.Billing/enrollmentAccounts/*****5/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2023-08-01"

$Headers = @{
    "Authorization" = "Bearer " + $AccessToken.access_token
    "Content-Type" = "application/json"
}

# Updated request body structure
$UsageRequestBody = @{
    type = "Usage"
    timeframe = "MonthToDate"
    dataset = @{
        granularity = "Daily"
        filter = @{
            and = @(
                @{
                    or = @(
                        @{
                            dimensions = @{
                                name = "ResourceLocation"
                                operator = "In"
                                values = @("East US", "West Europe")
                            }
                        },
                        @{
                            tags = @{
                                name = "Environment"
                                operator = "In"
                                values = @("UAT", "Prod")
                            }
                        }
                    )
                },
                @{
                    dimensions = @{
                        name = "ResourceGroup"
                        operator = "In"
                        values = @("YourSampleResourceGroupName")  # Replace this with your actual resource group name
                    }
                }
            )
        }
    }
}

# Convert the updated payload to JSON
$UsageRequestBodyJson = $UsageRequestBody | ConvertTo-Json

# Make the request using Invoke-RestMethod
$UsageRequest = Invoke-RestMethod -Method Post -Uri $RequestURI -Headers $Headers -Body $UsageRequestBodyJson -ContentType 'application/json'

# Output the response
$UsageRequest
# Check for pagination
while ($UsageRequest.properties.nextLink) {
    $NextLink = $UsageRequest.properties.nextLink
    $UsageRequest = Invoke-RestMethod -Uri $NextLink -Headers $Headers -Method Get
    $UsageRequest
}

此外,还有一个单独的 Rest API 专用于 Azure Blob 存储,您可以尝试调用 Azure blob Powershell 命令 来检索 blob 属性,如下所示:-

# Connect to Azure account and set the desired subscription
Connect-AzAccount -TenantId xxxxxxxab9
Set-AzContext -SubscriptionId 'xxxxxxcb2a7'

# Get the storage account context
$ctx = Get-AzStorageAccount -ResourceGroupName "valleyrg87" -Name "siliconstorage678"

# Check if the context is retrieved properly
$ctx


# Get the storage account context
$ctx = Get-AzStorageAccount -ResourceGroupName "valleyrg87" -Name "siliconstorage678"

# Check if the context is retrieved properly
$ctx

# If context is retrieved successfully, proceed to get blob properties
if ($ctx) {
    # Get the blob reference
    $blob = Get-AzStorageBlob -Blob "hello_world.py" -Container "data" -Context $ctx.Context

    # Check if the blob is retrieved successfully
    $blob

    # If the blob is retrieved successfully, get blob properties
    if ($blob) {
        $properties = $blob.ICloudBlob.Properties
        $properties
    } else {
        Write-Host "Blob not found or there was an issue retrieving the blob."
    }
} else {
    Write-Host "Storage account not found"
}

输出:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.