123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package minio
- import (
- "bufio"
- "bytes"
- "context"
- "encoding/xml"
- "net/http"
- "net/url"
- "time"
- jsoniter "github.com/json-iterator/go"
- "github.com/minio/minio-go/v7/pkg/notification"
- "github.com/minio/minio-go/v7/pkg/s3utils"
- )
- func (c Client) SetBucketNotification(ctx context.Context, bucketName string, config notification.Configuration) error {
-
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- return err
- }
-
-
- urlValues := make(url.Values)
- urlValues.Set("notification", "")
- notifBytes, err := xml.Marshal(&config)
- if err != nil {
- return err
- }
- notifBuffer := bytes.NewReader(notifBytes)
- reqMetadata := requestMetadata{
- bucketName: bucketName,
- queryValues: urlValues,
- contentBody: notifBuffer,
- contentLength: int64(len(notifBytes)),
- contentMD5Base64: sumMD5Base64(notifBytes),
- contentSHA256Hex: sum256Hex(notifBytes),
- }
-
- resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
- defer closeResponse(resp)
- if err != nil {
- return err
- }
- if resp != nil {
- if resp.StatusCode != http.StatusOK {
- return httpRespToErrorResponse(resp, bucketName, "")
- }
- }
- return nil
- }
- func (c Client) RemoveAllBucketNotification(ctx context.Context, bucketName string) error {
- return c.SetBucketNotification(ctx, bucketName, notification.Configuration{})
- }
- func (c Client) GetBucketNotification(ctx context.Context, bucketName string) (bucketNotification notification.Configuration, err error) {
-
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- return notification.Configuration{}, err
- }
- return c.getBucketNotification(ctx, bucketName)
- }
- func (c Client) getBucketNotification(ctx context.Context, bucketName string) (notification.Configuration, error) {
- urlValues := make(url.Values)
- urlValues.Set("notification", "")
-
- resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
- bucketName: bucketName,
- queryValues: urlValues,
- contentSHA256Hex: emptySHA256Hex,
- })
- defer closeResponse(resp)
- if err != nil {
- return notification.Configuration{}, err
- }
- return processBucketNotificationResponse(bucketName, resp)
- }
- func processBucketNotificationResponse(bucketName string, resp *http.Response) (notification.Configuration, error) {
- if resp.StatusCode != http.StatusOK {
- errResponse := httpRespToErrorResponse(resp, bucketName, "")
- return notification.Configuration{}, errResponse
- }
- var bucketNotification notification.Configuration
- err := xmlDecoder(resp.Body, &bucketNotification)
- if err != nil {
- return notification.Configuration{}, err
- }
- return bucketNotification, nil
- }
- func (c Client) ListenNotification(ctx context.Context, prefix, suffix string, events []string) <-chan notification.Info {
- return c.ListenBucketNotification(ctx, "", prefix, suffix, events)
- }
- func (c Client) ListenBucketNotification(ctx context.Context, bucketName, prefix, suffix string, events []string) <-chan notification.Info {
- notificationInfoCh := make(chan notification.Info, 1)
- const notificationCapacity = 4 * 1024 * 1024
- notificationEventBuffer := make([]byte, notificationCapacity)
-
- go func(notificationInfoCh chan<- notification.Info) {
- defer close(notificationInfoCh)
-
- if bucketName != "" {
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- select {
- case notificationInfoCh <- notification.Info{
- Err: err,
- }:
- case <-ctx.Done():
- }
- return
- }
- }
-
- if s3utils.IsAmazonEndpoint(*c.endpointURL) || s3utils.IsGoogleEndpoint(*c.endpointURL) {
- select {
- case notificationInfoCh <- notification.Info{
- Err: errAPINotSupported("Listening for bucket notification is specific only to `minio` server endpoints"),
- }:
- case <-ctx.Done():
- }
- return
- }
-
-
- retryDoneCh := make(chan struct{}, 1)
-
- defer close(retryDoneCh)
-
- urlValues := make(url.Values)
- urlValues.Set("prefix", prefix)
- urlValues.Set("suffix", suffix)
- urlValues["events"] = events
-
- for range c.newRetryTimerContinous(time.Second, time.Second*30, MaxJitter, retryDoneCh) {
-
- resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
- bucketName: bucketName,
- queryValues: urlValues,
- contentSHA256Hex: emptySHA256Hex,
- })
- if err != nil {
- select {
- case notificationInfoCh <- notification.Info{
- Err: err,
- }:
- case <-ctx.Done():
- }
- return
- }
-
- if resp.StatusCode != http.StatusOK {
- errResponse := httpRespToErrorResponse(resp, bucketName, "")
- select {
- case notificationInfoCh <- notification.Info{
- Err: errResponse,
- }:
- case <-ctx.Done():
- }
- return
- }
-
- bio := bufio.NewScanner(resp.Body)
-
-
- bio.Buffer(notificationEventBuffer, notificationCapacity)
- var json = jsoniter.ConfigCompatibleWithStandardLibrary
-
- for bio.Scan() {
- var notificationInfo notification.Info
- if err = json.Unmarshal(bio.Bytes(), ¬ificationInfo); err != nil {
-
-
- select {
- case notificationInfoCh <- notification.Info{
- Err: err,
- }:
- case <-ctx.Done():
- return
- }
- closeResponse(resp)
- continue
- }
-
- select {
- case notificationInfoCh <- notificationInfo:
- case <-ctx.Done():
- closeResponse(resp)
- return
- }
- }
- if err = bio.Err(); err != nil {
- select {
- case notificationInfoCh <- notification.Info{
- Err: err,
- }:
- case <-ctx.Done():
- return
- }
- }
-
- closeResponse(resp)
- }
- }(notificationInfoCh)
-
- return notificationInfoCh
- }
|