-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.py
70 lines (60 loc) · 2.37 KB
/
loop.py
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
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
import time
import boto3
import os
from qiniu import Auth, put_file
from utils.conf import UPLOAD_FOLDER
# Bucket = 'one-night'
from s3.get_url import Bucket
access_key = '4vtH5Qbea2P_qbji0mkxAqjEr4oulloHX4yDgnJP'
secret_key = 'no0C1cbm0nv26_zvVr8PPgFo2HmNk0DPtr_aDDKJ'
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def loop():
while True:
for f in os.listdir(UPLOAD_FOLDER):
if not 'tmp' == f.split('_')[-1]:
continue
else:
print 'start handle %s' % f
f_path = os.path.join(UPLOAD_FOLDER, f)
f_path_new = os.path.join(UPLOAD_FOLDER, f[:-4])
s3_client = boto3.client('s3')
s3_client.upload_file(f_path_new, Bucket, f[:-4])
os.rename(f_path, f_path_new)
print 'end handle %s' % f[:-4]
print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
def loop_qiniu():
while True:
for f in os.listdir(UPLOAD_FOLDER):
if 'tmp' not in f.split('_')[-1]:
os.remove(os.path.join(UPLOAD_FOLDER, f))
# print 'jump out and for one more time'
elif 'tmp0' == f.split('_')[-1]:
continue
elif 'tmp' == f.split('_')[-1]:
print 'start handle %s' % f
f_path = os.path.join(UPLOAD_FOLDER, f)
f_path_new = os.path.join(UPLOAD_FOLDER, f[:-4])
key = f[:-4]
q = Auth(access_key, secret_key)
token = q.upload_token(Bucket, key, 3600)
local_file = f_path
ret, _ = put_file(token, key, local_file)
try:
assert ret['key'] == key
except TypeError:
print 'ret has error'
continue
os.rename(f_path, f_path_new)
print 'end handle %s' % key
print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
else:
print '<----'
print u'为考虑到的情况'
print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
print '---->'
print 'pass this time while error', time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
if __name__ == '__main__':
loop_qiniu()