Ejemplo n.º 1
0
class BaiDuFuse(LoggingMixIn, Operations):
    def __init__(self, accessToken, basedir):
        self.pcs = PCS(accessToken)
        self.appdir = "/apps/" + basedir + "/"

    def statfs(self, path):
        disk_info = self.pcs.info().json()
        bsize = 4096
        total = disk_info['quota'] / bsize
        used = disk_info['used'] / bsize
        free = total - used

        free_files = 1024
        return dict(f_bsize=bsize, f_frsize=bsize, f_blocks=total, f_bavail=free, f_bfree=free,
                    f_files=free_files, f_ffree=free_files, f_favail=free_files)

    def readdir(self, path, fh):
        dirpath = normpath(self.appdir + path)
        filelist = self.pcs.list_files(dirpath).json()['list']

        files = ['.', '..']
        for file in filelist:
            files.append(basename(file['path']))

        return files

    def getattr(self, path, fh=None):
        print path

        dir_attr = dict(st_mode=(S_IFDIR | 0755), st_nlink=2)
        if path == '/':
            return dir_attr

        filelist = self.pcs.list_files(normpath(self.appdir + dirname(path))).json()['list']

        for file in filelist:
            file_path = file['path']
            if file_path != normpath(self.appdir + "/" + path):
                continue

            if file['isdir'] == 1:
                return dir_attr

            return dict(st_mode=(S_IFREG | 0777), st_nlink=1, st_size=file['size'])

        raise FuseOSError(ENOENT)
Ejemplo n.º 2
0
class BaiduPCS(storage.DiskStorageBase):
	def _connect(self):
		self.pcs = PCS(self.config["access_token"])

	def _check_error(self, resp):
		if resp.ok:
			try:
				return resp.json()
			except:
				pass
		else:
			try:
				msg = resp.json()
			except:
				raise storage.StorageFail(resp.content)
			if "error_code" in msg:
				raise storage.StorageFail(msg["error_msg"])
			else:
				raise storage.StorageFail(resp.state)

	def free(self):
		resp = self.pcs.info()
		msg = self._check_error(resp)
		return msg["quota"] - msg["used"]

	def _upload(self, infile, path):
		return self.pcs.upload(path, infile, ondup="overwrite")

	def _download(self, path, outfile):
		resp = self.pcs.download(path)
		self._check_error(resp)
		for chunk in resp.iter_content(chunk_size=1024):
			if chunk:
				outfile.write(chunk)

	def _get_contents(self, resp):
		return resp["list"]

	def _is_dir(self, item):
		return item["isdir"]

	def _get_path(self, item):
		return item["path"]

	def _meta(self, path):
		return self.pcs.list_files(path)

	def _mkdir(self, path):
		return self.pcs.mkdir(path)

	def _delete(self, path):
		return self.pcs.delete(path)