def ossuse(var): auth = oss2.Auth('LTAIT5MicSkqQBPx', '1fWKx1z1q8LoXanGXmpT7Vn78Zddx4') # 开启日志 log_file_path = "log.log" oss2.set_file_logger(log_file_path, 'oss2', logging.INFO) bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'ubzy') upurl = r'/opt/xyfxreport/pic/%s' % var key = 'app/exam/exampic/%s' % var bucket.put_object_from_file(key, upurl)
def __init__(self, kms_access_key_secret=None): oss2.set_file_logger(config.LogFile, 'oss2', config.LogLevel) if not kms_access_key_secret: kms_access_key_secret = str( getpass("请输入AK为\"%s\"的KMS服务的SK:" % color.red(config.KMSAccessKeyId))) self.__OssEndpoint = 'https://' + config.OssEndpoint self.__bucket = oss2.CryptoBucket( oss2.Auth(config.OSSAccessKeyId, config.OSSAccessKeySecret), self.__OssEndpoint, config.bucket_name, crypto_provider=oss2.crypto.AliKMSProvider(config.KMSAccessKeyId, kms_access_key_secret, config.KMSRegion, config.CMKID)) try: # 检测Bucket是否存在 self.__bucket.get_bucket_info() except oss2.exceptions.NoSuchBucket: logger.critical("Bucket:\"%s\"不存在" % config.bucket_name) raise ValueError("Bucket:\"%s\"不存在" % config.bucket_name) try: # 检测KMS配置有效性 KmsClient( OpenApiModels.Config( access_key_id=config.KMSAccessKeyId, access_key_secret=kms_access_key_secret, endpoint='kms.%s.aliyuncs.com' % config.KMSRegion)).generate_data_key( KmsModels.GenerateDataKeyRequest(key_id=config.CMKID)) except: logger.critical("无法调用KMS服务生成密钥,请检查相关配置,以及SK是否输入正确") raise ValueError("无法调用KMS服务生成密钥,请检查相关配置,以及SK是否输入正确") del kms_access_key_secret self.__ping_cmd = ["ping", "1", config.OssEndpoint] if os.name == 'nt': self.__ping_cmd.insert(1, "-n") elif os.name == 'posix': self.__ping_cmd.insert(1, "-c") else: raise OSError("无法识别操作系统") if subprocess.run(self.__ping_cmd, capture_output=True).returncode != 0: logger.error("无法连接至%s,请检查OssEndpoint和网络配置" % config.OssEndpoint) raise ValueError("无法连接至%s,请检查OssEndpoint和网络配置" % config.OssEndpoint) self.__restore_configuration_model = [ oss2.models.RESTORE_TIER_EXPEDITED, oss2.models.RESTORE_TIER_STANDARD, oss2.models.RESTORE_TIER_BULK ]
def __init__(self): """ 初始化云文件系统 需要初始化COS客户端,以及指定存储痛 """ oss2.set_stream_logger(level=logging.WARNING) oss2.set_file_logger(file_path='cloudsync.log', level=logging.WARNING) import cos_config self._access_key_id = cos_config.ali['access_key_id'] self._access_key_secret = cos_config.ali['access_key_secret'] self._endpoint = cos_config.ali['endpoint'] self._bucket_name = cos_config.ali['bucket_name'] self._auth = oss2.Auth(self._access_key_id, self._access_key_secret) self._client = oss2.Bucket(self._auth, self._endpoint, self._bucket_name) self.path_config = { 'history_path': cos_config.ali['history_path'], 'local_path': cos_config.ali['local_path'], 'cloud_path': cos_config.ali['cloud_path'] }
from .base import AbstractObjStore from seafobj.exceptions import GetObjectError import httplib import oss2 # set log level to WARNING # the api set_file_logger exists after oss2 2.6.0, which has a lot of 'INFO' log try: log_file_path = "log.log" oss2.set_file_logger(log_file_path, 'oss2', logging.WARNING) except: pass class OSSConf(object): def __init__(self, key_id, key, bucket_name, host): self.key_id = key_id self.key = key self.bucket_name = bucket_name self.host = host class SeafOSSClient(object): '''Wraps a oss connection and a bucket''' def __init__(self, conf): self.conf = conf # Due to a bug in httplib we can't use https self.auth = oss2.Auth(conf.key_id, conf.key) self.service = oss2.Service(self.auth, conf.host) self.bucket = oss2.Bucket(self.auth, conf.host, conf.bucket_name) def read_object_content(self, obj_id):
from .base import AbstractObjStore from seafobj.exceptions import GetObjectError import httplib import oss2 # set log level to WARNING # the api set_file_logger exists after oss2 2.6.0, which has a lot of 'INFO' log try: log_file_path = "log.log" oss2.set_file_logger(log_file_path, 'oss2', logging.WARNING) except: pass class OSSConf(object): def __init__(self, key_id, key, bucket_name, host): self.key_id = key_id self.key = key self.bucket_name = bucket_name self.host = host class SeafOSSClient(object): '''Wraps a oss connection and a bucket''' def __init__(self, conf): self.conf = conf # Due to a bug in httplib we can't use https self.auth = oss2.Auth(conf.key_id, conf.key) self.service = oss2.Service(self.auth, conf.host) self.bucket = oss2.Bucket(self.auth, conf.host, conf.bucket_name)
interact(False) else: print('incorrect command,please input again.') if temp_show_help_info: interact(True) else: interact(False) if __name__ == '__main__': init() log_file_path = "oss2_sync.log" # 设置日志等级 print("log_level: " + log_level) if log_level == 'NOTSET': oss2.set_file_logger(log_file_path, 'oss2', logging.NOTSET) elif log_level == 'DEBUG': oss2.set_file_logger(log_file_path, 'oss2', logging.DEBUG) elif log_level == 'INFO': oss2.set_file_logger(log_file_path, 'oss2', logging.INFO) elif log_level == 'WARNING': oss2.set_file_logger(log_file_path, 'oss2', logging.WARNING) elif log_level == 'ERROR': oss2.set_file_logger(log_file_path, 'oss2', logging.ERROR) elif log_level == 'CRITICAL': oss2.set_file_logger(log_file_path, 'oss2', logging.ERROR) else: #默认INFO等级 oss2.set_file_logger(log_file_path, 'oss2', logging.INFO) interact(show_help_info)
try: result = self.bucket.put_object(obj_name, ustr) logging.info('http status: {0}'.format(result.status)) logging.info('request_id: {0}'.format(result.request_id)) logging.info('ETag: {0}'.format(result.etag)) except Exception as e: logging.error(e) return {'err_msg': "error when try to upload"} return {'result':True} def view(self): # 遍历文件目录 for b in islice(oss2.ObjectIterator(self.bucket), 10): print(b.key) def test_upload(inst): name = 'mhd/test2' content = 'hello, this is sunxiaoqi' rsp = inst.upload_str(name, content) print(rsp) if __name__ == '__main__': # i = CybexKYCFilesLocate(config.AccessKeyr_ID, config.AccessKeySecret, config.ENDPOINT, config.BUCKET_NAME) log_file_path = "/tmp/ali_file.log" # 开启日志 oss2.set_file_logger(log_file_path, 'oss2', logging.DEBUG) i = CybexKYCFilesLocate('LTAI4FjsgBVYLauRYbCpjYoH','07qLC0zHZXwfz1nvt5gm7GgDz4t8rK','http://oss-cn-beijing.aliyuncs.com','candybull-ieo-kyc') i.view()