Buckets : bouton Vider (objets et envois multipart inachevés)

L'API d'administration ne sait pas supprimer d'objets : le serveur passe
par l'API S3 avec une clé temporaire (CreateKey + AllowBucketKey),
supprime les objets par lots de 1000 (ListObjectsV2 + DeleteObjects,
signature v4 en bibliothèque standard), nettoie les envois inachevés
(CleanupIncompleteUploads, olderThanSecs 0) puis supprime la clé.
Avancement diffusé en NDJSON (POST /api/buckets/empty?id=...).
Confirmation par saisie du nom, interruption et reprise possibles.
Nouvelle variable GARAGE_S3_API_URL (défaut : hôte admin, port 3900).
Mock : API S3 minimale sur :3900. Tests : signature (exemple AWS) et
vidage complet simulé.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNfwm2bSk3o3YKYUP5P691
This commit is contained in:
claude BotandClaude Opus 5.5 committed 2026-11-27 12:15:50 +01:00
1 parent ea2058b6c1
commit e21ef321f9
7 files changed
+278 -8

No files matched your search

+100 -1
View File
@@ -6,10 +6,13 @@ package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"os"
"sort"
"strings"
"sync"
"time"
@@ -33,6 +36,18 @@ type bucket struct {
MaxSize *int64
MaxObjs *int64
Keys map[string]perm
Names map[string]bool // objets côté S3, créés à la première requête S3 (Objects noms factices)
}
// s3Names renvoie les objets du bucket, générés à partir de Objects au premier appel.
func (b *bucket) s3Names() map[string]bool {
if b.Names == nil {
b.Names = map[string]bool{}
for i := 0; i < b.Objects; i++ {
b.Names[fmt.Sprintf("donnees/objet-%06d.bin", i)] = true
}
}
return b.Names
}
type key struct {
@@ -330,6 +345,85 @@ func handle(token string) http.HandlerFunc {
}
}
// handleS3 imite juste ce dont le vidage a besoin : ListObjectsV2 et DeleteObjects, en style path.
// La signature n'est pas vérifiée, seule la présence d'une clé connue l'est.
func handleS3(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
auth := r.Header.Get("Authorization")
known := false
for id := range keys {
if strings.Contains(auth, "Credential="+id+"/") {
known = true
}
}
if !known {
w.WriteHeader(403)
fmt.Fprint(w, `<Error><Code>InvalidAccessKeyId</Code><Message>clé inconnue</Message></Error>`)
return
}
name := strings.Trim(r.URL.Path, "/")
var b *bucket
for _, x := range buckets {
for _, a := range x.Aliases {
if a == name {
b = x
}
}
}
if b == nil {
w.WriteHeader(404)
fmt.Fprint(w, `<Error><Code>NoSuchBucket</Code><Message>bucket introuvable</Message></Error>`)
return
}
q := r.URL.Query()
names := b.s3Names()
log.Printf("S3 %s /%s?%s", r.Method, name, r.URL.RawQuery)
switch {
case r.Method == "GET" && q.Get("list-type") == "2":
var all []string
for k := range names {
if k > q.Get("continuation-token") {
all = append(all, k)
}
}
sort.Strings(all)
trunc := len(all) > 1000
if trunc {
all = all[:1000]
}
fmt.Fprint(w, `<ListBucketResult>`)
for _, k := range all {
fmt.Fprintf(w, `<Contents><Key>%s</Key></Contents>`, k)
}
if trunc {
fmt.Fprintf(w, `<IsTruncated>true</IsTruncated><NextContinuationToken>%s</NextContinuationToken>`, all[len(all)-1])
}
fmt.Fprint(w, `</ListBucketResult>`)
case r.Method == "POST" && q.Has("delete"):
var in struct {
Objects []struct{ Key string } `xml:"Object"`
}
data, _ := io.ReadAll(r.Body)
_ = xml.Unmarshal(data, &in)
for _, o := range in.Objects {
if names[o.Key] {
delete(names, o.Key)
b.Objects--
if b.Objects > 0 {
b.Bytes -= b.Bytes / int64(b.Objects+1)
} else {
b.Bytes = 0
}
}
}
time.Sleep(300 * time.Millisecond) // pour voir l'avancement dans l'interface
fmt.Fprint(w, `<DeleteResult></DeleteResult>`)
default:
w.WriteHeader(501)
}
}
func main() {
token := "test"
if v := os.Getenv("MOCK_TOKEN"); v != "" {
@@ -339,7 +433,12 @@ func main() {
if v := os.Getenv("MOCK_ADDR"); v != "" {
addr = v
}
log.Printf("mockgarage sur %s (jeton %q)", addr, token)
s3Addr := ":3900"
if v := os.Getenv("MOCK_S3_ADDR"); v != "" {
s3Addr = v
}
log.Printf("mockgarage sur %s (jeton %q), S3 sur %s", addr, token, s3Addr)
go func() { log.Fatal(http.ListenAndServe(s3Addr, http.HandlerFunc(handleS3))) }()
http.Handle("/v2/", handle(token))
log.Fatal(http.ListenAndServe(addr, nil))
}