def __init__(self, is_read=False): self.access_key_id = _get_config('ALIYUN_ACCESS_KEY') self.access_key_secret = _get_config('ALIYUN_ACCESS_SECRET') self.end_point = _normalize_endpoint( _get_config('ALIYUN_OSS_ENDPOINT').strip()) self.bucket_name = _get_config('ALIYUN_OSS_BUCKET') self.cdn_host = _get_config('ALIYUN_OSS_CDN_NAME') self.aliyun_bucket_create = _get_config('ALIYUN_BUCKET_CREATE', False) self.bucket_acl_type = _get_config('ALIYUN_BUCKET_ACL_TYPE', 'private') self.is_read = is_read # 上传文件是否公共读 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): if self.aliyun_bucket_create: # 创建bucket self.bucket = self._create_bucket(self.auth) raise FileExistsError(f'{self.bucket_name} is no exitis!') else: self.bucket = self._get_bucket(self.auth) except AccessDenied: # 当启用了RAM访问策略,是不允许list和create bucket的 self.bucket = self._get_bucket(self.auth)
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)
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') try: self.is_https = self._get_config('ALIYUN_OSS_HTTPS') except ImproperlyConfigured: self.is_https = False 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)
def get_buckets(auth=None): if not auth: auth = get_auth() service = Service(auth, 'https://oss.aliyuncs.com') return { i.name: dict(name=i.name, endpoint=i.extranet_endpoint, location=i.location) for i in BucketIterator(service) }
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) self.bucket = self._get_bucket(self.auth) self.bucket = self._get_bucket(self.auth)
def __init__(self): self.access_key_id = get_config('OSS2_ACCESS_KEY_ID') self.access_key_secret = get_config('OSS2_ACCESS_KEY_SECRET') self.end_point = _normalize_endpoint( get_config('OSS2_END_POINT').strip()) self.bucket_name = get_config('OSS2_BUCKET_NAME') self.auth = Auth(self.access_key_id, self.access_key_secret) self.service = Service(self.auth, self.end_point) if self.bucket_name not in self._list_bucket(self.service): self.bucket = self._create_bucket(self.auth) else: self.bucket = self._check_bucket_acl(self._get_bucket(self.auth))
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.auth = Auth(self.access_key_id, self.access_key_secret) self.service = Service(self.auth, self.end_point) 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))
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_ENDPOINT')) self.bucket_name = self._get_config('OSS_BUCKET_NAME') 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.get_bucket_acl().acl except oss2.exceptions.NoSuchBucket: raise SuspiciousOperation("Bucket '%s' does not exist." % self.bucket_name)
def bucket(self): if self.connected: return self._bucket else: 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.get_bucket_acl().acl except oss2.exceptions.NoSuchBucket: raise SuspiciousOperation("Bucket '%s' does not exist." % self.bucket_name) self.connected = True return self._bucket
def __init__(self, access_key_id=None, access_key_secret=None, end_point=None, bucket_name=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') sts_token = getattr(settings, 'ALIYUN_STS_TOKEN', None) # 这里表示如果有sts_token,需要使用stsauth进行鉴权 if sts_token: self.auth = StsAuth(self.access_key_id, self.access_key_secret, sts_token) else: self.auth = Auth(self.access_key_id, self.access_key_secret) self.service = Service(self.auth, self.end_point) use_oss_internal = getattr(settings, 'OSS_USE_INTERNAL', None) # 这里表示,如果是阿里云的内网机器,默认走内网的end_point,否则使用外网的end_point # 使用内网end_point,速度快,不收费 if use_oss_internal: self.end_point_internal = _normalize_endpoint( end_point if end_point else _get_config('OSS_ENDPOINT_INTERNAL' )) self.bucket = Bucket(self.auth, self.end_point_internal, self.bucket_name) else: self.bucket = Bucket(self.auth, self.end_point, self.bucket_name) self.bucket_public = Bucket(self.auth, self.end_point, self.bucket_name) # try to get bucket acl to check bucket exist or not try: self.bucket.get_bucket_acl().acl except oss2.exceptions.NoSuchBucket: raise SuspiciousOperation("Bucket '%s' does not exist." % self.bucket_name)
def __init__(self, access_key_id=None, access_key_secret=None, end_point=None, bucket_name=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.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)
def __init__(self, access_key_id, access_key_secret, end_point): self.access_key_id = access_key_id self.access_key_secret = access_key_secret self.end_point = end_point self.auth = Auth(self.access_key_id, self.access_key_secret) self.service = Service(self.auth, self.end_point)