Пример #1
0
    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)
Пример #2
0
    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 test_check_endpoint(self):
        from oss2.api import _normalize_endpoint

        endpoint = 'www.aliyuncs.com/?x=123#segemnt '
        oss2.Bucket(oss2.AnonymousAuth(), endpoint, 'test-bucket')
        normalized_endpoint = _normalize_endpoint(endpoint)
        self.assertEqual('http://www.aliyuncs.com', normalized_endpoint)

        endpoint = 'http://www.aliyuncs.com/?x=123#segemnt '
        oss2.Bucket(oss2.AnonymousAuth(), endpoint, 'test-bucket')
        normalized_endpoint = _normalize_endpoint(endpoint)
        self.assertEqual('http://www.aliyuncs.com', normalized_endpoint)

        endpoint = 'http://192.168.1.1:3182/?x=123#segemnt '
        oss2.Bucket(oss2.AnonymousAuth(), endpoint, 'test-bucket')
        normalized_endpoint = _normalize_endpoint(endpoint)
        self.assertEqual('http://192.168.1.1:3182', normalized_endpoint)

        endpoint = 'http://www.aliyuncs.com:80/?x=123#segemnt '
        oss2.Bucket(oss2.AnonymousAuth(), endpoint, 'test-bucket')
        normalized_endpoint = _normalize_endpoint(endpoint)
        self.assertEqual('http://www.aliyuncs.com:80', normalized_endpoint)

        endpoint = 'https://www.aliyuncs.com:443/?x=123#segemnt'
        oss2.Bucket(oss2.AnonymousAuth(), endpoint, 'test-bucket')
        normalized_endpoint = _normalize_endpoint(endpoint)
        self.assertEqual('https://www.aliyuncs.com:443', normalized_endpoint)

        endpoint = 'https://www.aliyuncs.com:443'
        oss2.Bucket(oss2.AnonymousAuth(), endpoint, 'test-bucket')
        normalized_endpoint = _normalize_endpoint(endpoint)
        self.assertEqual('https://www.aliyuncs.com:443', normalized_endpoint)

        self.assertRaises(oss2.exceptions.ClientError, oss2.Bucket, oss2.AnonymousAuth(), OSS_ENDPOINT + '\\',
                          'test-bucket')
Пример #4
0
    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)
Пример #5
0
    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))
Пример #7
0
    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.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))
        # make sure the bucket must be there
        self.bucket = Bucket(self.auth, self.end_point, self.bucket_name)