本文整理汇总了Python中oss2.Service方法的典型用法代码示例。如果您正苦于以下问题:Python oss2.Service方法的具体用法?Python oss2.Service怎么用?Python oss2.Service使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oss2
的用法示例。
在下文中一共展示了oss2.Service方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def __init__(self):
self.access_key_id = self._get_config('OSS_ACCESS_KEY_ID')
self.access_key_secret = self._get_config('OSS_ACCESS_KEY_SECRET')
self.end_point = _normalize_endpoint(self._get_config('OSS_END_POINT').strip())
self.bucket_name = self._get_config('OSS_BUCKET_NAME')
self.cname = self._get_config('OSS_CNAME')
self.auth = Auth(self.access_key_id, self.access_key_secret)
self.service = Service(self.auth, self.end_point)
try:
if self.bucket_name not in self._list_bucket(self.service):
# create bucket if not exists
self.bucket = self._create_bucket(self.auth)
else:
# change bucket acl if not consists
self.bucket = self._check_bucket_acl(self._get_bucket(self.auth))
except AccessDenied:
# 当启用了RAM访问策略,是不允许list和create bucket的
self.bucket = self._get_bucket(self.auth)
示例2: oss_check
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def oss_check(self, bucket_name, default_endpoint, access_key_id, access_key_secret):
if bucket_name in self.bucket_endpoints:
endpoint = self.bucket_endpoints[bucket_name]
try:
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name, connect_timeout=5.0, app_name=defaults.app_name)
res = bucket.get_bucket_acl()
except oss2.exceptions.OssError as e:
raise AuthenticationFailed("access bucket:%s using specified \
endpoint:%s failed. request_id:%s, status:%s, code:%s, message:%s" % (bucket_name, endpoint, e.request_id, unicode(e.status), e.code, e.message))
return endpoint
try:
service = oss2.Service(oss2.Auth(access_key_id, access_key_secret), default_endpoint, app_name=defaults.app_name)
res = service.list_buckets(prefix=bucket_name)
except oss2.exceptions.AccessDenied as e:
raise AuthenticationFailed("can't list buckets, check your access_key.request_id:%s, status:%s, code:%s, message:%s"% (e.request_id, unicode(e.status), e.code, e.message))
except oss2.exceptions.OssError as e:
raise AuthenticationFailed("list buckets error. request_id:%s, status:%s, code:%s, message:%s" % (e.request_id, unicode(e.status), e.code, e.message))
bucket_list = res.buckets
for bucket in bucket_list:
if bucket.name == bucket_name:
endpoint = self.get_endpoint(bucket_name, bucket.location.decode('utf-8'), access_key_id, access_key_secret)
return endpoint
raise AuthenticationFailed("can't find the bucket %s when list buckets." % bucket_name)
示例3: __init__
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def __init__(self, access_key_id=None, access_key_secret=None, end_point=None, bucket_name=None, expire_time=None):
self.access_key_id = access_key_id if access_key_id else _get_config('OSS_ACCESS_KEY_ID')
self.access_key_secret = access_key_secret if access_key_secret else _get_config('OSS_ACCESS_KEY_SECRET')
self.end_point = _normalize_endpoint(end_point if end_point else _get_config('OSS_ENDPOINT'))
self.bucket_name = bucket_name if bucket_name else _get_config('OSS_BUCKET_NAME')
self.expire_time = expire_time if expire_time else int(_get_config('OSS_EXPIRE_TIME', default=60*60*24*30))
self.auth = Auth(self.access_key_id, self.access_key_secret)
self.service = Service(self.auth, self.end_point)
self.bucket = Bucket(self.auth, self.end_point, self.bucket_name)
# try to get bucket acl to check bucket exist or not
try:
self.bucket_acl = self.bucket.get_bucket_acl().acl
except oss2.exceptions.NoSuchBucket:
raise SuspiciousOperation("Bucket '%s' does not exist." % self.bucket_name)
示例4: __init__
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def __init__(self):
self.access_key_id = self._get_config('ACCESS_KEY_ID')
self.access_key_secret = self._get_config('ACCESS_KEY_SECRET')
self.end_point = _normalize_endpoint(self._get_config('END_POINT').strip())
self.bucket_name = self._get_config('BUCKET_NAME')
self.cname = self._get_config('ALIYUN_OSS_CNAME')
self.auth = Auth(self.access_key_id, self.access_key_secret)
self.service = Service(self.auth, self.end_point)
try:
if self.bucket_name not in self._list_bucket(self.service):
# create bucket if not exists
self.bucket = self._create_bucket(self.auth)
else:
# change bucket acl if not consists
self.bucket = self._check_bucket_acl(self._get_bucket(self.auth))
except AccessDenied:
# 当启用了RAM访问策略,是不允许list和create bucket的
self.bucket = self._get_bucket(self.auth)
示例5: get_oss_client
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def get_oss_client(credentials, region=None):
try:
auth = oss2.Auth(credentials.credentials.access_key_id, credentials.credentials.access_key_secret)
client = oss2.Service(auth,
endpoint='oss-{}.aliyuncs.com'.format(region) if region
else 'oss-cn-hangzhou.aliyuncs.com')
return client
except Exception as e:
print_exception(e)
return None
示例6: __init__
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def __init__(self, access_key_id, access_key_secret, host, bucket_name, keeps=None):
"""
access_key_id:
access_key_secret:
host: 域名,如 http://oss-cn-hangzhou.aliyuncs.com
bucket_name: 不需要在后台自动创建,也会自动创建好
"""
super(OSSBackend, self).__init__(keeps)
auth = oss2.Auth(access_key_id, access_key_secret)
self.bucket = oss2.Bucket(auth, host, bucket_name)
service = oss2.Service(auth, host)
if bucket_name not in [b.name for b in oss2.BucketIterator(service)]:
# 说明bucket不存在
self.bucket.create_bucket()
示例7: service
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def service():
return oss2.Service(oss2.Auth('fake-access-key-id', 'fake-access-key-secret'),
'http://oss-cn-hangzhou.aliyuncs.com')
示例8: test_bucket_iterator
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def test_bucket_iterator(self):
service = oss2.Service(oss2.Auth(OSS_ID, OSS_SECRET), OSS_ENDPOINT)
self.assertTrue(OSS_BUCKET in (b.name for b in oss2.BucketIterator(service, max_keys=2)))
示例9: clean_and_delete_bucket_by_prefix
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def clean_and_delete_bucket_by_prefix(bucket_prefix):
service = oss2.Service(oss2.Auth(OSS_ID, OSS_SECRET), OSS_ENDPOINT)
buckets = service.list_buckets(prefix=bucket_prefix).buckets
for b in buckets:
bucket = oss2.Bucket(oss2.Auth(OSS_ID, OSS_SECRET), b.extranet_endpoint, b.name)
clean_and_delete_bucket(bucket)
示例10: test_user_agent
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
def test_user_agent(self):
app = 'fantastic-tool'
assert_found = False
def do_request(session_self, req, timeout):
if assert_found:
self.assertTrue(req.headers['User-Agent'].find(app) >= 0)
else:
self.assertTrue(req.headers['User-Agent'].find(app) < 0)
raise oss2.exceptions.ClientError('intentional')
from unittest.mock import patch
with patch.object(oss2.Session, 'do_request', side_effect=do_request, autospec=True):
# 不加 app_name
assert_found = False
self.assertRaises(oss2.exceptions.ClientError, self.bucket.get_bucket_acl)
service = oss2.Service(oss2.Auth(OSS_ID, OSS_SECRET), OSS_ENDPOINT)
self.assertRaises(oss2.exceptions.ClientError, service.list_buckets)
# 加app_name
assert_found = True
bucket = oss2.Bucket(oss2.Auth(OSS_ID, OSS_SECRET), OSS_ENDPOINT, OSS_BUCKET,
app_name=app)
self.assertRaises(oss2.exceptions.ClientError, bucket.get_bucket_acl)
service = oss2.Service(oss2.Auth(OSS_ID, OSS_SECRET), OSS_ENDPOINT,
app_name=app)
self.assertRaises(oss2.exceptions.ClientError, service.list_buckets)
示例11: __init__
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import Service [as 别名]
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)