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.

525 lines
16 KiB

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