def upload(main_service_name, mode, service_name, username, password, f, path_name, domain, bot: Bot, chat_id, message_id): try: up = UpYun(service_name, username, password) up.put(path_name, f) cdn_url = "%s/%s" % (domain, path_name) bot.send_message(chat_id=chat_id, text=upload_success(main_service_name, mode, cdn_url), reply_to_message_id=message_id, parse_mode=ParseMode.HTML) except UpYunServiceException as se: error_detail = """ HTTP Status Code: %s Error Message: %s """ % (se.status, se.msg) bot.send_message(chat_id=chat_id, text=upload_error(service_name, error_detail), reply_to_message_id=message_id, parse_mode=ParseMode.HTML) except UpYunClientException as ce: error_detail = """ Error Message: %s """ % ce.msg bot.send_message(chat_id=chat_id, text=upload_error(service_name, error_detail), reply_to_message_id=message_id, parse_mode=ParseMode.HTML)
def app(environ, start_response): path = environ['PATH_INFO'] data = '' if path == "/list.html": query = environ['QUERY_STRING'] m = re.match(r'^dir=/([^/]*)(.*)', query) bucket, fpath = m.group(1), m.group(2) up = UpYun(bucket = bucket, username = config.username, password = config.password ) info = up.getinfo(key = fpath) if info['file-type'] == 'folder': status = '200 OK' data = listdir(up, bucket, fpath) start_response(status, [ ("Content-Type", "text/html"), ("Content-Length", str(len(data))) ]) else: status = '302 Temporarily Moved' start_response(status, [ ("Content-Type", "text/plain"), ("Content-Length", str(len(data))) ( "Location", "http://%s.b0.upaiyun.com%s" % (bucket, fpath) ), ]) elif path == "/upload.html": m = re.match(r'uri=(.*)&dir=/([^/]*)(.*)', environ['QUERY_STRING']) uri, bucket, fpath = m.group(1), m.group(2), m.group(3) blob = '' resp = requests.get(uri, stream=True, timeout=5) for chunk in resp.iter_content(8192): if not chunk: break blob += chunk if blob != '': up = UpYun(bucket = bucket, username = config.username, password = config.password ) up.put(fpath, blob) data = '%s Upload to /%s%s .. OK' % (uri, bucket, fpath) else: status = '200 OK' data += '<h1>usage</h1>\n' data += '<p>/list.html?dir=</bucket/path/to/file></p>\n' data += '<p>/post.html?uri=<uri>&&dir=</bucket/path/to/file></p>\n' start_response(status, [ ("Content-Type", "text/html"), ("Content-Length", str(len(data))) ]) return iter([data])
class UpYunStore(object): APP_NAME = None USERNAME = None PASSWORD = None TMP_PATH = None def __init__(self, uri): assert uri.startswith('http://') self.upyun = UpYun(self.APP_NAME, self.USERNAME, self.PASSWORD) self.prefix = '/' + uri.split('/')[-1] def stat_image(self, key, info): image_info = self.upyun.getinfo(self.prefix + '/' + key) last_modified = int(image_info['date']) checksum = image_info['size'] return {'last_modified': last_modified, 'checksum': checksum} def persist_image(self, key, image, buf, info): tmp_path = os.path.join(self.TMP_PATH, 'tmp.jpg') image.save(tmp_path) data = open(tmp_path, 'rb') result = self.upyun.put(self.prefix + '/' + key, data, True) if not result: log.info("Image: Upload image to Upyun Failed! %s" % (self.prefix + key))
def upimg_save(**kwargs): res = dict(code=1) try: filename = kwargs["filename"] stream = kwargs["stream"] upload_path = kwargs.get("upload_path") or "" if not filename or not stream: return ValueError except (KeyError, ValueError): res.update(msg="Parameter error") else: try: from upyun import UpYun, ED_AUTO except ImportError: res.update(msg="Please install upyun module") return res dn = g.cfg.upyun_dn bucket = g.cfg.upyun_bucket user = g.cfg.upyun_username passwd = g.cfg.upyun_password upyun_basedir = g.cfg.upyun_basedir or '/' if not dn or not bucket or not user or not passwd: res.update(msg="The upyun parameter error") return res if isinstance(upload_path, string_types): if upload_path.startswith("/"): upload_path = upload_path.lstrip('/') if not upyun_basedir.startswith("/"): upyun_basedir = "/%s" % upyun_basedir saveto = join(upyun_basedir, upload_path) filepath = join(saveto, filename) up = UpYun(bucket, user, passwd, timeout=5, endpoint=ED_AUTO) res.update(up.put(filepath, stream)) res.update( code=0, src=slash_join(dn, filepath), basedir=upyun_basedir, ) else: res.update(msg="The upload_path type error") return res
def upload_drawing_bed(request, img_name, files, bed_default_types): if bed_default_types == 'qiniu': qiniu_settings = DrawingBedSetting.objects.filter( types=bed_default_types, create_user=request.user) if qiniu_settings.count() == 8: qiniu_access_key, qiniu_secret_key, qiniu_storage_space_name, qiniu_visit_website, qiniu_storage_area, \ qiniu_url_suffix, qiniu_storage_path, qiniu_default_types = get_drawing_beds(request, 'qiniu') access_key = qiniu_access_key.value secret_key = qiniu_secret_key.value # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = qiniu_storage_space_name.value # 上传后保存的文件名 key = qiniu_storage_path.value + img_name # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 ret, info = put_data(token, key, files) if ret and info: file_url = qiniu_visit_website.value + '/' + ret.get( 'key') + qiniu_url_suffix.value return file_url else: return False if bed_default_types == 'upyun': upyun_access_key, upyun_secret_key, upyun_storage_space_name, upyun_visit_website, upyun_url_suffix, \ upyun_storage_path, upyun_default_types = get_drawing_beds(request, bed_default_types) up = UpYun(upyun_storage_space_name.value, username=upyun_access_key.value, password=upyun_secret_key.value) # 上传后保存的文件名 key = upyun_storage_path.value + img_name # headers = {'x-gmkerl-rotate': '180'} res = up.put(key=key, value=files, checksum=False) if res.get('file-type'): file_url = upyun_visit_website.value + '/' + key + upyun_url_suffix.value return file_url else: return False
class UpYunTestCase(unittest.TestCase): #: File type bucket BUCKET_FILE = '' #: Image type bucket BUCKET_IMAGE = '' #: Username USERNAME = '' #: Password PASSWD = '' #: Predefined thumbnail version THUMB_VERSION = '' ASSET = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'asset') LOCAL_PATH_TXT_FILE = os.path.join(ASSET, 'upyun-test.txt') LOCAL_PATH_IMG_FILE = os.path.join(ASSET, 'upyun-test.gif') REMOTE_DIR = '/tmp/upyun-test' REMOTE_PATH_TXT_FILE = pathname2url( os.path.join(REMOTE_DIR, 'upyun-test.txt')) REMOTE_PATH_IMG_FILE = pathname2url( os.path.join(REMOTE_DIR, 'upyun-test.gif')) def setUp(self): self.client_file = UpYun(self.BUCKET_FILE, (self.USERNAME, self.PASSWD), const.SPACE_TYPE_FILE) self.client_image = UpYun(self.BUCKET_IMAGE, (self.USERNAME, self.PASSWD), const.SPACE_TYPE_IMAGE) self.test_file_txt = open(self.LOCAL_PATH_TXT_FILE) self.test_file_img = open(self.LOCAL_PATH_IMG_FILE, 'rb') def tearDown(self): self.test_file_txt.close() self.test_file_img.close() self._delete(self.REMOTE_PATH_TXT_FILE, self.client_file) self._delete(self.REMOTE_PATH_IMG_FILE, self.client_file) self._delete(self.REMOTE_PATH_IMG_FILE, self.client_image) self._delete(self.REMOTE_DIR, self.client_image) self._delete(self.REMOTE_DIR, self.client_file) def _put_file(self): return self.client_file.put( self.REMOTE_PATH_TXT_FILE, self.test_file_txt) def _put_image(self, client=None): client = client or self.client_image return client.put(self.REMOTE_PATH_IMG_FILE, self.test_file_img) def _mkdir(self, mk_parent=True, client=None): client = client or self.client_file return client.mkdir(self.REMOTE_DIR, mk_parent) def _delete(self, path, client): return client.delete(path) def test_put_file_space_file(self): resp = self._put_file() assert resp.success, resp.error def test_put_file_space_image(self): resp = self._put_image(self.client_file) assert resp.success, resp.error def test_put_image_space_image(self): resp = self._put_image(self.client_image) assert resp.success, resp.error assert isinstance(resp.frames, int) assert isinstance(resp.height, int) assert resp.height > 0 assert isinstance(resp.width, int) assert resp.width > 0 def test_put_thumbnail_version(self): resp = self.client_image.put_thumbnail(self.REMOTE_PATH_IMG_FILE, self.test_file_img, self.THUMB_VERSION) assert resp.success, resp.error def test_put_thumbnail_version_modified(self): resp = self.client_image.put_thumbnail(self.REMOTE_PATH_IMG_FILE, self.test_file_img, self.THUMB_VERSION, const.THUMB_TYPE_FIX_MAX, (10,), 100, True) assert resp.success, resp.error def test_put_thumbnail_custom(self): resp = self.client_image.put_thumbnail(self.REMOTE_PATH_IMG_FILE, self.test_file_img, ttype=const.THUMB_TYPE_FIX_MAX, res=(10,), quality=100, sharpen=True) assert resp.success, resp.error def test_get_text_file(self): self._put_file() resp = self.client_file.get(self.REMOTE_PATH_TXT_FILE) assert resp.success, resp.error self.test_file_txt.seek(0) assert resp.data == self.test_file_txt.read() def test_get_binary_file(self): client = self.client_image self._put_image(client) resp = client.get(self.REMOTE_PATH_IMG_FILE) assert resp.success, resp.error self.test_file_img.seek(0) assert resp.data == self.test_file_img.read() def test_ls(self): client = self.client_file self._put_file() self._put_image(client) resp = client.ls(self.REMOTE_DIR) assert resp.success, resp.error remote_file_paths = map(lambda f: f.path, resp.files.itervalues()) assert self.REMOTE_PATH_TXT_FILE in remote_file_paths assert self.REMOTE_PATH_IMG_FILE in remote_file_paths def test_mkdir(self): resp = self._mkdir() assert resp.success, resp.error def test_usage(self): resp = self.client_file.usage() assert resp.success, resp.error assert isinstance(resp.usage, int) resp = self.client_image.usage() assert resp.success, resp.error assert isinstance(resp.usage, int) def test_info(self): def _assert(resp): assert resp.success, resp.error assert isinstance(resp.size, int) assert isinstance(resp.date, datetime.datetime) assert resp.type == const.FILE_TYPE_FILE self._put_file() resp = self.client_file.info(self.REMOTE_PATH_TXT_FILE) _assert(resp) self._put_image() resp = self.client_image.info(self.REMOTE_PATH_IMG_FILE) _assert(resp) def test_delete(self): self._put_file() resp = self.client_file.delete(self.REMOTE_PATH_TXT_FILE) assert resp.success, resp.error self._put_image() resp = self.client_image.delete(self.REMOTE_PATH_IMG_FILE) assert resp.success, resp.error