mirror of
https://github.com/khairul169/garage-webui.git
synced 2025-11-30 04:51:04 +07:00
feat: added bucket cors requests using aws sdk
This commit is contained in:
parent
a095ea5611
commit
bd35a5ece1
@ -1,11 +1,16 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"khairul169/garage-webui/schema"
|
"khairul169/garage-webui/schema"
|
||||||
"khairul169/garage-webui/utils"
|
"khairul169/garage-webui/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Buckets struct{}
|
type Buckets struct{}
|
||||||
@ -52,3 +57,90 @@ func (b *Buckets) GetAll(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
utils.ResponseSuccess(w, res)
|
utils.ResponseSuccess(w, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b*Buckets) GetCors(w http.ResponseWriter, r *http.Request){
|
||||||
|
bucket := r.PathValue("bucket")
|
||||||
|
|
||||||
|
client, err := getS3Client(bucket)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
utils.ResponseError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := client.GetBucketCors(context.Background(), &s3.GetBucketCorsInput{
|
||||||
|
Bucket: aws.String(bucket),
|
||||||
|
})
|
||||||
|
|
||||||
|
res := []schema.BucketCors{}
|
||||||
|
|
||||||
|
for _, rule := range out.CORSRules {
|
||||||
|
res = append(res, schema.BucketCors{
|
||||||
|
AllowedOrigins: rule.AllowedOrigins,
|
||||||
|
AllowedMethods: rule.AllowedMethods,
|
||||||
|
AllowedHeaders: rule.AllowedHeaders,
|
||||||
|
ExposeHeaders: rule.ExposeHeaders,
|
||||||
|
MaxAgeSeconds: rule.MaxAgeSeconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
utils.ResponseError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.ResponseSuccess(w, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b*Buckets) PutCors(w http.ResponseWriter, r *http.Request){
|
||||||
|
bucket := r.PathValue("bucket")
|
||||||
|
|
||||||
|
|
||||||
|
var body struct {
|
||||||
|
Rules []schema.BucketCors `json:"rules"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
utils.ResponseError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := getS3Client(bucket)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
utils.ResponseError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cors := make([]types.CORSRule, 0, len(body.Rules))
|
||||||
|
|
||||||
|
for _, rule := range body.Rules {
|
||||||
|
if len(rule.AllowedMethods) == 0 || len(rule.AllowedOrigins) == 0 {
|
||||||
|
utils.ResponseError(w, fmt.Errorf("each CORS rule must have at least one allowed method and origin"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cors = append(cors, types.CORSRule{
|
||||||
|
AllowedHeaders: utils.NilIfEmpty(rule.AllowedHeaders),
|
||||||
|
AllowedMethods: rule.AllowedMethods, // required
|
||||||
|
AllowedOrigins: rule.AllowedOrigins, // required
|
||||||
|
ExposeHeaders: utils.NilIfEmpty(rule.ExposeHeaders),
|
||||||
|
MaxAgeSeconds: rule.MaxAgeSeconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := client.PutBucketCors(context.Background(), &s3.PutBucketCorsInput{
|
||||||
|
Bucket: aws.String(bucket),
|
||||||
|
CORSConfiguration: &types.CORSConfiguration{
|
||||||
|
CORSRules: cors,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
utils.ResponseError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.ResponseSuccess(w, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,8 @@ func HandleApiRouter() *http.ServeMux {
|
|||||||
|
|
||||||
buckets := &Buckets{}
|
buckets := &Buckets{}
|
||||||
router.HandleFunc("GET /buckets", buckets.GetAll)
|
router.HandleFunc("GET /buckets", buckets.GetAll)
|
||||||
|
router.HandleFunc("GET /buckets/{bucket}/cors", buckets.GetCors)
|
||||||
|
router.HandleFunc("PUT /buckets/{bucket}/cors", buckets.PutCors)
|
||||||
|
|
||||||
browse := &Browse{}
|
browse := &Browse{}
|
||||||
router.HandleFunc("GET /browse/{bucket}", browse.GetObjects)
|
router.HandleFunc("GET /browse/{bucket}", browse.GetObjects)
|
||||||
|
|||||||
@ -24,6 +24,14 @@ type Bucket struct {
|
|||||||
Created string `json:"created"`
|
Created string `json:"created"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BucketCors struct {
|
||||||
|
AllowedOrigins []string `json:"allowedOrigins"`
|
||||||
|
AllowedMethods []string `json:"allowedMethods"`
|
||||||
|
AllowedHeaders []string `json:"allowedHeaders"`
|
||||||
|
ExposeHeaders []string `json:"exposeHeaders"`
|
||||||
|
MaxAgeSeconds *int32 `json:"maxAgeSeconds"`
|
||||||
|
}
|
||||||
|
|
||||||
type LocalAlias struct {
|
type LocalAlias struct {
|
||||||
AccessKeyID string `json:"accessKeyId"`
|
AccessKeyID string `json:"accessKeyId"`
|
||||||
Alias string `json:"alias"`
|
Alias string `json:"alias"`
|
||||||
|
|||||||
@ -18,6 +18,13 @@ func LastString(str []string) string {
|
|||||||
return str[len(str)-1]
|
return str[len(str)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NilIfEmpty[T any](s []T) []T {
|
||||||
|
if len(s) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func ResponseError(w http.ResponseWriter, err error) {
|
func ResponseError(w http.ResponseWriter, err error) {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user