s3api_bucket_handlers.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. package s3api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/xml"
  6. "errors"
  7. "fmt"
  8. "github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
  9. "github.com/seaweedfs/seaweedfs/weed/s3api/s3bucket"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "math"
  12. "net/http"
  13. "strings"
  14. "time"
  15. "github.com/seaweedfs/seaweedfs/weed/filer"
  16. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  17. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  18. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  19. "github.com/aws/aws-sdk-go/aws"
  20. "github.com/aws/aws-sdk-go/service/s3"
  21. "github.com/seaweedfs/seaweedfs/weed/glog"
  22. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  23. )
  24. type ListAllMyBucketsResult struct {
  25. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
  26. Owner *s3.Owner
  27. Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
  28. }
  29. func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
  30. glog.V(3).Infof("ListBucketsHandler")
  31. var identity *Identity
  32. var s3Err s3err.ErrorCode
  33. if s3a.iam.isEnabled() {
  34. identity, s3Err = s3a.iam.authUser(r)
  35. if s3Err != s3err.ErrNone {
  36. s3err.WriteErrorResponse(w, r, s3Err)
  37. return
  38. }
  39. }
  40. var response ListAllMyBucketsResult
  41. entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
  42. if err != nil {
  43. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  44. return
  45. }
  46. identityId := r.Header.Get(s3_constants.AmzIdentityId)
  47. var buckets []*s3.Bucket
  48. for _, entry := range entries {
  49. if entry.IsDirectory {
  50. if identity != nil && !identity.canDo(s3_constants.ACTION_LIST, entry.Name, "") {
  51. continue
  52. }
  53. buckets = append(buckets, &s3.Bucket{
  54. Name: aws.String(entry.Name),
  55. CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
  56. })
  57. }
  58. }
  59. response = ListAllMyBucketsResult{
  60. Owner: &s3.Owner{
  61. ID: aws.String(identityId),
  62. DisplayName: aws.String(identityId),
  63. },
  64. Buckets: buckets,
  65. }
  66. writeSuccessResponseXML(w, r, response)
  67. }
  68. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  69. bucket, _ := s3_constants.GetBucketAndObject(r)
  70. glog.V(3).Infof("PutBucketHandler %s", bucket)
  71. // validate the bucket name
  72. err := s3bucket.VerifyS3BucketName(bucket)
  73. if err != nil {
  74. glog.Errorf("put invalid bucket name: %v %v", bucket, err)
  75. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidBucketName)
  76. return
  77. }
  78. // avoid duplicated buckets
  79. errCode := s3err.ErrNone
  80. if err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  81. if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
  82. IncludeEcVolumes: true,
  83. IncludeNormalVolumes: true,
  84. }); err != nil {
  85. glog.Errorf("list collection: %v", err)
  86. return fmt.Errorf("list collections: %v", err)
  87. } else {
  88. for _, c := range resp.Collections {
  89. if s3a.getCollectionName(bucket) == c.Name {
  90. errCode = s3err.ErrBucketAlreadyExists
  91. break
  92. }
  93. }
  94. }
  95. return nil
  96. }); err != nil {
  97. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  98. return
  99. }
  100. if exist, err := s3a.exists(s3a.option.BucketsPath, bucket, true); err == nil && exist {
  101. errCode = s3err.ErrBucketAlreadyExists
  102. }
  103. if errCode != s3err.ErrNone {
  104. s3err.WriteErrorResponse(w, r, errCode)
  105. return
  106. }
  107. fn := func(entry *filer_pb.Entry) {
  108. if identityId := r.Header.Get(s3_constants.AmzIdentityId); identityId != "" {
  109. if entry.Extended == nil {
  110. entry.Extended = make(map[string][]byte)
  111. }
  112. entry.Extended[s3_constants.AmzIdentityId] = []byte(identityId)
  113. }
  114. }
  115. // create the folder for bucket, but lazily create actual collection
  116. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
  117. glog.Errorf("PutBucketHandler mkdir: %v", err)
  118. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  119. return
  120. }
  121. w.Header().Set("Location", "/"+bucket)
  122. writeSuccessResponseEmpty(w, r)
  123. }
  124. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  125. bucket, _ := s3_constants.GetBucketAndObject(r)
  126. glog.V(3).Infof("DeleteBucketHandler %s", bucket)
  127. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  128. s3err.WriteErrorResponse(w, r, err)
  129. return
  130. }
  131. err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  132. if !s3a.option.AllowDeleteBucketNotEmpty {
  133. entries, _, err := s3a.list(s3a.option.BucketsPath+"/"+bucket, "", "", false, 2)
  134. if err != nil {
  135. return fmt.Errorf("failed to list bucket %s: %v", bucket, err)
  136. }
  137. for _, entry := range entries {
  138. if entry.Name != s3_constants.MultipartUploadsFolder {
  139. return errors.New(s3err.GetAPIError(s3err.ErrBucketNotEmpty).Code)
  140. }
  141. }
  142. }
  143. // delete collection
  144. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  145. Collection: s3a.getCollectionName(bucket),
  146. }
  147. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  148. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  149. return fmt.Errorf("delete collection %s: %v", bucket, err)
  150. }
  151. return nil
  152. })
  153. if err != nil {
  154. s3ErrorCode := s3err.ErrInternalError
  155. if err.Error() == s3err.GetAPIError(s3err.ErrBucketNotEmpty).Code {
  156. s3ErrorCode = s3err.ErrBucketNotEmpty
  157. }
  158. s3err.WriteErrorResponse(w, r, s3ErrorCode)
  159. return
  160. }
  161. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  162. if err != nil {
  163. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  164. return
  165. }
  166. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  167. }
  168. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  169. bucket, _ := s3_constants.GetBucketAndObject(r)
  170. glog.V(3).Infof("HeadBucketHandler %s", bucket)
  171. if entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket); entry == nil || err == filer_pb.ErrNotFound {
  172. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  173. return
  174. }
  175. writeSuccessResponseEmpty(w, r)
  176. }
  177. func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
  178. entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  179. if entry == nil || err == filer_pb.ErrNotFound {
  180. return s3err.ErrNoSuchBucket
  181. }
  182. if !s3a.hasAccess(r, entry) {
  183. return s3err.ErrAccessDenied
  184. }
  185. return s3err.ErrNone
  186. }
  187. func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
  188. isAdmin := r.Header.Get(s3_constants.AmzIsAdmin) != ""
  189. if isAdmin {
  190. return true
  191. }
  192. if entry.Extended == nil {
  193. return true
  194. }
  195. identityId := r.Header.Get(s3_constants.AmzIdentityId)
  196. if id, ok := entry.Extended[s3_constants.AmzIdentityId]; ok {
  197. if identityId != string(id) {
  198. return false
  199. }
  200. }
  201. return true
  202. }
  203. // GetBucketAclHandler Get Bucket ACL
  204. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html
  205. func (s3a *S3ApiServer) GetBucketAclHandler(w http.ResponseWriter, r *http.Request) {
  206. // collect parameters
  207. bucket, _ := s3_constants.GetBucketAndObject(r)
  208. glog.V(3).Infof("GetBucketAclHandler %s", bucket)
  209. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  210. s3err.WriteErrorResponse(w, r, err)
  211. return
  212. }
  213. amzAccountId := r.Header.Get(s3_constants.AmzAccountId)
  214. amzDisplayName := s3a.iam.GetAccountNameById(amzAccountId)
  215. response := AccessControlPolicy{
  216. Owner: CanonicalUser{
  217. ID: amzAccountId,
  218. DisplayName: amzDisplayName,
  219. },
  220. }
  221. response.AccessControlList.Grant = append(response.AccessControlList.Grant, Grant{
  222. Grantee: Grantee{
  223. ID: amzAccountId,
  224. DisplayName: amzDisplayName,
  225. Type: "CanonicalUser",
  226. XMLXSI: "CanonicalUser",
  227. XMLNS: "http://www.w3.org/2001/XMLSchema-instance"},
  228. Permission: s3.PermissionFullControl,
  229. })
  230. writeSuccessResponseXML(w, r, response)
  231. }
  232. // PutBucketAclHandler Put bucket ACL only responds success if the ACL is private.
  233. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketAcl.html //
  234. func (s3a *S3ApiServer) PutBucketAclHandler(w http.ResponseWriter, r *http.Request) {
  235. // collect parameters
  236. bucket, _ := s3_constants.GetBucketAndObject(r)
  237. glog.V(3).Infof("PutBucketAclHandler %s", bucket)
  238. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  239. s3err.WriteErrorResponse(w, r, err)
  240. return
  241. }
  242. cannedAcl := r.Header.Get(s3_constants.AmzCannedAcl)
  243. switch {
  244. case cannedAcl == "":
  245. acl := &s3.AccessControlPolicy{}
  246. if err := xmlDecoder(r.Body, acl, r.ContentLength); err != nil {
  247. glog.Errorf("PutBucketAclHandler: %s", err)
  248. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  249. return
  250. }
  251. if len(acl.Grants) == 1 && acl.Grants[0].Permission != nil && *acl.Grants[0].Permission == s3_constants.PermissionFullControl {
  252. writeSuccessResponseEmpty(w, r)
  253. return
  254. }
  255. case cannedAcl == s3_constants.CannedAclPrivate:
  256. writeSuccessResponseEmpty(w, r)
  257. return
  258. }
  259. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  260. }
  261. // GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration
  262. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html
  263. func (s3a *S3ApiServer) GetBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  264. // collect parameters
  265. bucket, _ := s3_constants.GetBucketAndObject(r)
  266. glog.V(3).Infof("GetBucketLifecycleConfigurationHandler %s", bucket)
  267. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  268. s3err.WriteErrorResponse(w, r, err)
  269. return
  270. }
  271. fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
  272. if err != nil {
  273. glog.Errorf("GetBucketLifecycleConfigurationHandler: %s", err)
  274. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  275. return
  276. }
  277. ttls := fc.GetCollectionTtls(s3a.getCollectionName(bucket))
  278. if len(ttls) == 0 {
  279. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchLifecycleConfiguration)
  280. return
  281. }
  282. response := Lifecycle{}
  283. for locationPrefix, internalTtl := range ttls {
  284. ttl, _ := needle.ReadTTL(internalTtl)
  285. days := int(ttl.Minutes() / 60 / 24)
  286. if days == 0 {
  287. continue
  288. }
  289. prefix, found := strings.CutPrefix(locationPrefix, fmt.Sprintf("%s/%s/", s3a.option.BucketsPath, bucket))
  290. if !found {
  291. continue
  292. }
  293. response.Rules = append(response.Rules, Rule{
  294. ID: prefix,
  295. Status: Enabled,
  296. Prefix: Prefix{val: prefix, set: true},
  297. Expiration: Expiration{Days: days, set: true},
  298. })
  299. }
  300. writeSuccessResponseXML(w, r, response)
  301. }
  302. // PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration
  303. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketLifecycleConfiguration.html
  304. func (s3a *S3ApiServer) PutBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  305. // collect parameters
  306. bucket, _ := s3_constants.GetBucketAndObject(r)
  307. glog.V(3).Infof("PutBucketLifecycleConfigurationHandler %s", bucket)
  308. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  309. s3err.WriteErrorResponse(w, r, err)
  310. return
  311. }
  312. lifeCycleConfig := Lifecycle{}
  313. if err := xmlDecoder(r.Body, &lifeCycleConfig, r.ContentLength); err != nil {
  314. glog.Warningf("PutBucketLifecycleConfigurationHandler xml decode: %s", err)
  315. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
  316. return
  317. }
  318. fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
  319. if err != nil {
  320. glog.Errorf("PutBucketLifecycleConfigurationHandler read filer config: %s", err)
  321. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  322. return
  323. }
  324. collectionName := s3a.getCollectionName(bucket)
  325. collectionTtls := fc.GetCollectionTtls(collectionName)
  326. changed := false
  327. for _, rule := range lifeCycleConfig.Rules {
  328. if rule.Status != Enabled {
  329. continue
  330. }
  331. var rulePrefix string
  332. switch {
  333. case rule.Filter.Prefix.set:
  334. rulePrefix = rule.Filter.Prefix.val
  335. case rule.Prefix.set:
  336. rulePrefix = rule.Prefix.val
  337. case !rule.Expiration.Date.IsZero() || rule.Transition.Days > 0 || !rule.Transition.Date.IsZero():
  338. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  339. return
  340. }
  341. if rule.Expiration.Days == 0 {
  342. continue
  343. }
  344. locConf := &filer_pb.FilerConf_PathConf{
  345. LocationPrefix: fmt.Sprintf("%s/%s/%s", s3a.option.BucketsPath, bucket, rulePrefix),
  346. Collection: collectionName,
  347. Ttl: fmt.Sprintf("%dd", rule.Expiration.Days),
  348. }
  349. if ttl, ok := collectionTtls[locConf.LocationPrefix]; ok && ttl == locConf.Ttl {
  350. continue
  351. }
  352. if err := fc.AddLocationConf(locConf); err != nil {
  353. glog.Errorf("PutBucketLifecycleConfigurationHandler add location config: %s", err)
  354. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  355. return
  356. }
  357. changed = true
  358. }
  359. if changed {
  360. var buf bytes.Buffer
  361. if err := fc.ToText(&buf); err != nil {
  362. glog.Errorf("PutBucketLifecycleConfigurationHandler save config to text: %s", err)
  363. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  364. }
  365. if err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  366. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf.Bytes())
  367. }); err != nil {
  368. glog.Errorf("PutBucketLifecycleConfigurationHandler save config inside filer: %s", err)
  369. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  370. return
  371. }
  372. }
  373. writeSuccessResponseEmpty(w, r)
  374. }
  375. // DeleteBucketLifecycleHandler Delete Bucket Lifecycle
  376. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html
  377. func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) {
  378. // collect parameters
  379. bucket, _ := s3_constants.GetBucketAndObject(r)
  380. glog.V(3).Infof("DeleteBucketLifecycleHandler %s", bucket)
  381. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  382. s3err.WriteErrorResponse(w, r, err)
  383. return
  384. }
  385. fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
  386. if err != nil {
  387. glog.Errorf("DeleteBucketLifecycleHandler read filer config: %s", err)
  388. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  389. return
  390. }
  391. collectionTtls := fc.GetCollectionTtls(s3a.getCollectionName(bucket))
  392. changed := false
  393. for prefix, ttl := range collectionTtls {
  394. bucketPrefix := fmt.Sprintf("%s/%s/", s3a.option.BucketsPath, bucket)
  395. if strings.HasPrefix(prefix, bucketPrefix) && strings.HasSuffix(ttl, "d") {
  396. fc.DeleteLocationConf(prefix)
  397. changed = true
  398. }
  399. }
  400. if changed {
  401. var buf bytes.Buffer
  402. if err := fc.ToText(&buf); err != nil {
  403. glog.Errorf("DeleteBucketLifecycleHandler save config to text: %s", err)
  404. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  405. }
  406. if err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  407. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf.Bytes())
  408. }); err != nil {
  409. glog.Errorf("DeleteBucketLifecycleHandler save config inside filer: %s", err)
  410. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  411. return
  412. }
  413. }
  414. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  415. }
  416. // GetBucketLocationHandler Get bucket location
  417. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html
  418. func (s3a *S3ApiServer) GetBucketLocationHandler(w http.ResponseWriter, r *http.Request) {
  419. writeSuccessResponseXML(w, r, LocationConstraint{})
  420. }
  421. // GetBucketRequestPaymentHandler Get bucket location
  422. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketRequestPayment.html
  423. func (s3a *S3ApiServer) GetBucketRequestPaymentHandler(w http.ResponseWriter, r *http.Request) {
  424. writeSuccessResponseXML(w, r, RequestPaymentConfiguration{Payer: "BucketOwner"})
  425. }
  426. // PutBucketOwnershipControls https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketOwnershipControls.html
  427. func (s3a *S3ApiServer) PutBucketOwnershipControls(w http.ResponseWriter, r *http.Request) {
  428. bucket, _ := s3_constants.GetBucketAndObject(r)
  429. glog.V(3).Infof("PutBucketOwnershipControls %s", bucket)
  430. errCode := s3a.checkAccessByOwnership(r, bucket)
  431. if errCode != s3err.ErrNone {
  432. s3err.WriteErrorResponse(w, r, errCode)
  433. return
  434. }
  435. if r.Body == nil || r.Body == http.NoBody {
  436. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  437. return
  438. }
  439. var v s3.OwnershipControls
  440. defer util.CloseRequest(r)
  441. err := xmlutil.UnmarshalXML(&v, xml.NewDecoder(r.Body), "")
  442. if err != nil {
  443. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  444. return
  445. }
  446. if len(v.Rules) != 1 {
  447. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  448. return
  449. }
  450. printOwnership := true
  451. ownership := *v.Rules[0].ObjectOwnership
  452. switch ownership {
  453. case s3_constants.OwnershipObjectWriter:
  454. case s3_constants.OwnershipBucketOwnerPreferred:
  455. case s3_constants.OwnershipBucketOwnerEnforced:
  456. printOwnership = false
  457. default:
  458. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  459. return
  460. }
  461. bucketEntry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  462. if err != nil {
  463. if err == filer_pb.ErrNotFound {
  464. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  465. return
  466. }
  467. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  468. return
  469. }
  470. oldOwnership, ok := bucketEntry.Extended[s3_constants.ExtOwnershipKey]
  471. if !ok || string(oldOwnership) != ownership {
  472. if bucketEntry.Extended == nil {
  473. bucketEntry.Extended = make(map[string][]byte)
  474. }
  475. bucketEntry.Extended[s3_constants.ExtOwnershipKey] = []byte(ownership)
  476. err = s3a.updateEntry(s3a.option.BucketsPath, bucketEntry)
  477. if err != nil {
  478. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  479. return
  480. }
  481. }
  482. if printOwnership {
  483. result := &s3.PutBucketOwnershipControlsInput{
  484. OwnershipControls: &v,
  485. }
  486. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, result)
  487. } else {
  488. writeSuccessResponseEmpty(w, r)
  489. }
  490. }
  491. // GetBucketOwnershipControls https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketOwnershipControls.html
  492. func (s3a *S3ApiServer) GetBucketOwnershipControls(w http.ResponseWriter, r *http.Request) {
  493. bucket, _ := s3_constants.GetBucketAndObject(r)
  494. glog.V(3).Infof("GetBucketOwnershipControls %s", bucket)
  495. errCode := s3a.checkAccessByOwnership(r, bucket)
  496. if errCode != s3err.ErrNone {
  497. s3err.WriteErrorResponse(w, r, errCode)
  498. return
  499. }
  500. bucketEntry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  501. if err != nil {
  502. if err == filer_pb.ErrNotFound {
  503. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  504. return
  505. }
  506. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  507. return
  508. }
  509. v, ok := bucketEntry.Extended[s3_constants.ExtOwnershipKey]
  510. if !ok {
  511. s3err.WriteErrorResponse(w, r, s3err.OwnershipControlsNotFoundError)
  512. return
  513. }
  514. ownership := string(v)
  515. result := &s3.PutBucketOwnershipControlsInput{
  516. OwnershipControls: &s3.OwnershipControls{
  517. Rules: []*s3.OwnershipControlsRule{
  518. {
  519. ObjectOwnership: &ownership,
  520. },
  521. },
  522. },
  523. }
  524. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, result)
  525. }
  526. // DeleteBucketOwnershipControls https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketOwnershipControls.html
  527. func (s3a *S3ApiServer) DeleteBucketOwnershipControls(w http.ResponseWriter, r *http.Request) {
  528. bucket, _ := s3_constants.GetBucketAndObject(r)
  529. glog.V(3).Infof("PutBucketOwnershipControls %s", bucket)
  530. errCode := s3a.checkAccessByOwnership(r, bucket)
  531. if errCode != s3err.ErrNone {
  532. s3err.WriteErrorResponse(w, r, errCode)
  533. return
  534. }
  535. bucketEntry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  536. if err != nil {
  537. if err == filer_pb.ErrNotFound {
  538. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  539. return
  540. }
  541. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  542. return
  543. }
  544. _, ok := bucketEntry.Extended[s3_constants.ExtOwnershipKey]
  545. if !ok {
  546. s3err.WriteErrorResponse(w, r, s3err.OwnershipControlsNotFoundError)
  547. return
  548. }
  549. delete(bucketEntry.Extended, s3_constants.ExtOwnershipKey)
  550. err = s3a.updateEntry(s3a.option.BucketsPath, bucketEntry)
  551. if err != nil {
  552. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  553. return
  554. }
  555. emptyOwnershipControls := &s3.OwnershipControls{
  556. Rules: []*s3.OwnershipControlsRule{},
  557. }
  558. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, emptyOwnershipControls)
  559. }
  560. // GetBucketVersioningHandler Get Bucket Versioning status
  561. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketVersioning.html
  562. func (s3a *S3ApiServer) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
  563. bucket, _ := s3_constants.GetBucketAndObject(r)
  564. glog.V(3).Infof("GetBucketVersioning %s", bucket)
  565. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  566. s3err.WriteErrorResponse(w, r, err)
  567. return
  568. }
  569. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, &s3.PutBucketVersioningInput{
  570. VersioningConfiguration: &s3.VersioningConfiguration{
  571. Status: aws.String(s3.BucketVersioningStatusSuspended),
  572. },
  573. })
  574. }