如何使用AWS API获取AWS每月发票PDF? [英] How to get AWS monthly invoice PDF using AWS API?

查看:451
本文介绍了如何使用AWS API获取AWS每月发票PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式下载会计部门每个月问我的PDF每月发票?

Is there a way to programmatically download the PDF monthly invoice the accounting department ask me every month?

我可以从AWS控制台获取它们(例如https://console.aws.amazon.com/billing/home?region=eu-west-3#/bills?year=2019&month=3) 有发票链接的地方.

I can get them from AWS Console (eg. https://console.aws.amazon.com/billing/home?region=eu-west-3#/bills?year=2019&month=3) Where there is a link to the invoice.

单击下载发票的那一刻,我可以看到对以下URL的HTTP请求: https://console.aws.amazon.com/billing/rest/v1.0/bill/invoice/generate?generatenew=true&invoiceGroupId=_SOME_ID_&invoicenumber=_SOME_ID_

The moment I click to download the invoice, I can see HTTP requests to the following URL: https://console.aws.amazon.com/billing/rest/v1.0/bill/invoice/generate?generatenew=true&invoiceGroupId=_SOME_ID_&invoicenumber=_SOME_ID_

然后对实际提供PDF文件的URL发出最终请求: https://console.aws.amazon.com/billing/rest/v1.0/bill/invoice/download?invoiceGroupId=_SOME_ID_&invoicenumber=_SOME_ID_

Then a final request to the URL that actually serves the PDF file: https://console.aws.amazon.com/billing/rest/v1.0/bill/invoice/download?invoiceGroupId=_SOME_ID_&invoicenumber=_SOME_ID_

我无法在AWS API上找到用于获取此类发票文档的文档(有一些用于开票报告和其他内容,但没有用于正式"文档的文档),所以我开始问自己是否可以使用?

I cannot find documentation on the AWS API to fetch such invoice document (there is some for billing reports and other stuff, but none for the "official" document) so I start to ask myself if it is even available?

在废弃AWS控制台之前(通过 Scrapy Selenium Puppeteer ),我要问社区;)

Before going scrapping the AWS Console (via Scrapy, Selenium, Puppeteer) I ask the community ;)

NB:我知道AWS可以通过电子邮件发送发票PDF,但我宁愿直接从AWS提取它,而不是从IMAP/POP电子邮件服务器提取.

NB: I know AWS can send the invoice PDF via e-mail but I rather fetch it directly from AWS instead of fetching from an IMAP/POP e-mail server.

推荐答案

您可以使用aws cli或aws sdk来获取json格式的数据.然后将json转换为pdf(此答案未涵盖).

You can use aws cli or aws sdk to get the data in json format. And then convert the json into pdf (not covered in this answer).

aws cli提供获取-成本和使用方式命令.通过摆弄参数,您可以获得与计费发票所产生的输出相匹配的输出.

aws cli provides get-cost-and-usage command. By fiddling with parameters you can get the output that matches the one that is produced by billing invoice.

此命令的用法示例:

    aws ce get-cost-and-usage \
        --time-period Start=2019-03-01,End=2019-04-01 \
        --granularity MONTHLY \
        --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" \
        --group-by Type=DIMENSION,Key=SERVICE

哪个会产生以下输出

{
  "GroupDefinitions": [
    {
      "Type": "DIMENSION",
      "Key": "SERVICE"
    }
  ],
  "ResultsByTime": [
    {
      "TimePeriod": {
        "Start": "2019-03-01",
        "End": "2019-04-01"
      },
      "Total": {},
      "Groups": [
        {
          "Keys": [
            "AWS Budgets"
          ],
          "Metrics": {
            "BlendedCost": {
              "Amount": "3.0392156805",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "3",
              "Unit": "USD"
            },
            "UsageQuantity": {
              "Amount": "155",
              "Unit": "N/A"
            }
          }
        },
        {
          "Keys": [
            "AWS CloudTrail"
          ],
          "Metrics": {
            "BlendedCost": {
              "Amount": "0",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "0",
              "Unit": "USD"
            },
            "UsageQuantity": {
              "Amount": "720042",
              "Unit": "N/A"
            }
          }
        },
...

AWS SDK

您还可以通过编程方式获取相同类型的数据.最简单的方法是使用aws sdk.请参阅要使用的SDK的文档.例如,可以在此处.

这篇关于如何使用AWS API获取AWS每月发票PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆