def get_config(self, path): config = Config() try: config.load_config_from_filepath(path) except: print('[ERROR] failed to load config info from %s' % path) sys.exit(-1) return config
def setUp(self): self.test_config = Config('ACCESS_KEY_ID', 'SECRET_ACCESS_KEY') self.test_op = { 'API': 'Test', 'Method': 'GET', 'URI': '/<bucket-name>/<object-key>', 'Headers': { 'Host': 'pek3a.qingstor.com', 'Date': 'Wed, 10 Dec 2014 17:20:31 GMT', 'x-qs-test-header1': 'test', 'x-qs-test-header2': '中文测试', 'test_empty_header': '', }, 'Params': { 'test_params_1': 'test_val', 'test_params_2': '中文测试', 'test_params_empty': '', }, 'Elements': { 'test_elements_1': 'test_val', 'test_elements_2': '中文测试', 'test_elements_empty': '', }, 'Properties': { 'zone': 'pek3a', 'bucket-name': 'test_bucket', 'object-key': '中文测试.json', }, 'Body': None, } self.test_req = Request(self.test_config, self.test_op)
def setUpClass(cls): cls.test_config = Config('ACCESS_KEY_ID', 'SECRET_ACCESS_KEY') cls.test_operation = { 'API': 'Test', 'Method': 'GET', 'URI': '/<bucket-name>/<object-key>', 'Headers': { 'Host': 'pek3a.qingstor.com', 'Date': 'Wed, 10 Dec 2014 17:20:31 GMT', 'x-qs-test-header1': 'test_val', 'x-qs-copy-source': '中文测试', 'x-qs-fetch-source': 'https://google.com/logo.png', 'test_empty_header': '', }, 'Params': { 'test_params_1': 'test_val', 'test_params_2': '中文测试', 'test_params_empty': '', }, 'Elements': { 'test_elements_1': 'test_val', 'test_elements_2': '中文测试', 'test_elements_empty': '', }, 'Properties': { 'zone': 'pek3a', 'bucket-name': 'test_bucket', 'object-key': '中文测试.json', }, 'Body': None, } cls.test_builder = Builder(cls.test_config, cls.test_operation)
def open_spider(self, spider): self.config= Config(self.qs_asscess_key,self.qs_secret_key) self.qingstor = QingStor(self.config) self.bucket = self.qingstor.Bucket(self.qs_bucket_name,self.qs_zone) self.session = requests.Session()
def test_init_without_key(self): test_config = Config() self.assertEqual(test_config.access_key_id, '') self.assertEqual(test_config.secret_access_key, '') self.assertEqual(test_config.host, 'qingstor.com') self.assertEqual(test_config.port, 443) self.assertEqual(test_config.protocol, 'https') self.assertEqual(test_config.connection_retries, 3) self.assertEqual(test_config.log_level, 'warn')
def __init__(self, access_key_id=QINGSTOR_ACCESS_KEY_ID, secret_access_key=QINGSTOR_SECRET_ACCESS_KEY, bucket_name=QINGSTOR_BUCKET_NAME, bucket_zone=QINGSTOR_BUCKET_ZONE, secure_url=QINGSTOR_SECURE_URL): self.config = Config(access_key_id=access_key_id, secret_access_key=secret_access_key) self.bucket_name = bucket_name self.zone = bucket_zone self.bucket = self._init_bucket(bucket_name, bucket_zone) self.secure_url = secure_url
def add_common_arguments(self, parser, args): # FIX: get default config file path config = Config() default_conf_path = config.get_user_config_file_path() parser.add_argument('-f', '--config', dest='conf_file', default=default_conf_path, help='Config file location') # get zone info. from config file options = parser.parse_known_args(args) # index 0 is known arg for -f or --config option options = options[0] conf = self.get_config(options.conf_file) parser.add_argument( '-z', '--zone', dest='zone', default=conf.zone, help='On which zone', )
def __init__(self, access_key_id=None, secret_access_key=None, zone=None, bucket_name=None): self.access_key_id = access_key_id if access_key_id else _get_config( 'QINGSTOR_ACCESS_KEY_ID') self.secret_access_key = secret_access_key if secret_access_key else _get_config( 'QINGSTOR_SECRET_ACCESS_KEY') self.zone = zone if zone else _get_config('QINGSTOR_ZONE') self.bucket_name = bucket_name if bucket_name else _get_config( 'QINGSTOR_BUCKET') # get bucket config = Config(self.access_key_id, self.secret_access_key) qingstor = QingStor(config) self.bucket = qingstor.Bucket(bucket_name=self.bucket_name, zone=self.zone)
def test_init_with_data(self): config_data = { 'access_key_id': 'ACCESS_KEY_ID_1', 'secret_access_key': 'SECRET_ACCESS_KEY_1', 'host': 'private.com', 'port': 80, 'protocol': 'http', 'connection_retries': 1, 'log_level': 'info' } test_config = Config().load_config_from_data(config_data) self.assertEqual(test_config.access_key_id, 'ACCESS_KEY_ID_1') self.assertEqual(test_config.secret_access_key, 'SECRET_ACCESS_KEY_1') self.assertEqual(test_config.host, 'private.com') self.assertEqual(test_config.port, 80) self.assertEqual(test_config.protocol, 'http') self.assertEqual(test_config.connection_retries, 1) self.assertEqual(test_config.log_level, 'info')
def get_connection(self, conf): try: key_id = conf.qy_access_key_id except AttributeError: print('[ERROR] cannot find key id info in config file') sys.exit(-1) try: secret_key = conf.qy_secret_access_key except AttributeError: print('[ERROR] cannot find secret key info in config file') sys.exit(-1) try: zone = conf.zone except AttributeError: print('[ERROR] cannot find zone info in config file') sys.exit(-1) config = Config(key_id, secret_key) service = QingStor(config) return service
from qingstor.sdk.service.qingstor import QingStor from qingstor.sdk.config import Config ACCESS_KEY_ID = 'KPXLUFSRVNVNZGFCEPDT' SECRET_ACCESS_KEY = '9RW7JW2RsIDmArXSdeHhCjYt7A9vHPs6LBT8zSEp' config = Config(ACCESS_KEY_ID, SECRET_ACCESS_KEY) qingstor = QingStor(config) # List all buckets. output = qingstor.list_buckets() # Print HTTP status code. print(output.status_code) # Print the buckets. print(output['buckets']) bucket = qingstor.Bucket('mrc-lxm', 'pek3b') fname = 'main1.jpg' with open('trendsetter/img/{}'.format(fname), 'rb') as f: output = bucket.put_object(fname, body=f) # Print the HTTP status code. # Example: 201 print(output.status_code)
# -*- coding: utf-8 -*- import json from os import path import yaml from assertpy import assert_that from behave import * from qingstor.sdk.config import Config from qingstor.sdk.service.qingstor import QingStor test_config_file_path = path.abspath( path.join(path.dirname(__file__), path.pardir)) config = Config().load_config_from_filepath(test_config_file_path + "/config.yaml") qingstor = QingStor(config) with open(test_config_file_path + '/test_config.yaml') as f: test = yaml.load(f) f.close() bucket = qingstor.Bucket(test['bucket_name'], test['zone']) bucket.put() @when(u'put bucket ACL') def step_impl(context): context.res = bucket.put_acl(json.loads(context.text)['acl']) @then(u'put bucket ACL status code is 200') def step_impl(context):
# -*- coding: utf-8 -*- from __future__ import unicode_literals import os import sh import yaml from behave import * from assertpy import assert_that from qingstor.sdk.config import Config from qingstor.sdk.service.qingstor import QingStor config = Config().load_user_config() qingstor = QingStor(config) test_config_file_path = os.path.abspath( os.path.join(os.path.dirname(__file__), os.path.pardir)) with open(test_config_file_path + '/test_config.yaml') as f: test_data = yaml.load(f) f.close() bucket = qingstor.Bucket(test_data['bucket_name'], test_data['zone']) bucket.put() qsctl = sh.Command("qsctl") @given(u'an empty QingStor bucket') def step_impl(context): context.empty_bucket = qingstor.Bucket(test_data['bucket_name'] + "1", test_data['zone']) context.empty_bucket.put()
def add_assets(username, type, music_name): # 配置青云服务器 config = Config(qy_access_key, qy_secret_access_key) service = QingStor(config) buckets = service.list_buckets('pek3a')['buckets'] print(buckets) # 获取业务用 bucket [bucket_info] = filter(lambda bucket: bucket['name'] == 'nest-habit', buckets) bucket = service.Bucket('nest-habit', 'pek3a') # 随机一个文件名 if type == 'uploaded_musics': base = 'music' elif type == 'avatar': base = 'image' else: return json.dumps({'error': 'Invalid operation!'}), 400, regular_req_headers filename = mimetypes.guess_extension(request.headers.get('x-mime-type')) filename = '%s/%s%s' % (base, str(random.randint(1, 1E15)), filename) # 上传这个文件 result = bucket.put_object(filename, body=request.data) print(result) # 组装 URL url = bucket_info['url'] + '/' + filename if type == 'uploaded_musics': # 更新数据库 inserted_id = db['_musics'].insert_one({ 'url': url, 'music_name': music_name, 'created_time': datetime.datetime.utcnow(), 'creator': username }).inserted_id result = db['_users'].find_one_and_update( {'username': username}, {'$push': { 'uploaded_musics': inserted_id }}, return_document=ReturnDocument.AFTER) elif type == 'avatar': result = db['_users'].find_one_and_update( {'username': username}, {'$set': { 'avatar': url }}, return_document=ReturnDocument.AFTER) else: return json.dumps({'error': 'Invalid operation!'}), 400, regular_req_headers if result == None: bucket.delete_object(filename) return _no_user_named_xxx, 400, regular_req_headers # 生成响应 if type == 'uploaded_musics': result = {'_id': inserted_id, 'music_name': music_name, 'url': url} return json.dumps(result, default=oid_handler), 200, regular_req_headers keys = list( filter(lambda key: key not in ['_id', 'password', 'created_time'], result.keys())) values = list(map(lambda key: result[key], keys)) return json.dumps(dict(zip(keys, values)), default=oid_handler), 200, regular_req_headers
def get_client(cls, conf): config = Config().load_config_from_data(conf) return QingStor(config)
# +------------------------------------------------------------------------- import os from flask import Flask, request from qingstor.sdk.config import Config from qingstor.sdk.request import Request app = Flask(__name__) # Get access_key_id and secret_access_key from system environment access_key_id = os.environ.get('ACCESS_KEY_ID') secret_access_key = os.environ.get('SECRET_ACCESS_KEY') # Init a new config object with keyid config = Config(access_key_id, secret_access_key) def get_properties(data): """ Get properties from data :param data: the request data to be authorized :return: properties: request properties """ properties = {} url = data['url'].split('/') if len(url) == 2 and url[1]: properties['bucket-name'] = url[1] if len(url) > 2 and url[2]: properties['object-key'] = url[2].split('?')[0] return properties
# -*- coding:utf-8 -*- from qingstor.sdk.service.qingstor import QingStor from qingstor.sdk.config import Config import requests import tempfile # 初始化QingStor对象 config = Config('EBZAULNLJTMJJCZJWCRG', '02MLUQZA5wZPwTrOmVWUajPhNUSFI8c5VKu2Wf0u') qingstor = QingStor(config) # 获取bucket列表 # output = qingstor.list_buckets() # print(output.status_code) # print(output['buckets']) # 获取指定bucket对象 bucket = qingstor.Bucket('fifa-helper-bucket', 'sh1a') def getFileByKey(key): resp = bucket.get_object(key) with tempfile.NamedTemporaryFile() as f: for chunk in resp.iter_content(): f.write(chunk) return f def getRespByKey(key): resp = bucket.get_object(key)