Skip to content

Instantly share code, notes, and snippets.

@kapilt
Last active November 3, 2015 16:12
Show Gist options
  • Select an option

  • Save kapilt/2b596e458f93c001522d to your computer and use it in GitHub Desktop.

Select an option

Save kapilt/2b596e458f93c001522d to your computer and use it in GitHub Desktop.
fetch info about an s3 bucket using cloudwatch api
from datetime import datetime, timedelta
import boto3
import pprint
import os
def main():
bucket = os.environ.get('BUCKET')
s = boto3.Session()
c = s.client('cloudwatch')
response = c.get_metric_statistics(
Namespace='AWS/S3',
MetricName='NumberOfObjects',
Dimensions=[
{'Name': 'BucketName',
'Value': bucket},
{'Name': 'StorageType',
'Value': 'StandardStorage'},
],
StartTime=datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(10),
EndTime=datetime.now().replace(hour=0, minute=0, second=0, microsecond=0),
Period=60*24*24,
Statistics=['Average'])
pprint.pprint(response)
response = c.get_metric_statistics(
Namespace='AWS/S3',
MetricName='BucketSizeBytes',
Dimensions=[
{'Name': 'BucketName',
'Value': bucket},
{'Name': 'StorageType',
'Value': 'StandardStorage'},
],
StartTime=datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(10),
EndTime=datetime.now().replace(hour=0, minute=0, second=0, microsecond=0),
Period=60*24*24,
Statistics=['Average'])
pprint.pprint(response)
if __name__ == '__main__':
main()
{u'Datapoints': [],
u'Label': 'NumberOfObjects',
'ResponseMetadata': {'HTTPStatusCode': 200,
'RequestId': '74e3ac17-8245-11e5-835f-cd3fdfe1e32d'}}
{u'Datapoints': [{u'Average': 580472213.0,
u'Timestamp': datetime.datetime(2015, 11, 1, 19, 12, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 562677776.0,
u'Timestamp': datetime.datetime(2015, 10, 28, 0, 0, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 548243812.0,
u'Timestamp': datetime.datetime(2015, 10, 24, 0, 0, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 576847723.0,
u'Timestamp': datetime.datetime(2015, 11, 1, 0, 0, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 554575731.0,
u'Timestamp': datetime.datetime(2015, 10, 26, 0, 0, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 566266447.0,
u'Timestamp': datetime.datetime(2015, 10, 28, 19, 12, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 573510466.0,
u'Timestamp': datetime.datetime(2015, 10, 30, 19, 12, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 551270255.0,
u'Timestamp': datetime.datetime(2015, 10, 24, 19, 12, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 558712576.0,
u'Timestamp': datetime.datetime(2015, 10, 26, 19, 12, tzinfo=tzutc()),
u'Unit': 'Bytes'},
{u'Average': 570043657.0,
u'Timestamp': datetime.datetime(2015, 10, 30, 0, 0, tzinfo=tzutc()),
u'Unit': 'Bytes'}],
u'Label': 'BucketSizeBytes',
'ResponseMetadata': {'HTTPStatusCode': 200,
'RequestId': '752cc2ba-8245-11e5-914e-53aa03c7f588'}}
@kapilt

kapilt commented Nov 3, 2015

Copy link
Copy Markdown
Author

For some reason the NumberOfObjects query returns zero datapoints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment