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.

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