Debian Codegraph @

s3/types.go

raw
1 package s3
2
3 import (
4 "encoding/xml"
5 "fmt"
6 "net/http"
7 "net/url"
8 "strings"
9 "time"
10 )
11
12 // Config holds the parsed fields from an S3 URL. Use ParseURL to create one from a URL string.
13 type Config struct {
14 Scheme string // URL scheme, e.g. "https" or "http"
15 Endpoint string // host[:port] only, e.g. "s3.us-east-1.amazonaws.com"
16 PathStyle bool
17 Bucket string
18 Prefix string
19 Region string
20 AccessKey string
21 SecretKey string
22 DisableHTTP2 bool // Force HTTP/1.1 to work around HTTP/2 issues with some S3-compatible providers
23 HTTPClient *http.Client // if nil, a default client is created (respecting DisableHTTP2)
24 }
25
26 // BucketURL returns the base URL for bucket-level operations.
27 func (c *Config) BucketURL() string {
28 scheme := "https"
29 if c.Scheme != "" {
30 scheme = c.Scheme
31 }
32 if c.PathStyle {
33 return fmt.Sprintf("%s://%s/%s", scheme, c.Endpoint, c.Bucket)
34 }
35 return fmt.Sprintf("%s://%s.%s", scheme, c.Bucket, c.Endpoint)
36 }
37
38 // HostHeader returns the value for the Host header.
39 func (c *Config) HostHeader() string {
40 if c.PathStyle {
41 return c.Endpoint
42 }
43 return c.Bucket + "." + c.Endpoint
44 }
45
46 // ListPrefix returns the prefix to use in ListObjectsV2 requests,
47 // with a trailing slash so that only objects under the prefix directory are returned.
48 func (c *Config) ListPrefix() string {
49 if c.Prefix != "" {
50 return c.Prefix + "/"
51 }
52 return ""
53 }
54
55 // StripPrefix removes the configured prefix from a key returned by ListObjectsV2,
56 // so keys match what was passed to PutObject/GetObject/DeleteObjects.
57 func (c *Config) StripPrefix(key string) string {
58 if c.Prefix != "" {
59 return strings.TrimPrefix(key, c.Prefix+"/")
60 }
61 return key
62 }
63
64 // ObjectKey prepends the configured prefix to the given key.
65 func (c *Config) ObjectKey(key string) string {
66 if c.Prefix != "" {
67 return c.Prefix + "/" + key
68 }
69 return key
70 }
71
72 // ObjectURL returns the full URL for an object, automatically prepending the configured prefix.
73 func (c *Config) ObjectURL(key string) string {
74 u, _ := url.JoinPath(c.BucketURL(), c.ObjectKey(key))
75 return u
76 }
77
78 // Object represents an S3 object returned by list operations.
79 type Object struct {
80 Key string
81 Size int64
82 LastModified time.Time
83 }
84
85 // errorResponse is returned when S3 responds with a non-2xx status code.
86 type errorResponse struct {
87 StatusCode int
88 Code string `xml:"Code"`
89 Message string `xml:"Message"`
90 Body string `xml:"-"` // raw response body
91 }
92
93 func (e *errorResponse) Error() string {
94 if e.Code != "" {
95 return fmt.Sprintf("s3: %s (HTTP %d): %s", e.Code, e.StatusCode, e.Message)
96 }
97 return fmt.Sprintf("s3: HTTP %d: %s", e.StatusCode, e.Body)
98 }
99
100 // listObjectsV2Result is the XML response from S3 ListObjectsV2
101 type listObjectsV2Result struct {
102 Contents []*listObject `xml:"Contents"`
103 IsTruncated bool `xml:"IsTruncated"`
104 NextContinuationToken string `xml:"NextContinuationToken"`
105 }
106
107 type listObject struct {
108 Key string `xml:"Key"`
109 Size int64 `xml:"Size"`
110 LastModified string `xml:"LastModified"`
111 }
112
113 // deleteObjectsRequest is the XML request body for S3 DeleteObjects
114 type deleteObjectsRequest struct {
115 XMLName xml.Name `xml:"Delete"`
116 Quiet bool `xml:"Quiet"`
117 Objects []*deleteObject `xml:"Object"`
118 }
119
120 type deleteObject struct {
121 Key string `xml:"Key"`
122 }
123
124 // deleteObjectsResult is the XML response from S3 DeleteObjects
125 type deleteObjectsResult struct {
126 Errors []*deleteError `xml:"Error"`
127 }
128
129 type deleteError struct {
130 Key string `xml:"Key"`
131 Code string `xml:"Code"`
132 Message string `xml:"Message"`
133 }
134
135 // listMultipartUploadsResult is the XML response from S3 listMultipartUploads
136 type listMultipartUploadsResult struct {
137 Uploads []*listUpload `xml:"Upload"`
138 IsTruncated bool `xml:"IsTruncated"`
139 NextKeyMarker string `xml:"NextKeyMarker"`
140 NextUploadIDMarker string `xml:"NextUploadIdMarker"`
141 }
142
143 type listUpload struct {
144 Key string `xml:"Key"`
145 UploadID string `xml:"UploadId"`
146 Initiated string `xml:"Initiated"`
147 }
148
149 // multipartUpload represents an in-progress multipart upload returned by listMultipartUploads.
150 type multipartUpload struct {
151 Key string
152 UploadID string
153 Initiated time.Time
154 }
155
156 // initiateMultipartUploadResult is the XML response from S3 InitiateMultipartUpload
157 type initiateMultipartUploadResult struct {
158 UploadID string `xml:"UploadId"`
159 }
160
161 // completeMultipartUploadRequest is the XML request body for S3 CompleteMultipartUpload
162 type completeMultipartUploadRequest struct {
163 XMLName xml.Name `xml:"CompleteMultipartUpload"`
164 Parts []*completedPart `xml:"Part"`
165 }
166
167 // completedPart represents a successfully uploaded part for CompleteMultipartUpload
168 type completedPart struct {
169 PartNumber int `xml:"PartNumber"`
170 ETag string `xml:"ETag"`
171 }