Exemplo n.º 1
0
    async def copy_file(self, bucket: str, key: str, to_bucket: str,
                        to_key: str, force: bool =False) -> None:
        """拷贝文件

        :param bucket: 待拷贝文件空间名
        :param key: 待拷贝文件名
        :param to_bucket: 目标空间名
        :param to_key: 目标文件名
        :param force: force标记,bool类型,默认为False

        :return: None

        详见:https://developer.qiniu.com/kodo/api/1254/copy
        """
        src_encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        dst_encoded_entry_uri = get_encoded_entry_uri(to_bucket, to_key)
        path = "/copy/{}/{}/force/{}".format(
            src_encoded_entry_uri, dst_encoded_entry_uri,
            "true" if force else "false")
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "http://rs.qiniu.com" + path

        async with self.httpclient.post(url, headers=headers) as resp:
            await raise_for_error(resp)
Exemplo n.º 2
0
    async def move_file(self, bucket: str, key: str, to_bucket: str,
                        to_key: str, force=False) -> None:
        """移动文件

        :param bucket: 待移动文件空间名
        :param key: 待移动文件名
        :param to_bucket: 目标空间名
        :param to_key: 目标文件名
        :param force: force标记,bool类型,默认为False

        :return: None

        详见:https://developer.qiniu.com/kodo/api/1288/move
        """
        src_encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        dst_encoded_entry_uri = get_encoded_entry_uri(to_bucket, to_key)
        path = "/move/{}/{}/force/{}".format(
            src_encoded_entry_uri, dst_encoded_entry_uri,
            "true" if force else "false")
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "http://rs.qiniu.com" + path

        async with self._httpclient.post(url, headers=headers) as resp:
            assert resp.status == 200, "HTTP {}".format(resp.status)

        return
Exemplo n.º 3
0
    def _get_operation_string(self, code: str, *args) -> str:
        assert code in self._opcode2arglen, "非法的操作码: {}".format(code)
        assert len(args) in self._opcode2arglen[code], "操作参数错误"

        if code in ("stat", "delete"):
            return "op=/{}/{}".format(code, get_encoded_entry_uri(*args))
        if code == "rename":
            code = "move"
            args = (*args[:2], args[0], *args[2:])
        if code in ("move", "copy"):
            src = get_encoded_entry_uri(args[0], args[1])
            dst = get_encoded_entry_uri(args[2], args[3])
            force = "true" if len(args) == 5 and args[4] else "false"
            return "op=/{}/{}/{}/force/{}".format(code, src, dst, force)
Exemplo n.º 4
0
    async def delete_file(self, bucket: str, key: str) -> None:
        """删除文件

        :param bucket: 待删除文件所在空间名
        :param key: 待删除文件名

        详见:https://developer.qiniu.com/kodo/api/1257/delete
        """
        encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        path = "/delete/{}".format(encoded_entry_uri)
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "http://rs.qiniu.com" + path

        async with self.httpclient.post(url, headers=headers) as resp:
            await raise_for_error(resp)
Exemplo n.º 5
0
    async def delete_file_after_days(self, bucket: str, key: str,
                                     days: int) -> None:
        """设置文件在指定天数后删除

        :param bucket: 待设置文件所在空间名
        :param key: 待设置文件名
        :param days: 文件存活的天数,设置为0表示无限存活期

        详见:https://developer.qiniu.com/kodo/api/1732/update-file-lifecycle
        """
        encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        path = "/deleteAfterDays/{}/{}".format(encoded_entry_uri, days)
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "http://rs.qiniu.com" + path

        async with self.httpclient.post(url, headers=headers) as resp:
            await raise_for_error(resp)
Exemplo n.º 6
0
    async def change_file_mime(self, bucket: str, key: str, mime: str) -> None:
        """修改文件的MIME类型信息

        :param bucket: 待操作资源所在空间名
        :param key: 待操作资源文件名
        :param mime: 待操作文件目标MIME类型信息

        详见:https://developer.qiniu.com/kodo/api/1252/chgm
        """
        encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        encoded_mime = urlsafe_b64encode(mime.encode()).decode()
        path = "/chgm/{}/mime/{}".format(encoded_entry_uri, encoded_mime)
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "http://rs.qiniu.com" + path

        async with self.httpclient.post(url, headers=headers) as resp:
            await raise_for_error(resp)
Exemplo n.º 7
0
    async def prefetch(self, bucket: str, key: str) -> None:
        """镜像回源预取

        对于设置了镜像存储的空间,从镜像源站抓取指定资源并存储到该空间中。
        如果该空间中已存在同名资源,则会将镜像源站的资源覆盖空间中的同名资源。

        :param bucket: 待获取资源的镜像空间名
        :param key: 待获取资源文件名

        详见:https://developer.qiniu.com/kodo/api/1293/prefetch
        """
        encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        path = "/prefetch/{}".format(encoded_entry_uri)
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "https://iovip.qbox.me" + path

        async with self.httpclient.post(url, headers=headers) as resp:
            await raise_for_error(resp)
Exemplo n.º 8
0
    async def get_file_stat(self, bucket: str, key: str) -> dict:
        """查询文件信息

        :param bucket: 待查询文件空间名
        :param key: 待查询文件名

        :return: 文件信息,包含hash,key,fsize和mimeType

        详见:https://developer.qiniu.com/kodo/api/1308/stat
        """
        encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        path = "/stat/{}".format(encoded_entry_uri)
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "http://rs.qiniu.com" + path

        async with self._httpclient.get(url, headers=headers) as resp:
            assert resp.status == 200, "HTTP {}".format(resp.status)
            stat = await resp.json()

        return stat
Exemplo n.º 9
0
    async def fetch(self, url: str, bucket: str, key=None) -> dict:
        """七牛云第三方资源抓取

        :param url: 要抓取的URL
        :param bucket: 目标资源空间名
        :param key: 目标资源文件名,默认为空

        :return: 爬取成功后的文件信息,包含hash,key,fsize和mimeType

        详见:https://developer.qiniu.com/kodo/api/1263/fetch
        """
        encoded_url = urlsafe_b64encode(url.encode()).decode()
        encoded_entry_uri = get_encoded_entry_uri(bucket, key)
        path = "/fetch/{}/to/{}".format(encoded_url, encoded_entry_uri)
        access_token = self.get_access_token(path)
        headers = {"Authorization": "QBox {}".format(access_token)}
        url = "https://iovip.qbox.me" + path

        async with self._httpclient.post(url, headers=headers) as resp:
            assert resp.status == 200, "HTTP {}".format(resp.status)
            ret = await resp.json()

        return ret
Exemplo n.º 10
0
def test_get_encoded_entry_uri():
    for i in range(20):
        bucket = ''.join(choice(ascii_letters) for i in range(10))
        key = ''.join(choice(ascii_letters) for i in range(10))
        assert aqutils.get_encoded_entry_uri(bucket,
                                             key) == qutils.entry(bucket, key)