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.

467 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. for {
  123. empty := true
  124. nextMarker, doErr = s3a.doListFilerEntries(client, reqDir, prefix, cursor, marker, delimiter, false, func(dir string, entry *filer_pb.Entry) {
  125. empty = false
  126. if entry.IsDirectory {
  127. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
  128. if delimiter == "/" { // A response can contain CommonPrefixes only if you specify a delimiter.
  129. commonPrefixes = append(commonPrefixes, PrefixEntry{
  130. Prefix: fmt.Sprintf("%s/%s/", dir, entry.Name)[len(bucketPrefix):],
  131. })
  132. //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.
  133. cursor.maxKeys--
  134. } else if entry.IsDirectoryKeyObject() {
  135. contents = append(contents, ListEntry{
  136. Key: fmt.Sprintf("%s/%s/", dir, entry.Name)[len(bucketPrefix):],
  137. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  138. ETag: "\"" + filer.ETag(entry) + "\"",
  139. Owner: CanonicalUser{
  140. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  141. DisplayName: entry.Attributes.UserName,
  142. },
  143. StorageClass: "STANDARD",
  144. })
  145. cursor.maxKeys--
  146. }
  147. } else {
  148. storageClass := "STANDARD"
  149. if v, ok := entry.Extended[s3_constants.AmzStorageClass]; ok {
  150. storageClass = string(v)
  151. }
  152. contents = append(contents, ListEntry{
  153. Key: fmt.Sprintf("%s/%s", dir, entry.Name)[len(bucketPrefix):],
  154. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  155. ETag: "\"" + filer.ETag(entry) + "\"",
  156. Size: int64(filer.FileSize(entry)),
  157. Owner: CanonicalUser{
  158. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  159. DisplayName: entry.Attributes.UserName,
  160. },
  161. StorageClass: StorageClass(storageClass),
  162. })
  163. cursor.maxKeys--
  164. }
  165. })
  166. if doErr != nil {
  167. return doErr
  168. }
  169. if cursor.isTruncated {
  170. if requestDir != "" {
  171. nextMarker = requestDir + "/" + nextMarker
  172. }
  173. break
  174. } else if empty {
  175. nextMarker = ""
  176. break
  177. } else {
  178. // start next loop
  179. marker = nextMarker
  180. }
  181. }
  182. response = ListBucketResult{
  183. Name: bucket,
  184. Prefix: originalPrefix,
  185. Marker: originalMarker,
  186. NextMarker: nextMarker,
  187. MaxKeys: maxKeys,
  188. Delimiter: delimiter,
  189. IsTruncated: cursor.isTruncated,
  190. Contents: contents,
  191. CommonPrefixes: commonPrefixes,
  192. }
  193. return nil
  194. })
  195. return
  196. }
  197. type ListingCursor struct {
  198. maxKeys int
  199. isTruncated bool
  200. }
  201. // the prefix and marker may be in different directories
  202. // normalizePrefixMarker ensures the prefix and marker both starts from the same directory
  203. func normalizePrefixMarker(prefix, marker string) (alignedDir, alignedPrefix, alignedMarker string) {
  204. // alignedDir should not end with "/"
  205. // alignedDir, alignedPrefix, alignedMarker should only have "/" in middle
  206. prefix = strings.TrimLeft(prefix, "/")
  207. marker = strings.TrimLeft(marker, "/")
  208. if prefix == "" {
  209. return "", "", marker
  210. }
  211. if marker == "" {
  212. alignedDir, alignedPrefix = toDirAndName(prefix)
  213. return
  214. }
  215. if !strings.HasPrefix(marker, prefix) {
  216. // something wrong
  217. return "", prefix, marker
  218. }
  219. if strings.HasPrefix(marker, prefix+"/") {
  220. alignedDir = prefix
  221. alignedPrefix = ""
  222. alignedMarker = marker[len(alignedDir)+1:]
  223. return
  224. }
  225. alignedDir, alignedPrefix = toDirAndName(prefix)
  226. if alignedDir != "" {
  227. alignedMarker = marker[len(alignedDir)+1:]
  228. } else {
  229. alignedMarker = marker
  230. }
  231. return
  232. }
  233. func toDirAndName(dirAndName string) (dir, name string) {
  234. sepIndex := strings.LastIndex(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 toParentAndDescendants(dirAndName string) (dir, name string) {
  243. sepIndex := strings.Index(dirAndName, "/")
  244. if sepIndex >= 0 {
  245. dir, name = dirAndName[0:sepIndex], dirAndName[sepIndex+1:]
  246. } else {
  247. name = dirAndName
  248. }
  249. return
  250. }
  251. 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) {
  252. // invariants
  253. // prefix and marker should be under dir, marker may contain "/"
  254. // maxKeys should be updated for each recursion
  255. if prefix == "/" && delimiter == "/" {
  256. return
  257. }
  258. if cursor.maxKeys <= 0 {
  259. return
  260. }
  261. if strings.Contains(marker, "/") {
  262. subDir, subMarker := toParentAndDescendants(marker)
  263. // println("doListFilerEntries dir", dir+"/"+subDir, "subMarker", subMarker)
  264. subNextMarker, subErr := s3a.doListFilerEntries(client, dir+"/"+subDir, "", cursor, subMarker, delimiter, false, eachEntryFn)
  265. if subErr != nil {
  266. err = subErr
  267. return
  268. }
  269. nextMarker = subDir + "/" + subNextMarker
  270. // finished processing this sub directory
  271. marker = subDir
  272. }
  273. if cursor.isTruncated {
  274. return
  275. }
  276. // now marker is also a direct child of dir
  277. request := &filer_pb.ListEntriesRequest{
  278. Directory: dir,
  279. Prefix: prefix,
  280. Limit: uint32(cursor.maxKeys + 2), // bucket root directory needs to skip additional s3_constants.MultipartUploadsFolder folder
  281. StartFromFileName: marker,
  282. InclusiveStartFrom: inclusiveStartFrom,
  283. }
  284. ctx, cancel := context.WithCancel(context.Background())
  285. defer cancel()
  286. stream, listErr := client.ListEntries(ctx, request)
  287. if listErr != nil {
  288. err = fmt.Errorf("list entires %+v: %v", request, listErr)
  289. return
  290. }
  291. for {
  292. resp, recvErr := stream.Recv()
  293. if recvErr != nil {
  294. if recvErr == io.EOF {
  295. break
  296. } else {
  297. err = fmt.Errorf("iterating entires %+v: %v", request, recvErr)
  298. return
  299. }
  300. }
  301. if cursor.maxKeys <= 0 {
  302. cursor.isTruncated = true
  303. return
  304. }
  305. entry := resp.Entry
  306. nextMarker = entry.Name
  307. if entry.IsDirectory {
  308. // println("ListEntries", dir, "dir:", entry.Name)
  309. if entry.Name == s3_constants.MultipartUploadsFolder { // FIXME no need to apply to all directories. this extra also affects maxKeys
  310. continue
  311. }
  312. if delimiter != "/" {
  313. eachEntryFn(dir, entry)
  314. subNextMarker, subErr := s3a.doListFilerEntries(client, dir+"/"+entry.Name, "", cursor, "", delimiter, false, eachEntryFn)
  315. if subErr != nil {
  316. err = fmt.Errorf("doListFilerEntries2: %v", subErr)
  317. return
  318. }
  319. // println("doListFilerEntries2 dir", dir+"/"+entry.Name, "subNextMarker", subNextMarker)
  320. nextMarker = entry.Name + "/" + subNextMarker
  321. if cursor.isTruncated {
  322. return
  323. }
  324. // println("doListFilerEntries2 nextMarker", nextMarker)
  325. } else {
  326. var isEmpty bool
  327. if !s3a.option.AllowEmptyFolder && !entry.IsDirectoryKeyObject() {
  328. if isEmpty, err = s3a.ensureDirectoryAllEmpty(client, dir, entry.Name); err != nil {
  329. glog.Errorf("check empty folder %s: %v", dir, err)
  330. }
  331. }
  332. if !isEmpty {
  333. eachEntryFn(dir, entry)
  334. }
  335. }
  336. } else {
  337. eachEntryFn(dir, entry)
  338. // println("ListEntries", dir, "file:", entry.Name, "maxKeys", cursor.maxKeys)
  339. }
  340. }
  341. return
  342. }
  343. func getListObjectsV2Args(values url.Values) (prefix, token, startAfter, delimiter string, fetchOwner bool, maxkeys int) {
  344. prefix = values.Get("prefix")
  345. token = values.Get("continuation-token")
  346. startAfter = values.Get("start-after")
  347. delimiter = values.Get("delimiter")
  348. if values.Get("max-keys") != "" {
  349. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  350. } else {
  351. maxkeys = maxObjectListSizeLimit
  352. }
  353. fetchOwner = values.Get("fetch-owner") == "true"
  354. return
  355. }
  356. func getListObjectsV1Args(values url.Values) (prefix, marker, delimiter string, maxkeys int) {
  357. prefix = values.Get("prefix")
  358. marker = values.Get("marker")
  359. delimiter = values.Get("delimiter")
  360. if values.Get("max-keys") != "" {
  361. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  362. } else {
  363. maxkeys = maxObjectListSizeLimit
  364. }
  365. return
  366. }
  367. func (s3a *S3ApiServer) ensureDirectoryAllEmpty(filerClient filer_pb.SeaweedFilerClient, parentDir, name string) (isEmpty bool, err error) {
  368. // println("+ ensureDirectoryAllEmpty", dir, name)
  369. glog.V(4).Infof("+ isEmpty %s/%s", parentDir, name)
  370. defer glog.V(4).Infof("- isEmpty %s/%s %v", parentDir, name, isEmpty)
  371. var fileCounter int
  372. var subDirs []string
  373. currentDir := parentDir + "/" + name
  374. var startFrom string
  375. var isExhausted bool
  376. var foundEntry bool
  377. cutOffTimeAtSec := time.Now().Unix() + cutoffTimeNewEmptyDir
  378. for fileCounter == 0 && !isExhausted && err == nil {
  379. err = filer_pb.SeaweedList(filerClient, currentDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  380. foundEntry = true
  381. if entry.IsDirectory {
  382. if entry.Attributes != nil && cutOffTimeAtSec >= entry.Attributes.GetCrtime() {
  383. fileCounter++
  384. } else {
  385. subDirs = append(subDirs, entry.Name)
  386. }
  387. } else {
  388. fileCounter++
  389. }
  390. startFrom = entry.Name
  391. isExhausted = isExhausted || isLast
  392. glog.V(4).Infof(" * %s/%s isLast: %t", currentDir, startFrom, isLast)
  393. return nil
  394. }, startFrom, false, 8)
  395. if !foundEntry {
  396. break
  397. }
  398. }
  399. if err != nil {
  400. return false, err
  401. }
  402. if fileCounter > 0 {
  403. return false, nil
  404. }
  405. for _, subDir := range subDirs {
  406. isSubEmpty, subErr := s3a.ensureDirectoryAllEmpty(filerClient, currentDir, subDir)
  407. if subErr != nil {
  408. return false, subErr
  409. }
  410. if !isSubEmpty {
  411. return false, nil
  412. }
  413. }
  414. glog.V(1).Infof("deleting empty folder %s", currentDir)
  415. if err = doDeleteEntry(filerClient, parentDir, name, true, true); err != nil {
  416. return
  417. }
  418. return true, nil
  419. }