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.

464 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
5 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 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. _, errCode := s3a.checkAccessForReadBucket(r, bucket, s3_constants.PermissionRead)
  38. if errCode != s3err.ErrNone {
  39. s3err.WriteErrorResponse(w, r, errCode)
  40. return
  41. }
  42. originalPrefix, continuationToken, startAfter, delimiter, _, maxKeys := getListObjectsV2Args(r.URL.Query())
  43. if maxKeys < 0 {
  44. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxKeys)
  45. return
  46. }
  47. if delimiter != "" && delimiter != "/" {
  48. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  49. return
  50. }
  51. marker := continuationToken
  52. if continuationToken == "" {
  53. marker = startAfter
  54. }
  55. response, err := s3a.listFilerEntries(bucket, originalPrefix, maxKeys, marker, delimiter)
  56. if err != nil {
  57. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  58. return
  59. }
  60. if len(response.Contents) == 0 {
  61. if exists, existErr := s3a.exists(s3a.option.BucketsPath, bucket, true); existErr == nil && !exists {
  62. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  63. return
  64. }
  65. }
  66. responseV2 := &ListBucketResultV2{
  67. XMLName: response.XMLName,
  68. Name: response.Name,
  69. CommonPrefixes: response.CommonPrefixes,
  70. Contents: response.Contents,
  71. ContinuationToken: continuationToken,
  72. Delimiter: response.Delimiter,
  73. IsTruncated: response.IsTruncated,
  74. KeyCount: len(response.Contents) + len(response.CommonPrefixes),
  75. MaxKeys: response.MaxKeys,
  76. NextContinuationToken: response.NextMarker,
  77. Prefix: response.Prefix,
  78. StartAfter: startAfter,
  79. }
  80. writeSuccessResponseXML(w, r, responseV2)
  81. }
  82. func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
  83. // https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html
  84. // collect parameters
  85. bucket, _ := s3_constants.GetBucketAndObject(r)
  86. glog.V(3).Infof("ListObjectsV1Handler %s", bucket)
  87. _, errCode := s3a.checkAccessForReadBucket(r, bucket, s3_constants.PermissionRead)
  88. if errCode != s3err.ErrNone {
  89. s3err.WriteErrorResponse(w, r, errCode)
  90. return
  91. }
  92. originalPrefix, marker, delimiter, maxKeys := getListObjectsV1Args(r.URL.Query())
  93. if maxKeys < 0 {
  94. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxKeys)
  95. return
  96. }
  97. if delimiter != "" && delimiter != "/" {
  98. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  99. return
  100. }
  101. response, err := s3a.listFilerEntries(bucket, originalPrefix, maxKeys, marker, delimiter)
  102. if err != nil {
  103. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  104. return
  105. }
  106. if len(response.Contents) == 0 {
  107. if exists, existErr := s3a.exists(s3a.option.BucketsPath, bucket, true); existErr == nil && !exists {
  108. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  109. return
  110. }
  111. }
  112. writeSuccessResponseXML(w, r, response)
  113. }
  114. func (s3a *S3ApiServer) listFilerEntries(bucket string, originalPrefix string, maxKeys int, originalMarker string, delimiter string) (response ListBucketResult, err error) {
  115. // convert full path prefix into directory name and prefix for entry name
  116. requestDir, prefix, marker := normalizePrefixMarker(originalPrefix, originalMarker)
  117. bucketPrefix := fmt.Sprintf("%s/%s/", s3a.option.BucketsPath, bucket)
  118. reqDir := bucketPrefix[:len(bucketPrefix)-1]
  119. if requestDir != "" {
  120. reqDir = fmt.Sprintf("%s%s", bucketPrefix, requestDir)
  121. }
  122. var contents []ListEntry
  123. var commonPrefixes []PrefixEntry
  124. var doErr error
  125. var nextMarker string
  126. cursor := &ListingCursor{
  127. maxKeys: maxKeys,
  128. }
  129. // check filer
  130. err = s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  131. nextMarker, doErr = s3a.doListFilerEntries(client, reqDir, prefix, cursor, marker, delimiter, false, func(dir string, entry *filer_pb.Entry) {
  132. if entry.IsDirectory {
  133. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
  134. if delimiter == "/" { // A response can contain CommonPrefixes only if you specify a delimiter.
  135. commonPrefixes = append(commonPrefixes, PrefixEntry{
  136. Prefix: fmt.Sprintf("%s/%s/", dir, entry.Name)[len(bucketPrefix):],
  137. })
  138. //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.
  139. cursor.maxKeys--
  140. } else if entry.IsDirectoryKeyObject() {
  141. contents = append(contents, ListEntry{
  142. Key: fmt.Sprintf("%s/%s/", dir, entry.Name)[len(bucketPrefix):],
  143. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  144. ETag: "\"" + filer.ETag(entry) + "\"",
  145. Owner: CanonicalUser{
  146. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  147. DisplayName: entry.Attributes.UserName,
  148. },
  149. StorageClass: "STANDARD",
  150. })
  151. cursor.maxKeys--
  152. }
  153. } else {
  154. storageClass := "STANDARD"
  155. if v, ok := entry.Extended[s3_constants.AmzStorageClass]; ok {
  156. storageClass = string(v)
  157. }
  158. contents = append(contents, ListEntry{
  159. Key: fmt.Sprintf("%s/%s", dir, entry.Name)[len(bucketPrefix):],
  160. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  161. ETag: "\"" + filer.ETag(entry) + "\"",
  162. Size: int64(filer.FileSize(entry)),
  163. Owner: CanonicalUser{
  164. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  165. DisplayName: entry.Attributes.UserName,
  166. },
  167. StorageClass: StorageClass(storageClass),
  168. })
  169. cursor.maxKeys--
  170. }
  171. })
  172. if doErr != nil {
  173. return doErr
  174. }
  175. if !cursor.isTruncated {
  176. nextMarker = ""
  177. } else {
  178. if requestDir != "" {
  179. nextMarker = requestDir + "/" + 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.maxKeys <= 0 {
  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. for fileCounter == 0 && !isExhausted && err == nil {
  378. err = filer_pb.SeaweedList(filerClient, currentDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  379. foundEntry = true
  380. if entry.IsDirectory {
  381. subDirs = append(subDirs, entry.Name)
  382. } else {
  383. fileCounter++
  384. }
  385. startFrom = entry.Name
  386. isExhausted = isExhausted || isLast
  387. glog.V(4).Infof(" * %s/%s isLast: %t", currentDir, startFrom, isLast)
  388. return nil
  389. }, startFrom, false, 8)
  390. if !foundEntry {
  391. break
  392. }
  393. }
  394. if err != nil {
  395. return false, err
  396. }
  397. if fileCounter > 0 {
  398. return false, nil
  399. }
  400. for _, subDir := range subDirs {
  401. isSubEmpty, subErr := s3a.ensureDirectoryAllEmpty(filerClient, currentDir, subDir)
  402. if subErr != nil {
  403. return false, subErr
  404. }
  405. if !isSubEmpty {
  406. return false, nil
  407. }
  408. }
  409. glog.V(1).Infof("deleting empty folder %s", currentDir)
  410. if err = doDeleteEntry(filerClient, parentDir, name, true, true); err != nil {
  411. return
  412. }
  413. return true, nil
  414. }