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.

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