[python]阿里云oss储存对象笔记
import oss2
class my_alioss:
def __init__(self, s_keyid, s_keysecret, s_ossurl, s_bucketname):
self.oj_auth = oss2.Auth(s_keyid, s_keysecret)
self.oj_bucket = oss2.Bucket(self.oj_auth, s_ossurl, s_bucketname)
self.name = s_bucketname
def download(self, s_alipath, s_localpath):
self.oj_bucket.get_object_to_file(s_alipath.replace('\\', '/').lstrip('/'), s_localpath.replace('\\', '/').strip('/'))
def upload(self, s_alipath, s_localpath):
self.oj_bucket.put_object_from_file(s_alipath.replace('\\', '/').lstrip('/'), s_localpath.replace('\\', '/').strip('/'))
def retstr_fileurl(self, s_alipath, i_timeout = 60):
return self.oj_bucket.sign_url('GET', s_alipath.replace('\\', '/').lstrip('/'),i_timeout)
def remove(self, s_alipath):
self.oj_bucket.delete_object(s_alipath.replace('\\', '/').lstrip('/'))
def moveto(self, s_from_alipath, s_to_alipath):
self.oj_bucket.copy_object(self.name, s_from_alipath.replace('\\', '/').lstrip('/'), s_to_alipath.replace('\\', '/').lstrip('/'))
self.remove(s_from_alipath)
def retbol_fileexist(self, s_alipath):
return self.oj_bucket.object_exists(s_alipath.replace('\\', '/').lstrip('/'))
def rettup_all(self):
ls_filelongpath = []
for obj in oss2.ObjectIterator(self.oj_bucket):
ls_filelongpath.append(obj.key)
return tuple(ls_filelongpath)
def rettup_alldirs(self):
ls_filelongpath = []
for obj in oss2.ObjectIterator(self.oj_bucket):
if obj.key.endswith('/'):
ls_filelongpath.append(obj.key)
return tuple(ls_filelongpath)
def rettup_allfiles(self):
ls_filelongpath = []
for obj in oss2.ObjectIterator(self.oj_bucket):
if not obj.key.endswith('/'):
ls_filelongpath.append(obj.key)
return tuple(ls_filelongpath)
def rettup_rootdirs(self):
s_files= []
for obj in oss2.ObjectIterator(self.oj_bucket, delimiter = '/'):
if obj.is_prefix():
s_files.append(obj.key)
return tuple(s_files)
def rettup_rootfiles(self):
s_files= []
for obj in oss2.ObjectIterator(self.oj_bucket, delimiter = '/'):
if not obj.is_prefix():
s_files.append(obj.key)
return tuple(s_files)