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.

203 lines
5.8 KiB

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