5fd203d0956165198.jpg_fo742.jpg

删除七牛CDN中wordpress未调用的对象

获取资源列表

#安装七牛sdk
pip install qiniu
#初始化sdk.py
from qiniu import Auth
from qiniu import BucketManager

access_key = '***********'
secret_key = '***********'

q = Auth(access_key, secret_key)
bucket = BucketManager(q)

#获取文件列表list.py
# -*- coding: utf-8 -*-
# flake8: noqa
from sdk import bucket
import sys
import json

reload(sys)
sys.setdefaultencoding( "utf-8" )


bucket_name = 'bucket-name'
# 前缀
prefix = None
# 列举条目
limit = 9999999
# 列举出除'/'的所有文件以及以'/'为分隔的所有前缀
delimiter = None
marker = None

ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)

print(info)
print info.text_body
print type(info.text_body)
info_str=str(info.text_body)
info_dict=json.loads(info_str)
for  img in range(len(info_dict['items'])):
    print info_dict['items'][img]['key']
#json.dumps(dict)
#print json.loads(info.text_body)
#print info_json
#info_json=json.load(info.text_body)


assert len(ret.get('items')) is not None


将获取的列表导入wordpress库中,例如表test:

#得到调用的对象,存入表test1
SELECT test.key,wp_posts.post_content from wp_posts,test
where locate(test.key,wp_posts.post_content)
GROUP BY test.key
select test.key  from test
where test.key not in (select test1.key from test1)

得到未被调用的对象列表unused.txt,如:

dogBodyLanguageOgg不给吃饭就不听话!!.ogg
dogBodyLanguageOgg中午我们吃啥.ogg
dogBodyLanguageOgg主人,有小贼入侵!!.ogg
dogBodyLanguageOgg你真残忍,欺负狗狗!!.ogg
dogBodyLanguageOgg你还好吗,亲爱的!!.ogg
dogBodyLanguageOgg再见,常来做客哦!!.ogg
dogBodyLanguageOgg别惹我,给我滚开!!.ogg

删除操作:

#delete.py
# -*- coding: utf-8 -*-
# flake8: noqa
from sdk import bucket

bucket_name = 'netinn'
file = open("unused.txt")
while 1:
    line = file.readline()
    if not line:
        break
    print line,type(line)
    ret, info = bucket.delete(bucket_name, line)
    print(info)

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code