You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.9 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
6 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  6. "io"
  7. "net/http"
  8. "slices"
  9. "strings"
  10. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  11. "golang.org/x/exp/slices"
  12. "github.com/seaweedfs/seaweedfs/weed/filer"
  13. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  14. "github.com/seaweedfs/seaweedfs/weed/glog"
  15. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  16. "github.com/seaweedfs/seaweedfs/weed/util"
  17. )
  18. const (
  19. deleteMultipleObjectsLimit = 1000
  20. )
  21. func (s3a *S3ApiServer) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
  22. bucket, object := s3_constants.GetBucketAndObject(r)
  23. glog.V(3).Infof("DeleteObjectHandler %s %s", bucket, object)
  24. target := util.FullPath(fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, bucket, object))
  25. dir, name := target.DirAndName()
  26. err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  27. if err := doDeleteEntry(client, dir, name, true, false); err != nil {
  28. return err
  29. }
  30. if s3a.option.AllowEmptyFolder {
  31. return nil
  32. }
  33. directoriesWithDeletion := make(map[string]int)
  34. if strings.LastIndex(object, "/") > 0 {
  35. directoriesWithDeletion[dir]++
  36. // purge empty folders, only checking folders with deletions
  37. for len(directoriesWithDeletion) > 0 {
  38. directoriesWithDeletion = s3a.doDeleteEmptyDirectories(client, directoriesWithDeletion)
  39. }
  40. }
  41. return nil
  42. })
  43. if err != nil {
  44. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  45. return
  46. }
  47. w.WriteHeader(http.StatusNoContent)
  48. }
  49. // / ObjectIdentifier carries key name for the object to delete.
  50. type ObjectIdentifier struct {
  51. ObjectName string `xml:"Key"`
  52. }
  53. // DeleteObjectsRequest - xml carrying the object key names which needs to be deleted.
  54. type DeleteObjectsRequest struct {
  55. // Element to enable quiet mode for the request
  56. Quiet bool
  57. // List of objects to be deleted
  58. Objects []ObjectIdentifier `xml:"Object"`
  59. }
  60. // DeleteError structure.
  61. type DeleteError struct {
  62. Code string
  63. Message string
  64. Key string
  65. }
  66. // DeleteObjectsResponse container for multiple object deletes.
  67. type DeleteObjectsResponse struct {
  68. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ DeleteResult" json:"-"`
  69. // Collection of all deleted objects
  70. DeletedObjects []ObjectIdentifier `xml:"Deleted,omitempty"`
  71. // Collection of errors deleting certain objects.
  72. Errors []DeleteError `xml:"Error,omitempty"`
  73. }
  74. // DeleteMultipleObjectsHandler - Delete multiple objects
  75. func (s3a *S3ApiServer) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Request) {
  76. bucket, _ := s3_constants.GetBucketAndObject(r)
  77. glog.V(3).Infof("DeleteMultipleObjectsHandler %s", bucket)
  78. deleteXMLBytes, err := io.ReadAll(r.Body)
  79. if err != nil {
  80. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  81. return
  82. }
  83. deleteObjects := &DeleteObjectsRequest{}
  84. if err := xml.Unmarshal(deleteXMLBytes, deleteObjects); err != nil {
  85. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
  86. return
  87. }
  88. if len(deleteObjects.Objects) > deleteMultipleObjectsLimit {
  89. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxDeleteObjects)
  90. return
  91. }
  92. var deletedObjects []ObjectIdentifier
  93. var deleteErrors []DeleteError
  94. var auditLog *s3err.AccessLog
  95. directoriesWithDeletion := make(map[string]int)
  96. if s3err.Logger != nil {
  97. auditLog = s3err.GetAccessLog(r, http.StatusNoContent, s3err.ErrNone)
  98. }
  99. s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  100. // delete file entries
  101. for _, object := range deleteObjects.Objects {
  102. if object.ObjectName == "" {
  103. continue
  104. }
  105. lastSeparator := strings.LastIndex(object.ObjectName, "/")
  106. parentDirectoryPath, entryName, isDeleteData, isRecursive := "", object.ObjectName, true, false
  107. if lastSeparator > 0 && lastSeparator+1 < len(object.ObjectName) {
  108. entryName = object.ObjectName[lastSeparator+1:]
  109. parentDirectoryPath = "/" + object.ObjectName[:lastSeparator]
  110. }
  111. parentDirectoryPath = fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, bucket, parentDirectoryPath)
  112. err := doDeleteEntry(client, parentDirectoryPath, entryName, isDeleteData, isRecursive)
  113. if err == nil {
  114. directoriesWithDeletion[parentDirectoryPath]++
  115. deletedObjects = append(deletedObjects, object)
  116. } else if strings.Contains(err.Error(), filer.MsgFailDelNonEmptyFolder) {
  117. deletedObjects = append(deletedObjects, object)
  118. } else {
  119. delete(directoriesWithDeletion, parentDirectoryPath)
  120. deleteErrors = append(deleteErrors, DeleteError{
  121. Code: "",
  122. Message: err.Error(),
  123. Key: object.ObjectName,
  124. })
  125. }
  126. if auditLog != nil {
  127. auditLog.Key = entryName
  128. s3err.PostAccessLog(*auditLog)
  129. }
  130. }
  131. if s3a.option.AllowEmptyFolder {
  132. return nil
  133. }
  134. // purge empty folders, only checking folders with deletions
  135. for len(directoriesWithDeletion) > 0 {
  136. directoriesWithDeletion = s3a.doDeleteEmptyDirectories(client, directoriesWithDeletion)
  137. }
  138. return nil
  139. })
  140. deleteResp := DeleteObjectsResponse{}
  141. if !deleteObjects.Quiet {
  142. deleteResp.DeletedObjects = deletedObjects
  143. }
  144. deleteResp.Errors = deleteErrors
  145. writeSuccessResponseXML(w, r, deleteResp)
  146. }
  147. func (s3a *S3ApiServer) doDeleteEmptyDirectories(client filer_pb.SeaweedFilerClient, directoriesWithDeletion map[string]int) (newDirectoriesWithDeletion map[string]int) {
  148. var allDirs []string
  149. for dir := range directoriesWithDeletion {
  150. allDirs = append(allDirs, dir)
  151. }
  152. slices.SortFunc(allDirs, func(a, b string) int {
  153. return len(b) - len(a)
  154. })
  155. newDirectoriesWithDeletion = make(map[string]int)
  156. for _, dir := range allDirs {
  157. parentDir, dirName := util.FullPath(dir).DirAndName()
  158. if parentDir == s3a.option.BucketsPath {
  159. continue
  160. }
  161. if err := doDeleteEntry(client, parentDir, dirName, false, false); err != nil {
  162. glog.V(4).Infof("directory %s has %d deletion but still not empty: %v", dir, directoriesWithDeletion[dir], err)
  163. } else {
  164. newDirectoriesWithDeletion[parentDir]++
  165. }
  166. }
  167. return
  168. }