github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/ais/s3/bucket.go (about)

     1  // Package s3 provides Amazon S3 compatibility layer
     2  /*
     3   * Copyright (c) 2018-2022, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package s3
     6  
     7  import (
     8  	"encoding/xml"
     9  	"fmt"
    10  	"time"
    11  
    12  	"github.com/NVIDIA/aistore/cmn/cos"
    13  	"github.com/NVIDIA/aistore/cmn/debug"
    14  	"github.com/NVIDIA/aistore/core/meta"
    15  	"github.com/NVIDIA/aistore/memsys"
    16  )
    17  
    18  type (
    19  	// List bucket response
    20  	ListBucketResult struct {
    21  		Ns      string    `xml:"xmlns,attr"`
    22  		Owner   BckOwner  `xml:"Owner"`
    23  		Buckets []*Bucket `xml:"Buckets>Bucket"`
    24  	}
    25  	BckOwner struct {
    26  		ID   string `xml:"ID"`
    27  		Name string `xml:"DisplayName"`
    28  	}
    29  	Bucket struct {
    30  		Name    string `xml:"Name"`
    31  		Created string `xml:"CreationDate"`
    32  		String  string `xml:"String"`
    33  	}
    34  
    35  	// Bucket versioning
    36  	VersioningConfiguration struct {
    37  		Status string `xml:"Status"`
    38  	}
    39  
    40  	// Multiple object delete request
    41  	Delete struct {
    42  		Object []*DeleteObjectInfo `xml:"Object"`
    43  		Quiet  bool                `xml:"Quiet"`
    44  	}
    45  	DeleteObjectInfo struct {
    46  		Key     string `xml:"Key"`
    47  		Version string `xml:"Version"`
    48  	}
    49  )
    50  
    51  func NewListBucketResult() (r *ListBucketResult) {
    52  	r = &ListBucketResult{
    53  		Ns:      s3Namespace,
    54  		Owner:   BckOwner{Name: "ListAllMyBucketsResult"},
    55  		Buckets: make([]*Bucket, 0, 8),
    56  	}
    57  	r.Owner.ID = "1" // to satisfy s3
    58  	return
    59  }
    60  
    61  func (r *ListBucketResult) Add(bck *meta.Bck) {
    62  	var warn string
    63  	for _, b := range r.Buckets {
    64  		if b.Name == bck.Name {
    65  			// NOTE: when bck.Name is not unique
    66  			warn = fmt.Sprintf(" (WARNING: {%s, %s} and {%s, Provider: %s} share the same name)",
    67  				b.Name, b.String, bck.Name, bck.Provider)
    68  		}
    69  	}
    70  	b := &Bucket{
    71  		Name:    bck.Name,
    72  		Created: cos.FormatTime(time.Unix(0, bck.Props.Created), cos.ISO8601),
    73  		String:  "Provider: " + bck.Provider + warn,
    74  	}
    75  	r.Buckets = append(r.Buckets, b)
    76  }
    77  
    78  func (r *ListBucketResult) MustMarshal(sgl *memsys.SGL) {
    79  	sgl.Write([]byte(xml.Header))
    80  	err := xml.NewEncoder(sgl).Encode(r)
    81  	debug.AssertNoErr(err)
    82  }
    83  
    84  func NewVersioningConfiguration(enabled bool) *VersioningConfiguration {
    85  	if enabled {
    86  		return &VersioningConfiguration{Status: versioningEnabled}
    87  	}
    88  	return &VersioningConfiguration{Status: versioningDisabled}
    89  }
    90  
    91  func (r *VersioningConfiguration) MustMarshal(sgl *memsys.SGL) {
    92  	sgl.Write([]byte(xml.Header))
    93  	err := xml.NewEncoder(sgl).Encode(r)
    94  	debug.AssertNoErr(err)
    95  }
    96  
    97  func (r *VersioningConfiguration) Enabled() bool {
    98  	return r.Status == versioningEnabled
    99  }