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.

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