/
OSSManager.py
64 lines (52 loc) · 1.97 KB
/
OSSManager.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
import os
import oss2
import subprocess
from configparser import ConfigParser
from itertools import islice
from oss2.models import PartInfo
from oss2 import determine_part_size
import json
class OSSManager(object):
_bucket = {}
_config = {}
_endpoint = {}
_videoPath = ""
def __init__(self):
self._config = ConfigParser()
self._config.read('config.ini')
self._endpoint = self._config.get('OSS', 'endpoint')
self._bucketName = self._config.get('OSS', 'bucket.name')
self._videoPath = self._config.get('OSS', 'video.path')
credFile = self._config.get('CRED', 'file.name')
with open(credFile, 'r') as f:
cred = json.load(f)
auth = oss2.Auth(cred['AK'], cred['SK'])
self._bucket = oss2.Bucket(auth, self._endpoint, self._bucketName)
def getVideoPath(self):
return self._videoPath
def listFiles(self, path=''):
fileKeys = []
currentMarker = ''
while True:
listObjects = oss2.ObjectIterator(self._bucket, prefix=path, marker=currentMarker, max_keys=100)
for obj in listObjects:
if not obj.key.endswith('/'):
fileKeys.append(obj.key)
currentMarker = listObjects.next_marker
if not currentMarker:
break
return fileKeys
def downloadFile(self, path, localPath):
oss2.resumable_download(self._bucket, path, localPath,
store=oss2.ResumableDownloadStore(root='./tmp'),
multiget_threshold=20 * 1024 * 1024,
part_size=10 * 1024 * 1024,
num_threads=3)
def uploadFile(self, path, localPath):
oss2.resumable_upload(self._bucket, path, localPath,
store=oss2.ResumableDownloadStore(root='./tmp'),
multipart_threshold=20 * 1024 * 1024,
part_size=10 * 1024 * 1024,
num_threads=3)
def cleanDirs(self, key):
return ""