Not able to generate image from cloudwatch method get_metric_widget_image boto3 sdk

0

Hi All, I am trying to retreive a snapshot of a cloudwatch metric using the below the code snippet

import boto3
import base64
from PIL import Image
cw=boto3.client("cloudwatch",region_name='us-east-1')
response = cw.get_metric_widget_image(
    MetricWidget='''{
    "view": "timeSeries",
    "stacked": false,
    "metrics": [
        [ "AWS/EC2", "CPUUtilization", "InstanceId", "i-0fcbc91098629eca6" ]
    ],
    "width": 1061,
    "height": 177,
    "start": "-PT168H",
    "end": "P0H"    
    }'''
)
img = Image.frombytes("L", (1000, 177), response["MetricWidgetImage"])
img1 = list(img.getdata())
print(img1)

but I am getting the below error: ValueError: not enough image data Acc. to documentation, the output of get_metric_widget_image method is an base64 encoded string, but the output looks like a byte string without any decoding How can we generate the image? Thankyou

質問済み 2年前695ビュー
1回答
0
承認された回答

I was able to get through this, using the below code:

im = Image.open(io.BytesIO(response["MetricWidgetImage"]))
im.save("cw-1.png")
回答済み 2年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ