123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- /*
- * MinIO Go Library for Amazon S3 Compatible Cloud Storage
- * Copyright 2017-2020 MinIO, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- 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"
- )
- // SetBucketNotification saves a new bucket notification with a context to control cancellations and timeouts.
- func (c Client) SetBucketNotification(ctx context.Context, bucketName string, config notification.Configuration) error {
- // Input validation.
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- return err
- }
- // Get resources properly escaped and lined up before
- // using them in http request.
- 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),
- }
- // Execute PUT to upload a new bucket notification.
- 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
- }
- // RemoveAllBucketNotification - Remove bucket notification clears all previously specified config
- func (c Client) RemoveAllBucketNotification(ctx context.Context, bucketName string) error {
- return c.SetBucketNotification(ctx, bucketName, notification.Configuration{})
- }
- // GetBucketNotification returns current bucket notification configuration
- func (c Client) GetBucketNotification(ctx context.Context, bucketName string) (bucketNotification notification.Configuration, err error) {
- // Input validation.
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- return notification.Configuration{}, err
- }
- return c.getBucketNotification(ctx, bucketName)
- }
- // Request server for notification rules.
- func (c Client) getBucketNotification(ctx context.Context, bucketName string) (notification.Configuration, error) {
- urlValues := make(url.Values)
- urlValues.Set("notification", "")
- // Execute GET on bucket to list objects.
- 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)
- }
- // processes the GetNotification http response from the server.
- 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
- }
- // ListenNotification listen for all events, this is a MinIO specific API
- func (c Client) ListenNotification(ctx context.Context, prefix, suffix string, events []string) <-chan notification.Info {
- return c.ListenBucketNotification(ctx, "", prefix, suffix, events)
- }
- // ListenBucketNotification listen for bucket events, this is a MinIO specific API
- 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)
- // Only success, start a routine to start reading line by line.
- go func(notificationInfoCh chan<- notification.Info) {
- defer close(notificationInfoCh)
- // Validate the bucket name.
- if bucketName != "" {
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- select {
- case notificationInfoCh <- notification.Info{
- Err: err,
- }:
- case <-ctx.Done():
- }
- return
- }
- }
- // Check ARN partition to verify if listening bucket is supported
- 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
- }
- // Continuously run and listen on bucket notification.
- // Create a done channel to control 'ListObjects' go routine.
- retryDoneCh := make(chan struct{}, 1)
- // Indicate to our routine to exit cleanly upon return.
- defer close(retryDoneCh)
- // Prepare urlValues to pass into the request on every loop
- urlValues := make(url.Values)
- urlValues.Set("prefix", prefix)
- urlValues.Set("suffix", suffix)
- urlValues["events"] = events
- // Wait on the jitter retry loop.
- for range c.newRetryTimerContinous(time.Second, time.Second*30, MaxJitter, retryDoneCh) {
- // Execute GET on bucket to list objects.
- 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
- }
- // Validate http response, upon error return quickly.
- if resp.StatusCode != http.StatusOK {
- errResponse := httpRespToErrorResponse(resp, bucketName, "")
- select {
- case notificationInfoCh <- notification.Info{
- Err: errResponse,
- }:
- case <-ctx.Done():
- }
- return
- }
- // Initialize a new bufio scanner, to read line by line.
- bio := bufio.NewScanner(resp.Body)
- // Use a higher buffer to support unexpected
- // caching done by proxies
- bio.Buffer(notificationEventBuffer, notificationCapacity)
- var json = jsoniter.ConfigCompatibleWithStandardLibrary
- // Unmarshal each line, returns marshaled values.
- for bio.Scan() {
- var notificationInfo notification.Info
- if err = json.Unmarshal(bio.Bytes(), ¬ificationInfo); err != nil {
- // Unexpected error during json unmarshal, send
- // the error to caller for actionable as needed.
- select {
- case notificationInfoCh <- notification.Info{
- Err: err,
- }:
- case <-ctx.Done():
- return
- }
- closeResponse(resp)
- continue
- }
- // Send notificationInfo
- 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
- }
- }
- // Close current connection before looping further.
- closeResponse(resp)
- }
- }(notificationInfoCh)
- // Returns the notification info channel, for caller to start reading from.
- return notificationInfoCh
- }
|