diff --git a/.gitignore b/.gitignore index cd25905..ee36ccd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -.idea -private.key -cert +.idea +private.key +cert +.vscode \ No newline at end of file diff --git a/api/bucket.go b/api/bucket.go index 59e8f38..ebed165 100644 --- a/api/bucket.go +++ b/api/bucket.go @@ -25,12 +25,12 @@ func (a *API) GetBucket(c *fiber.Ctx) error { if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } + err = client.SetBucketQuota(100 << 20) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } } - err = client.SetBucketQuota(100 << 20) - if err != nil { - return fiber.NewError(fiber.StatusInternalServerError, err.Error()) - } info, err := client.GetBucketInfoWithStats() if err != nil { diff --git a/dal/bucket.go b/dal/bucket.go index 0c08b69..aa411b8 100644 --- a/dal/bucket.go +++ b/dal/bucket.go @@ -5,9 +5,9 @@ import ( "encoding/json" "fmt" + "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/policy" "github.com/minio/minio-go/v7/pkg/set" - "github.com/minio/minio-go/v7" "penahub.gitlab.yandexcloud.net/backend/penahub_disk/dal/yaCloud" ) diff --git a/dal/yaCloud/bucket.go b/dal/yaCloud/bucket.go index 6d1ef69..ed27035 100644 --- a/dal/yaCloud/bucket.go +++ b/dal/yaCloud/bucket.go @@ -5,7 +5,6 @@ import ( "crypto/rsa" "encoding/json" "fmt" - "github.com/golang-jwt/jwt/v4" "io" "log" "net/http" @@ -13,18 +12,20 @@ import ( "os" "strings" "time" + + "github.com/golang-jwt/jwt/v4" ) var ( - keyID = "" - serviceAccountID = "" + keyID = "aje4u394kp05ijl74ce3" + serviceAccountID = "ajelmc4tjbct675tjdh9" keyFile = "private.key" endpoint = "https://storage.api.cloud.yandex.net/storage/v1/buckets" ) func init() { - keyID = os.Getenv("YA_KEY_ID") - serviceAccountID = os.Getenv("YA_SERVICE_ACC_ID") + // keyID = os.Getenv("YA_KEY_ID") + // serviceAccountID = os.Getenv("YA_SERVICE_ACC_ID") if val, ok := os.LookupEnv("YA_KEY_FILE"); ok { keyFile = val } @@ -116,9 +117,12 @@ func (c *Client) GetBucketStats() (*BucketStats, error) { } func (c *Client) UpdateBucketMaxSize(quota uint64) error { - href := fmt.Sprintf("https://%v/storage/v1/buckets/%v", endpoint, c.bucket) + href := fmt.Sprintf("%v/%v", endpoint, c.bucket) body, err := json.Marshal(NewReqUpdateBucketMaxSize(quota)) + if err != nil { + return err + } req, err := http.NewRequest("PATCH", href, bytes.NewBuffer(body)) if err != nil { @@ -126,16 +130,19 @@ func (c *Client) UpdateBucketMaxSize(quota uint64) error { } resp, err := c.client.Do(req) - defer resp.Body.Close() + if err != nil { return err } + defer resp.Body.Close() + if err = checkError(resp); err != nil { return err } var r RespUpdateBucketMaxSize + if err = json.NewDecoder(resp.Body).Decode(&r); err != nil { return err } @@ -151,7 +158,10 @@ func checkError(resp *http.Response) error { switch resp.StatusCode { case 200, 201, 202, 203, 204, 205, 206, 207, 208, 226: return nil + case 404: + return fmt.Errorf("api/yaCloud (404) url: %v", resp.Request.URL) } + r := RespError{} err := json.NewDecoder(resp.Body).Decode(&r) if err != nil {