def __init__(self, path, supports_mmap=True, readonly=False, debug=False): self.folder = path self.supports_mmap = supports_mmap self.readonly = readonly self._debug = debug self.locks = {} self.bucket = Bucket(path)
def get(self, request, *args, **kwargs): bucket_name = getattr(settings, "SAE_STORAGE_BUCKET_NAME", 'xkong1946') sae_bucket = SaeBucket(bucket_name) kv_bucket = Bucket() files = ['dict.txt', 'jieba.cache'] ret = [] for file_ in files: contents = sae_bucket.get_object_contents(file_) kv_bucket.save(file_, contents) ret.append(file_) # or return json return HttpResponse("done")
def get(self, request, *args, **kwargs): # Uncomment the following two lines to allow initial. done = {'done': '0', 'ret': '0'} # return self.render_to_response(done) bucket_name = getattr(settings, "SAE_STORAGE_BUCKET_NAME", 'xkong1946') sae_bucket = SaeBucket(bucket_name) kv_bucket = Bucket() files = ['dict.txt', 'jieba.cache'] ret = [] for file_ in files: contents = sae_bucket.get_object_contents(file_) kv_bucket.save(file_, contents) ret.append(file_) done = {'done': '0', 'ret': ret} return self.render_to_response(done)
class SAEFileStorage(WhooshStorage): supports_mmap = True def __init__(self, path, supports_mmap=True, readonly=False, debug=False): self.folder = path self.supports_mmap = supports_mmap self.readonly = readonly self._debug = debug self.locks = {} self.bucket = Bucket(path) def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self.folder) def create(self): return self def destory(self): self.clean() self.bucket.delete() def create_file(self, name, excl=False, mode='wb', **kwargs): if self.readonly: raise ReadOnlyError # Of course SAE bucket does not support `exclusive` mode. # fileobj = self.bucket.get_object(name) fileobj = SAEFile(name, mode, self.bucket) f = StructFile(fileobj, name=name, **kwargs) return f def open_file(self, name, **kwargs): fileobj = SAEFile.loadfile(name, mode='rb', bucket=self.bucket) f = StructFile(fileobj, name=name, **kwargs) return f def clean(self, ignore=False): if self.readonly: raise ReadOnlyError files = self.list() for fname in files: self.bucket.delete_object(fname) def list(self): return self.bucket.list() def file_exists(self, name): return self.bucket.exists(name) def file_modified(self, name): last_modified = self.bucket.stat_object(name)['last_modified'] mtime = datetime.datetime.strptime(last_modified, '%Y-%m-%dT%H:%M:%S.%f') return mtime def file_length(self, name): try: data = self.bucket.stat_object(name) except Exception as e: print e return 0 else: bytes_ = data['bytes'] return int(bytes_) def delete_file(self, name): if self.readonly: raise ReadOnlyError self.bucket.delete_object(name) def rename_file(self, oldname, newname, safe=False): if self.readonly: raise ReadOnlyError if self.file_exists(newname): if safe: raise NameError("File %r exists" % newname) else: self.delete_file(newname) self.bucket.put_object(newname, self.bucket.get_object_contents(oldname)) def lock(self, name): if name not in self.locks: self.locks[name] = threading.Lock() return self.locks[name] def temp_storage(self, name=None): name = "whooshtmp" tempstore = SAEFileStorage(name) return tempstore