def file_url(self, host): url = ['{0}/mkfile/{1}'.format(host, self.size)] if self.mime_type: url.append('mimeType/{0}'.format( urlsafe_base64_encode(self.mime_type))) if self.key is not None: url.append('key/{0}'.format(urlsafe_base64_encode(self.key))) if self.file_name is not None: url.append('fname/{0}'.format(urlsafe_base64_encode( self.file_name))) if self.params: for k, v in self.params.items(): url.append('{0}/{1}'.format(k, urlsafe_base64_encode(v))) pass if self.modify_time and self.keep_last_modified: url.append("x-qn-meta-!Last-Modified/{0}".format( urlsafe_base64_encode(rfc_from_timestamp(self.modify_time)))) url = '/'.join(url) # print url return url
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 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))
def _token(self, data): data = b(data) hashed = hmac.new(self._secret_key, data, sha1) return urlsafe_base64_encode(hashed.digest())