def _two_key_batch(cls, operation, source_bucket, key_pairs, target_bucket, force='false'): if target_bucket is None: target_bucket = source_bucket return [ cls._build_op(operation, entry(source_bucket, k), entry(target_bucket, v), 'force/{0}'.format(force)) for k, v in key_pairs.items() ]
async def copy(self, key, bucket_to, key_to, force='false'): """复制文件: 将指定资源复制为新命名资源,具体规格参考: http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html Args: bucket: 待操作资源所在空间 bucket_to: 目标资源空间名 key: 待操作资源文件名 key_to: 目标资源文件名 Returns: 一个dict变量,成功返回NULL,失败返回{"error": "<errMsg string>"} 一个ResponseInfo对象 """ resource = entry(self._bucket, key) to = entry(bucket_to, key_to) return await self._rs_do('copy', resource, to, 'force/{0}'.format(force))
async def move(self, key, bucket_to=None, key_to=None, force='false'): """移动文件: 将资源从一个空间到另一个空间,具体规格参考: http://developer.qiniu.com/docs/v6/api/reference/rs/move.html Args: bucket: 待操作资源所在空间 bucket_to: 目标资源空间名 key: 待操作资源文件名 key_to: 目标资源文件名 Returns: 一个dict变量,成功返回NULL,失败返回{"error": "<errMsg string>"} 一个ResponseInfo对象 """ resource = entry(self._bucket, key) to = entry(bucket_to if bucket_to else self._bucket, key_to if key_to else key) return await self._rs_do('move', resource, to, 'force/{0}'.format(force))
async def change_type(self, key, storage_type): """修改文件的存储类型 修改文件的存储类型为普通存储或者是低频存储,参考文档: https://developer.qiniu.com/kodo/api/3710/modify-the-file-type Args: bucket: 待操作资源所在空间 key: 待操作资源文件名 storage_type: 待操作资源存储类型,0为普通存储,1为低频存储,2 为归档存储 """ resource = entry(self._bucket, key) return await self._rs_do('chtype', resource, 'type/{0}'.format(storage_type))
async def restoreAr(self, key, freezeAfter_days): """解冻归档存储文件 修改文件的存储类型为普通存储或者是低频存储,参考文档: https://developer.qiniu.com/kodo/api/6380/restore-archive Args: bucket: 待操作资源所在空间 key: 待操作资源文件名 freezeAfter_days: 解冻有效时长,取值范围 1~7 """ resource = entry(self._bucket, key) return await self._rs_do( 'restoreAr', resource, 'freezeAfterDays/{0}'.format(freezeAfter_days))
async def change_mime(self, key, mime): """修改文件mimeType: 主动修改指定资源的文件类型,具体规格参考: http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html Args: bucket: 待操作资源所在空间 key: 待操作资源文件名 mime: 待操作文件目标mimeType """ resource = entry(self._bucket, key) encode_mime = urlsafe_base64_encode(mime) return await self._rs_do('chgm', resource, 'mime/{0}'.format(encode_mime))
async def delete(self, key): """删除文件: 删除指定资源,具体规格参考: http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html Args: bucket: 待获取信息资源所在的空间 key: 待获取资源的文件名 Returns: 一个dict变量,成功返回NULL,失败返回{"error": "<errMsg string>"} 一个ResponseInfo对象 """ resource = entry(self._bucket, key) return await self._rs_do('delete', resource)
async def prefetch(self, key, hostscache_dir=None): """镜像回源预取文件: 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源,具体规格参考 http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html Args: bucket: 待获取资源所在的空间 key: 代获取资源文件名 hostscache_dir: host请求 缓存文件保存位置 Returns: 一个dict变量,成功返回NULL,失败返回{"error": "<errMsg string>"} 一个ResponseInfo对象 """ resource = entry(self._bucket, key) return await self._io_do(self._bucket, 'prefetch', hostscache_dir, resource)
async def change_status(self, key, status, cond): """修改文件的状态 修改文件的存储类型为可用或禁用: Args: bucket: 待操作资源所在空间 key: 待操作资源文件名 storage_type: 待操作资源存储类型,0为启用,1为禁用 """ resource = entry(self._bucket, key) if cond and isinstance(cond, dict): condstr = "" for k, v in cond.items(): condstr += "{0}={1}&".format(k, v) condstr = urlsafe_base64_encode(condstr[:-1]) return await self._rs_do('chstatus', resource, 'status/{0}'.format(status), 'cond', condstr) return await self._rs_do('chstatus', resource, 'status/{0}'.format(status))
async def fetch(self, url, key=None, hostscache_dir=None): """抓取文件: 从指定URL抓取资源,并将该资源存储到指定空间中,具体规格参考: http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html Args: url: 指定的URL bucket: 目标资源空间 key: 目标资源文件名 hostscache_dir: host请求 缓存文件保存位置 Returns: 一个dict变量: 成功 返回{'fsize': <fsize int>, 'hash': <hash string>, 'key': <key string>, 'mimeType': <mimeType string>} 失败 返回 None 一个ResponseInfo对象 """ resource = urlsafe_base64_encode(url) to = entry(self._bucket, key) return await self._io_do(self._bucket, 'fetch', hostscache_dir, resource, 'to/{0}'.format(to))
async def delete_after_days(self, key, days): """更新文件生命周期 Returns: 一个dict变量,返回结果类似: [ { "code": <HttpCode int>, "data": <Data> }, { "code": <HttpCode int> }, { "code": <HttpCode int> }, { "code": <HttpCode int> }, { "code": <HttpCode int>, "data": { "error": "<ErrorMessage string>" } }, ... ] 一个ResponseInfo对象 Args: key: 目标资源文件名 days: 指定天数 """ if isinstance(days, int): days = str(days) resource = entry(self._bucket, key) return await self._rs_do('deleteAfterDays', resource, days)
async def stat(self, key): """获取文件信息: 获取资源的元信息,但不返回文件内容,具体规格参考: https://developer.qiniu.com/kodo/api/1308/stat Args: bucket: 待获取信息资源所在的空间 key: 待获取资源的文件名 Returns: 一个dict变量,类似: { "fsize": 5122935, "hash": "ljfockr0lOil_bZfyaI2ZY78HWoH", "mimeType": "application/octet-stream", "putTime": 13603956734587420 "type": 0 } 一个ResponseInfo对象 """ resource = entry(self._bucket, key) return await self._rs_do('stat', resource)
def _three_key_batch(cls, operation, bucket, keys): return [ cls._build_op(operation, entry(bucket, k), 'freezeAfterDays/{0}'.format(v)) for k, v in keys.items() ]
def _one_key_batch(cls, operation, bucket, keys): return [cls._build_op(operation, entry(bucket, key)) for key in keys]
def op_save(op, bucket, key): return pipe_cmd(op, 'saveas/' + entry(bucket, key))