獲取阿裏雲同一賬號下多個bucket容量的腳本
阿新 • • 發佈:2017-09-28
sdk oss openapi 阿裏雲 接口調用
執行腳本前的準備工作請移步:http://chenx1242.blog.51cto.com/10430133/1968378 這篇文章裏看,下面就是完整的腳本內容。
#!/usr/bin/env python # -*- coding: UTF-8 -*- #這個腳本需要python 2.7+版本 from aliyunsdkcore import client from aliyunsdkcms.request.v20170301 import QueryMetricListRequest import time,re,os,datetime oss_list=[‘lechangecloud‘,‘lechangecloud-public‘,‘lechangehls‘,‘online-sz-private‘,‘online-sz-public‘,‘lechangecloud-public2‘] #這裏就是雲存儲bucket的列表 def gettodayoss(oss): global today_lechangecloud_GB clt = client.AcsClient(‘這裏填寫ak‘,‘這裏填寫sk‘,‘這裏填寫對應的區域‘) request = QueryMetricListRequest.QueryMetricListRequest() request.set_accept_format(‘json‘) request.set_Project(‘acs_oss‘) request.set_Metric(‘MeteringStorageUtilization‘) today_time = time.strftime(‘%Y-%m-%d‘) +" 04:00:00" timestamp_today = int(time.mktime(time.strptime(today_time, "%Y-%m-%d %H:%M:%S"))) * 1000 request.set_StartTime(timestamp_today) request.set_Dimensions("{\‘BucketName\‘:"+ oss +"}") request.set_Period(‘3600‘) result = clt.do_action_with_exception(request) #print result storage = int(re.split(‘"|}|:‘,result)[34]) today_lechangecloud_GB = (‘%.2f‘ %(storage/float(1073741824))) print oss+"在"+today_time+"的容量大小是:"+today_lechangecloud_GB+"GB。" def getyesterdayoss(oss): global yes_lechangecloud_GB clt = client.AcsClient(‘這裏填寫ak‘,‘這裏填寫sk‘,‘這裏填寫對應的區域‘) request = QueryMetricListRequest.QueryMetricListRequest() request.set_accept_format(‘json‘) request.set_Project(‘acs_oss‘) request.set_Metric(‘MeteringStorageUtilization‘) now_time = datetime.datetime.now() yes_time= now_time + datetime.timedelta(days=-1) yes_time_start = yes_time.strftime(‘%Y-%m-%d‘)+" 04:00:00" #yes_time_end = yes_time.strftime(‘%Y-%m-%d‘)+" 10:00:00" timestamp_yesterday_start = int(time.mktime(time.strptime(yes_time_start, "%Y-%m-%d %H:%M:%S"))) * 1000 #timestamp_yesterday_end = int(time.mktime(time.strptime(yes_time_end, "%Y-%m-%d %H:%M:%S"))) * 1000 request.set_StartTime(timestamp_yesterday_start) #request.set_EndTime(timestamp_yesterday_end) request.set_Dimensions("{\‘BucketName\‘:"+ oss +"}") request.set_Period(‘3600‘) yes_result = clt.do_action_with_exception(request) #print yes_result yes_storage = int(re.split(‘"|}|:‘,yes_result)[34]) yes_lechangecloud_GB = (‘%.2f‘ %(yes_storage/float(1073741824))) print oss+"在"+yes_time_start+"的容量大小是:"+yes_lechangecloud_GB+"GB。" def getdiff(oss): diff = float(today_lechangecloud_GB) - float(yes_lechangecloud_GB) print "今天與昨天同一時間的雲存儲差值是"+str(diff)+"GB。" if __name__ == "__main__": for oss in oss_list: oss = "‘%s‘"%oss gettodayoss(oss) getyesterdayoss(oss) getdiff(oss) print("整個腳本執行結束,感謝您的使用!")
執行效果如下:
新的知識點!
1)python獲取今天日期的方法:
>>> import time >>> time.strftime(‘%Y-%m-%d‘) ‘2017-09-28‘ >>> type(time.strftime(‘%Y-%m-%d‘)) <type ‘str‘>
2)python獲取昨天的日期的辦法:
>>> import datetime >>> now_time = datetime.datetime.now() >>> yes_time = now_time + datetime.timedelta(days=-1) >>> yes_time_nyr = yes_time.strftime(‘%Y-%m-%d‘) >>> print (yes_time_nyr) 2017-09-27 >>> type(yes_time_nyr) <type ‘str‘>
3)在 if __name__ == "__main__" 這一步的時候,我添加了一句 oss = "‘%s‘"%oss ,這是因為如果不加這句話,那麽傳遞給阿裏雲的bucket名稱格式不正確,正常的格式應該是:
{‘BucketName‘:‘lechangecloud‘}
不加這句話,得到的格式是缺少單引號的:
{‘BucketName‘:lechangecloud}
最後的最後,如果您覺得本文對您升職加薪有幫助,那麽請不吝贊助之手,刷一下下面的二維碼,贊助本人繼續寫更多的博文!
本文出自 “生活就是等待戈多” 博客,請務必保留此出處http://chenx1242.blog.51cto.com/10430133/1969364
獲取阿裏雲同一賬號下多個bucket容量的腳本