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.

459 lines
14 KiB

7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
2 years ago
2 years ago
4 years ago
4 years ago
2 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/seaweedfs/seaweedfs/weed/filer"
  15. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  16. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  17. )
  18. const cutoffTimeNewEmptyDir = 3
  19. type ListBucketResultV2 struct {
  20. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListBucketResult"`
  21. Name string `xml:"Name"`
  22. Prefix string `xml:"Prefix"`
  23. MaxKeys int `xml:"MaxKeys"`
  24. Delimiter string `xml:"Delimiter,omitempty"`
  25. IsTruncated bool `xml:"IsTruncated"`
  26. Contents []ListEntry `xml:"Contents,omitempty"`
  27. CommonPrefixes []PrefixEntry `xml:"CommonPrefixes,omitempty"`
  28. ContinuationToken string `xml:"ContinuationToken,omitempty"`
  29. NextContinuationToken string `xml:"NextContinuationToken,omitempty"`
  30. KeyCount int `xml:"KeyCount"`
  31. StartAfter string `xml:"StartAfter,omitempty"`
  32. }
  33. func (s3a *S3ApiServer) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request) {
  34. // https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
  35. // collect parameters
  36. bucket, _ := s3_constants.GetBucketAndObject(r)
  37. glog.V(3).Infof("ListObjectsV2Handler %s", bucket)
  38. originalPrefix, continuationToken, startAfter, delimiter, _, maxKeys := getListObjectsV2Args(r.URL.Query())
  39. if maxKeys < 0 {
  40. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxKeys)
  41. return
  42. }
  43. if delimiter != "" && delimiter != "/" {
  44. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  45. return
  46. }
  47. marker := continuationToken
  48. if continuationToken == "" {
  49. marker = startAfter
  50. }
  51. response, err := s3a.listFilerEntries(bucket, originalPrefix, maxKeys, marker, delimiter)
  52. if err != nil {
  53. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  54. return
  55. }
  56. if len(response.Contents) == 0 {
  57. if exists, existErr := s3a.exists(s3a.option.BucketsPath, bucket, true); existErr == nil && !exists {
  58. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  59. return
  60. }
  61. }
  62. responseV2 := &ListBucketResultV2{
  63. XMLName: response.XMLName,
  64. Name: response.Name,
  65. CommonPrefixes: response.CommonPrefixes,
  66. Contents: response.Contents,
  67. ContinuationToken: continuationToken,
  68. Delimiter: response.Delimiter,
  69. IsTruncated: response.IsTruncated,
  70. KeyCount: len(response.Contents) + len(response.CommonPrefixes),
  71. MaxKeys: response.MaxKeys,
  72. NextContinuationToken: response.NextMarker,
  73. Prefix: response.Prefix,
  74. StartAfter: startAfter,
  75. }
  76. writeSuccessResponseXML(w, r, responseV2)
  77. }
  78. func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
  79. // https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html
  80. // collect parameters
  81. bucket, _ := s3_constants.GetBucketAndObject(r)
  82. glog.V(3).Infof("ListObjectsV1Handler %s", bucket)
  83. originalPrefix, marker, delimiter, maxKeys := getListObjectsV1Args(r.URL.Query())
  84. if maxKeys < 0 {
  85. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxKeys)
  86. return
  87. }
  88. if delimiter != "" && delimiter != "/" {
  89. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  90. return
  91. }
  92. response, err := s3a.listFilerEntries(bucket, originalPrefix, maxKeys, marker, delimiter)
  93. if err != nil {
  94. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  95. return
  96. }
  97. if len(response.Contents) == 0 {
  98. if exists, existErr := s3a.exists(s3a.option.BucketsPath, bucket, true); existErr == nil && !exists {
  99. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  100. return
  101. }
  102. }
  103. writeSuccessResponseXML(w, r, response)
  104. }
  105. func (s3a *S3ApiServer) listFilerEntries(bucket string, originalPrefix string, maxKeys int, originalMarker string, delimiter string) (response ListBucketResult, err error) {
  106. // convert full path prefix into directory name and prefix for entry name
  107. requestDir, prefix, marker := normalizePrefixMarker(originalPrefix, originalMarker)
  108. bucketPrefix := fmt.Sprintf("%s/%s/", s3a.option.BucketsPath, bucket)
  109. reqDir := bucketPrefix[:len(bucketPrefix)-1]
  110. if requestDir != "" {
  111. reqDir = fmt.Sprintf("%s%s", bucketPrefix, requestDir)
  112. }
  113. var contents []ListEntry
  114. var commonPrefixes []PrefixEntry
  115. var doErr error
  116. var nextMarker string
  117. cursor := &ListingCursor{
  118. maxKeys: maxKeys,
  119. }
  120. // check filer
  121. err = s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  122. nextMarker, doErr = s3a.doListFilerEntries(client, reqDir, prefix, cursor, marker, delimiter, false, func(dir string, entry *filer_pb.Entry) {
  123. if entry.IsDirectory {
  124. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
  125. if delimiter == "/" { // A response can contain CommonPrefixes only if you specify a delimiter.
  126. commonPrefixes = append(commonPrefixes, PrefixEntry{
  127. Prefix: fmt.Sprintf("%s/%s/", dir, entry.Name)[len(bucketPrefix):],
  128. })
  129. //All of the keys (up to 1,000) rolled up into a common prefix count as a single return when calculating the number of returns.
  130. cursor.maxKeys--
  131. } else if entry.IsDirectoryKeyObject() {
  132. contents = append(contents, ListEntry{
  133. Key: fmt.Sprintf("%s/%s/", dir, entry.Name)[len(bucketPrefix):],
  134. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  135. ETag: "\"" + filer.ETag(entry) + "\"",
  136. Owner: CanonicalUser{
  137. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  138. DisplayName: entry.Attributes.UserName,
  139. },
  140. StorageClass: "STANDARD",
  141. })
  142. cursor.maxKeys--
  143. }
  144. } else {
  145. storageClass := "STANDARD"
  146. if v, ok := entry.Extended[s3_constants.AmzStorageClass]; ok {
  147. storageClass = string(v)
  148. }
  149. contents = append(contents, ListEntry{
  150. Key: fmt.Sprintf("%s/%s", dir, entry.Name)[len(bucketPrefix):],
  151. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  152. ETag: "\"" + filer.ETag(entry) + "\"",
  153. Size: int64(filer.FileSize(entry)),
  154. Owner: CanonicalUser{
  155. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  156. DisplayName: entry.Attributes.UserName,
  157. },
  158. StorageClass: StorageClass(storageClass),
  159. })
  160. cursor.maxKeys--
  161. }
  162. })
  163. if doErr != nil {
  164. return doErr
  165. }
  166. if !cursor.isTruncated {
  167. nextMarker = ""
  168. } else {
  169. if requestDir != "" {
  170. nextMarker = requestDir + "/" + nextMarker
  171. }
  172. }
  173. response = ListBucketResult{
  174. Name: bucket,
  175. Prefix: originalPrefix,
  176. Marker: originalMarker,
  177. NextMarker: nextMarker,
  178. MaxKeys: maxKeys,
  179. Delimiter: delimiter,
  180. IsTruncated: cursor.isTruncated,
  181. Contents: contents,
  182. CommonPrefixes: commonPrefixes,
  183. }
  184. return nil
  185. })
  186. return
  187. }
  188. type ListingCursor struct {
  189. maxKeys int
  190. isTruncated bool
  191. }
  192. // the prefix and marker may be in different directories
  193. // normalizePrefixMarker ensures the prefix and marker both starts from the same directory
  194. func normalizePrefixMarker(prefix, marker string) (alignedDir, alignedPrefix, alignedMarker string) {
  195. // alignedDir should not end with "/"
  196. // alignedDir, alignedPrefix, alignedMarker should only have "/" in middle
  197. prefix = strings.TrimLeft(prefix, "/")
  198. marker = strings.TrimLeft(marker, "/")
  199. if prefix == "" {
  200. return "", "", marker
  201. }
  202. if marker == "" {
  203. alignedDir, alignedPrefix = toDirAndName(prefix)
  204. return
  205. }
  206. if !strings.HasPrefix(marker, prefix) {
  207. // something wrong
  208. return "", prefix, marker
  209. }
  210. if strings.HasPrefix(marker, prefix+"/") {
  211. alignedDir = prefix
  212. alignedPrefix = ""
  213. alignedMarker = marker[len(alignedDir)+1:]
  214. return
  215. }
  216. alignedDir, alignedPrefix = toDirAndName(prefix)
  217. if alignedDir != "" {
  218. alignedMarker = marker[len(alignedDir)+1:]
  219. } else {
  220. alignedMarker = marker
  221. }
  222. return
  223. }
  224. func toDirAndName(dirAndName string) (dir, name string) {
  225. sepIndex := strings.LastIndex(dirAndName, "/")
  226. if sepIndex >= 0 {
  227. dir, name = dirAndName[0:sepIndex], dirAndName[sepIndex+1:]
  228. } else {
  229. name = dirAndName
  230. }
  231. return
  232. }
  233. func toParentAndDescendants(dirAndName string) (dir, name string) {
  234. sepIndex := strings.Index(dirAndName, "/")
  235. if sepIndex >= 0 {
  236. dir, name = dirAndName[0:sepIndex], dirAndName[sepIndex+1:]
  237. } else {
  238. name = dirAndName
  239. }
  240. return
  241. }
  242. func (s3a *S3ApiServer) doListFilerEntries(client filer_pb.SeaweedFilerClient, dir, prefix string, cursor *ListingCursor, marker, delimiter string, inclusiveStartFrom bool, eachEntryFn func(dir string, entry *filer_pb.Entry)) (nextMarker string, err error) {
  243. // invariants
  244. // prefix and marker should be under dir, marker may contain "/"
  245. // maxKeys should be updated for each recursion
  246. if prefix == "/" && delimiter == "/" {
  247. return
  248. }
  249. if cursor.maxKeys <= 0 {
  250. return
  251. }
  252. if strings.Contains(marker, "/") {
  253. subDir, subMarker := toParentAndDescendants(marker)
  254. // println("doListFilerEntries dir", dir+"/"+subDir, "subMarker", subMarker)
  255. subNextMarker, subErr := s3a.doListFilerEntries(client, dir+"/"+subDir, "", cursor, subMarker, delimiter, false, eachEntryFn)
  256. if subErr != nil {
  257. err = subErr
  258. return
  259. }
  260. nextMarker = subDir + "/" + subNextMarker
  261. // finished processing this sub directory
  262. marker = subDir
  263. }
  264. if cursor.isTruncated {
  265. return
  266. }
  267. // now marker is also a direct child of dir
  268. request := &filer_pb.ListEntriesRequest{
  269. Directory: dir,
  270. Prefix: prefix,
  271. Limit: uint32(cursor.maxKeys + 2), // bucket root directory needs to skip additional s3_constants.MultipartUploadsFolder folder
  272. StartFromFileName: marker,
  273. InclusiveStartFrom: inclusiveStartFrom,
  274. }
  275. ctx, cancel := context.WithCancel(context.Background())
  276. defer cancel()
  277. stream, listErr := client.ListEntries(ctx, request)
  278. if listErr != nil {
  279. err = fmt.Errorf("list entires %+v: %v", request, listErr)
  280. return
  281. }
  282. for {
  283. resp, recvErr := stream.Recv()
  284. if recvErr != nil {
  285. if recvErr == io.EOF {
  286. break
  287. } else {
  288. err = fmt.Errorf("iterating entires %+v: %v", request, recvErr)
  289. return
  290. }
  291. }
  292. if cursor.maxKeys <= 0 {
  293. cursor.isTruncated = true
  294. return
  295. }
  296. entry := resp.Entry
  297. nextMarker = entry.Name
  298. if entry.IsDirectory {
  299. // println("ListEntries", dir, "dir:", entry.Name)
  300. if entry.Name == s3_constants.MultipartUploadsFolder { // FIXME no need to apply to all directories. this extra also affects maxKeys
  301. continue
  302. }
  303. if delimiter != "/" {
  304. eachEntryFn(dir, entry)
  305. subNextMarker, subErr := s3a.doListFilerEntries(client, dir+"/"+entry.Name, "", cursor, "", delimiter, false, eachEntryFn)
  306. if subErr != nil {
  307. err = fmt.Errorf("doListFilerEntries2: %v", subErr)
  308. return
  309. }
  310. // println("doListFilerEntries2 dir", dir+"/"+entry.Name, "subNextMarker", subNextMarker)
  311. nextMarker = entry.Name + "/" + subNextMarker
  312. if cursor.isTruncated {
  313. return
  314. }
  315. // println("doListFilerEntries2 nextMarker", nextMarker)
  316. } else {
  317. var isEmpty bool
  318. if !s3a.option.AllowEmptyFolder && !entry.IsDirectoryKeyObject() {
  319. if isEmpty, err = s3a.ensureDirectoryAllEmpty(client, dir, entry.Name); err != nil {
  320. glog.Errorf("check empty folder %s: %v", dir, err)
  321. }
  322. }
  323. if !isEmpty {
  324. eachEntryFn(dir, entry)
  325. }
  326. }
  327. } else {
  328. eachEntryFn(dir, entry)
  329. // println("ListEntries", dir, "file:", entry.Name, "maxKeys", cursor.maxKeys)
  330. }
  331. }
  332. return
  333. }
  334. func getListObjectsV2Args(values url.Values) (prefix, token, startAfter, delimiter string, fetchOwner bool, maxkeys int) {
  335. prefix = values.Get("prefix")
  336. token = values.Get("continuation-token")
  337. startAfter = values.Get("start-after")
  338. delimiter = values.Get("delimiter")
  339. if values.Get("max-keys") != "" {
  340. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  341. } else {
  342. maxkeys = maxObjectListSizeLimit
  343. }
  344. fetchOwner = values.Get("fetch-owner") == "true"
  345. return
  346. }
  347. func getListObjectsV1Args(values url.Values) (prefix, marker, delimiter string, maxkeys int) {
  348. prefix = values.Get("prefix")
  349. marker = values.Get("marker")
  350. delimiter = values.Get("delimiter")
  351. if values.Get("max-keys") != "" {
  352. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  353. } else {
  354. maxkeys = maxObjectListSizeLimit
  355. }
  356. return
  357. }
  358. func (s3a *S3ApiServer) ensureDirectoryAllEmpty(filerClient filer_pb.SeaweedFilerClient, parentDir, name string) (isEmpty bool, err error) {
  359. // println("+ ensureDirectoryAllEmpty", dir, name)
  360. glog.V(4).Infof("+ isEmpty %s/%s", parentDir, name)
  361. defer glog.V(4).Infof("- isEmpty %s/%s %v", parentDir, name, isEmpty)
  362. var fileCounter int
  363. var subDirs []string
  364. currentDir := parentDir + "/" + name
  365. var startFrom string
  366. var isExhausted bool
  367. var foundEntry bool
  368. cutOffTimeAtSec := time.Now().Unix() + cutoffTimeNewEmptyDir
  369. for fileCounter == 0 && !isExhausted && err == nil {
  370. err = filer_pb.SeaweedList(filerClient, currentDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  371. foundEntry = true
  372. if entry.IsDirectory {
  373. if entry.Attributes != nil && cutOffTimeAtSec >= entry.Attributes.GetCrtime() {
  374. fileCounter++
  375. } else {
  376. subDirs = append(subDirs, entry.Name)
  377. }
  378. } else {
  379. fileCounter++
  380. }
  381. startFrom = entry.Name
  382. isExhausted = isExhausted || isLast
  383. glog.V(4).Infof(" * %s/%s isLast: %t", currentDir, startFrom, isLast)
  384. return nil
  385. }, startFrom, false, 8)
  386. if !foundEntry {
  387. break
  388. }
  389. }
  390. if err != nil {
  391. return false, err
  392. }
  393. if fileCounter > 0 {
  394. return false, nil
  395. }
  396. for _, subDir := range subDirs {
  397. isSubEmpty, subErr := s3a.ensureDirectoryAllEmpty(filerClient, currentDir, subDir)
  398. if subErr != nil {
  399. return false, subErr
  400. }
  401. if !isSubEmpty {
  402. return false, nil
  403. }
  404. }
  405. glog.V(1).Infof("deleting empty folder %s", currentDir)
  406. if err = doDeleteEntry(filerClient, parentDir, name, true, true); err != nil {
  407. return
  408. }
  409. return true, nil
  410. }