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.

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