​获取阿里云同一账号下多个bucket容量的脚本

执行脚本前的准备工作请移步:http://chenx1242.blog.51cto.com/10430133/1968378 这篇文章里看,下面就是完整的脚本内容。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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 ( "整个脚本执行结束,感谢您的使用!" )


执行效果如下:

wKioL1nMakPjfPapAAKWBp6JSPc022.png


新的知识点!

1)python获取今天日期的方法:

1
2
3
4
5
>>>  import  time
>>> time.strftime( '%Y-%m-%d' )
'2017-09-28'
>>>  type (time.strftime( '%Y-%m-%d' ))
< type  'str' >

2)python获取昨天的日期的办法:

1
2
3
4
5
6
7
8
>>>  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名称格式不正确,正常的格式应该是:

1
{ 'BucketName' : 'lechangecloud' }


不加这句话,得到的格式是缺少单引号的:

1
{ 'BucketName' :lechangecloud}



最后的最后,如果您觉得本文对您升职加薪有帮助,那么请不吝赞助之手,刷一下下面的二维码,赞助本人继续写更多的博文!

wKioL1l16m3BMYDKAACPHEqd55Q687.jpg


 本文转自 苏幕遮618 51CTO博客,原文链接:http://blog.51cto.com/chenx1242/1969364


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值