Exemplo n.º 1
0
        public Task <bool> CreateBucketAsync(string bucketName)
        {
            string apiUrl = $"{QiniuApi.GetServiceApi(Options)}/mkbucketv3/{bucketName}/region/{GetRegionZone(Options.Region)}";
            string token  = _auth.CreateManageToken(apiUrl);

            HttpResult hr = _http.Post(apiUrl, token);

            switch (hr.Code)
            {
            case 200:
                return(Task.FromResult(true));

            case 614:
                throw new BucketExistException($"Bucket '{bucketName}' already exists.");

            default:
            {
                if (!string.IsNullOrEmpty(hr.Text))
                {
                    QiniuError error = JsonUtil.DeserializeObject <QiniuError>(hr.Text);
                    throw new Exception($"Create bucket [{bucketName}] failed, error code: {hr.Code}, error msg: {error.error}");
                }
                throw new Exception($"Create bucket [{bucketName}] failed, error code: {hr.Code}, text: {hr.Text}");
            }
            }
        }
Exemplo n.º 2
0
        public Task <AccessMode> GetBucketAclAsync(string bucketName)
        {
            string     bucketsUrl = QiniuApi.GetBaseApi(_config.Zone.RsHost, Options) + "/bucket/" + bucketName;
            string     token      = _auth.CreateManageToken(bucketsUrl);
            HttpResult hr         = _http.Get(bucketsUrl, token);

            if (hr.Code != (int)HttpCode.OK)
            {
                if (!string.IsNullOrEmpty(hr.Text))
                {
                    QiniuError error = JsonUtil.DeserializeObject <QiniuError>(hr.Text);
                    throw new Exception($"Get bucket acl failed, error code: {hr.Code}, error msg: {error.error}");
                }
                throw new Exception($"Get bucket acl failed, error code: {hr.Code}, text: {hr.Text}");
            }
            QiniuFullBucketInfo ret = JsonUtil.DeserializeObject <QiniuFullBucketInfo>(hr.Text);

            return(ret.@private switch
            {
                1 => Task.FromResult(AccessMode.Private),
                _ => Task.FromResult(AccessMode.PublicRead),
            });
Exemplo n.º 3
0
        public Task <bool> RemoveBucketAsync(string bucketName)
        {
            string apiUrl = $"{QiniuApi.GetServiceApi(Options)}/drop/{bucketName}";
            string token  = _auth.CreateManageToken(apiUrl);

            HttpResult hr = _http.Post(apiUrl, token);

            switch (hr.Code)
            {
            case 200:
                return(Task.FromResult(true));

            default:
            {
                if (!string.IsNullOrEmpty(hr.Text))
                {
                    QiniuError error = JsonUtil.DeserializeObject <QiniuError>(hr.Text);
                    throw new Exception($"Remove bucket [{bucketName}] failed, error code: {hr.Code}, error msg: {error.error}");
                }
                throw new Exception($"Remove bucket [{bucketName}] failed, error code: {hr.Code}, text: {hr.Text}");
            }
            }
        }
Exemplo n.º 4
0
        public Task <bool> BucketExistsAsync(string bucketName)
        {
            string     bucketsUrl = QiniuApi.GetBaseApi(_config.Zone.RsHost, Options) + "/bucket/" + bucketName;
            string     token      = _auth.CreateManageToken(bucketsUrl);
            HttpResult hr         = _http.Get(bucketsUrl, token);

            if (hr.Code == (int)HttpCode.OK && !string.IsNullOrEmpty(hr.Text))
            {
                return(Task.FromResult(true));
            }
            else if (hr.Code == 612)
            {
                return(Task.FromResult(false));
            }
            else
            {
                if (!string.IsNullOrEmpty(hr.Text))
                {
                    QiniuError error = JsonUtil.DeserializeObject <QiniuError>(hr.Text);
                    throw new Exception($"Get bucket exist status failed, error code: {hr.Code}, error msg: {error.error}");
                }
                throw new Exception($"Get bucket exist status failed, error code: {hr.Code}, text: {hr.Text}");
            }
        }