-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbucket.go
More file actions
940 lines (781 loc) · 24.2 KB
/
bucket.go
File metadata and controls
940 lines (781 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
// LICENSE BSD-2-Clause-FreeBSD
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
package simples3
import (
"bytes"
"crypto/md5"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// Bucket represents a single S3 bucket.
type Bucket struct {
Name string `xml:"Name"`
CreationDate time.Time `xml:"CreationDate"`
}
// ListBucketsInput is passed to ListBuckets as a parameter.
type ListBucketsInput struct {
// Empty - ListBuckets lists all buckets for the account
}
// ListBucketsOutput is returned by ListBuckets.
type ListBucketsOutput struct {
Buckets []Bucket `xml:"Buckets>Bucket"`
Owner struct {
ID string `xml:"ID"`
DisplayName string `xml:"DisplayName"`
} `xml:"Owner"`
}
// CreateBucketInput is passed to CreateBucket as a parameter.
type CreateBucketInput struct {
// Required: The name of the bucket to create
Bucket string
// Optional: AWS region for the bucket.
// If empty, uses the region from S3 struct.
// Note: For us-east-1, no LocationConstraint is needed.
Region string
}
// CreateBucketOutput is returned by CreateBucket.
type CreateBucketOutput struct {
// Location header from the response
Location string
}
// DeleteBucketInput is passed to DeleteBucket as a parameter.
type DeleteBucketInput struct {
// Required: The name of the bucket to delete
// Note: The bucket must be empty before deletion
Bucket string
}
// ListBuckets lists all S3 buckets for the AWS account.
// It makes a GET request to the S3 service endpoint (not a specific bucket).
func (s3 *S3) ListBuckets(input ListBucketsInput) (ListBucketsOutput, error) {
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return ListBucketsOutput{}, err
}
// Build endpoint URL - ListBuckets uses the service endpoint (no bucket name)
var endpoint string
if len(s3.Endpoint) > 0 {
endpoint = s3.Endpoint + "/"
} else {
// For AWS, use the service endpoint
endpoint = fmt.Sprintf("https://s3.%s.amazonaws.com/", s3.Region)
}
// Create HTTP request
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return ListBucketsOutput{}, err
}
// Sign the request
if err := s3.signRequest(req); err != nil {
return ListBucketsOutput{}, err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return ListBucketsOutput{}, err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return ListBucketsOutput{}, err
}
// Handle non-OK status codes
if res.StatusCode != http.StatusOK {
return ListBucketsOutput{}, fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
// Parse XML response
var result ListBucketsOutput
if err := xml.Unmarshal(body, &result); err != nil {
return ListBucketsOutput{}, err
}
return result, nil
}
// CreateBucket creates a new S3 bucket.
// For regions other than us-east-1, it sends a LocationConstraint in the request body.
func (s3 *S3) CreateBucket(input CreateBucketInput) (CreateBucketOutput, error) {
// Validate input
if input.Bucket == "" {
return CreateBucketOutput{}, fmt.Errorf("bucket name is required")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return CreateBucketOutput{}, err
}
// Determine region to use
region := input.Region
if region == "" {
region = s3.Region
}
// Build endpoint URL
url := s3.getURL(input.Bucket)
// Prepare request body for non-us-east-1 regions
// AWS S3 requires LocationConstraint for regions other than us-east-1
var body io.Reader
if region != "us-east-1" && region != "" {
xmlBody := fmt.Sprintf(
"<CreateBucketConfiguration><LocationConstraint>%s</LocationConstraint></CreateBucketConfiguration>",
region,
)
body = strings.NewReader(xmlBody)
}
// Create HTTP request
req, err := http.NewRequest(http.MethodPut, url, body)
if err != nil {
return CreateBucketOutput{}, err
}
// Sign the request
if err := s3.signRequest(req); err != nil {
return CreateBucketOutput{}, err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return CreateBucketOutput{}, err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body for error messages
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return CreateBucketOutput{}, err
}
// Handle non-success status codes
// AWS returns 200 OK or 201 Created on success
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
return CreateBucketOutput{}, fmt.Errorf("status code: %s: %s", res.Status, string(bodyBytes))
}
// Extract location from response header
location := res.Header.Get("Location")
return CreateBucketOutput{Location: location}, nil
}
// DeleteBucket deletes an empty S3 bucket.
// The bucket must be empty (no objects) before it can be deleted.
// Returns an error if the bucket is not empty or does not exist.
func (s3 *S3) DeleteBucket(input DeleteBucketInput) error {
// Validate input
if input.Bucket == "" {
return fmt.Errorf("bucket name is required")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return err
}
// Build endpoint URL
url := s3.getURL(input.Bucket)
// Create HTTP request
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return err
}
// Sign the request
if err := s3.signRequest(req); err != nil {
return err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body for error messages
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return err
}
// Handle non-success status codes
// AWS returns 204 No Content on successful deletion
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("status code: %s: %s", res.Status, string(bodyBytes))
}
return nil
}
// VersioningConfiguration represents the versioning configuration of a bucket.
type VersioningConfiguration struct {
Status string `xml:"Status"` // Enabled or Suspended
MfaDelete string `xml:"MfaDelete,omitempty"` // Enabled or Disabled
}
// PutBucketVersioningInput is passed to PutBucketVersioning as a parameter.
type PutBucketVersioningInput struct {
// Required: The name of the bucket
Bucket string
// Required: Versioning status (Enabled or Suspended)
Status string
// Optional: MFA Delete status (Enabled or Disabled)
// Note: Requires MFA authentication in the request, which is not yet supported
MfaDelete string
}
// GetBucketVersioningOutput is returned by GetBucketVersioning.
type GetBucketVersioningOutput struct {
Status string
MfaDelete string
}
// versioningConfigurationXML is the internal type for XML marshaling.
type versioningConfigurationXML struct {
XMLName xml.Name `xml:"VersioningConfiguration"`
XMLNS string `xml:"xmlns,attr"`
Status string `xml:"Status"`
MfaDelete string `xml:"MfaDelete,omitempty"`
}
// PutBucketVersioning sets the versioning configuration for a bucket.
func (s3 *S3) PutBucketVersioning(input PutBucketVersioningInput) error {
// Validate input
if input.Bucket == "" {
return fmt.Errorf("bucket name is required")
}
if input.Status != "Enabled" && input.Status != "Suspended" {
return fmt.Errorf("status must be 'Enabled' or 'Suspended'")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return err
}
// Build XML request body
config := versioningConfigurationXML{
XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/",
Status: input.Status,
MfaDelete: input.MfaDelete,
}
xmlBody, err := xml.Marshal(config)
if err != nil {
return err
}
// Calculate Content-MD5
md5Hash := md5.Sum(xmlBody)
contentMD5 := base64.StdEncoding.EncodeToString(md5Hash[:])
// Build URL with ?versioning query parameter
baseURL := s3.getURL(input.Bucket)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return err
}
parsedURL.RawQuery = "versioning"
// Create PUT request
req, err := http.NewRequest(http.MethodPut, parsedURL.String(), bytes.NewReader(xmlBody))
if err != nil {
return err
}
// Set headers
req.ContentLength = int64(len(xmlBody))
req.Header.Set("Content-Type", "application/xml")
req.Header.Set("Content-MD5", contentMD5)
// Calculate SHA256
h := sha256.New()
h.Write(xmlBody)
req.Header.Set("x-amz-content-sha256", fmt.Sprintf("%x", h.Sum(nil)))
req.Header.Set("Host", req.URL.Host)
// Sign the request
if err := s3.signRequest(req); err != nil {
return err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
// Handle non-OK status codes
if res.StatusCode != http.StatusOK {
return fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
return nil
}
// GetBucketVersioning gets the versioning configuration for a bucket.
func (s3 *S3) GetBucketVersioning(bucket string) (GetBucketVersioningOutput, error) {
// Validate input
if bucket == "" {
return GetBucketVersioningOutput{}, fmt.Errorf("bucket name is required")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return GetBucketVersioningOutput{}, err
}
// Build URL with ?versioning query parameter
baseURL := s3.getURL(bucket)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return GetBucketVersioningOutput{}, err
}
parsedURL.RawQuery = "versioning"
// Create GET request
req, err := http.NewRequest(http.MethodGet, parsedURL.String(), nil)
if err != nil {
return GetBucketVersioningOutput{}, err
}
// Sign the request
if err := s3.signRequest(req); err != nil {
return GetBucketVersioningOutput{}, err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return GetBucketVersioningOutput{}, err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return GetBucketVersioningOutput{}, err
}
// Handle non-OK status codes
if res.StatusCode != http.StatusOK {
return GetBucketVersioningOutput{}, fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
// Parse XML response
var config VersioningConfiguration
if err := xml.Unmarshal(body, &config); err != nil {
return GetBucketVersioningOutput{}, err
}
return GetBucketVersioningOutput{
Status: config.Status,
MfaDelete: config.MfaDelete,
}, nil
}
// --- ACL Operations ---
// AccessControlPolicy represents the Access Control List (ACL) for a bucket or object.
type AccessControlPolicy struct {
XMLName xml.Name `xml:"AccessControlPolicy"`
XMLNS string `xml:"xmlns,attr,omitempty"`
Owner Owner `xml:"Owner"`
AccessControlList []Grant `xml:"AccessControlList>Grant"`
}
// Owner represents the owner of the bucket or object.
type Owner struct {
ID string `xml:"ID,omitempty"`
DisplayName string `xml:"DisplayName,omitempty"`
}
// Grant represents a permission grant.
type Grant struct {
Grantee Grantee `xml:"Grantee"`
Permission string `xml:"Permission"`
}
// Grantee represents the recipient of the permission grant.
type Grantee struct {
XMLNS string `xml:"xmlns:xsi,attr,omitempty"`
Type string `xml:"xsi:type,attr"` // CanonicalUser, AmazonCustomerByEmail, Group
ID string `xml:"ID,omitempty"`
DisplayName string `xml:"DisplayName,omitempty"`
URI string `xml:"URI,omitempty"` // For Group
EmailAddress string `xml:"EmailAddress,omitempty"` // For AmazonCustomerByEmail
}
// PutBucketAclInput is passed to PutBucketAcl.
type PutBucketAclInput struct {
// Required: The name of the bucket
Bucket string
// Optional: Canned ACL (private, public-read, public-read-write, authenticated-read)
// If CannedACL is set, AccessControlPolicy is ignored.
CannedACL string
// Optional: Full Access Control Policy
AccessControlPolicy *AccessControlPolicy
}
// PutBucketAcl sets the Access Control List (ACL) for a bucket.
// You can either use a CannedACL OR provide a full AccessControlPolicy.
func (s3 *S3) PutBucketAcl(input PutBucketAclInput) error {
// Validate input
if input.Bucket == "" {
return fmt.Errorf("bucket name is required")
}
if input.CannedACL == "" && input.AccessControlPolicy == nil {
return fmt.Errorf("either CannedACL or AccessControlPolicy must be provided")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return err
}
// Build URL with ?acl query parameter
baseURL := s3.getURL(input.Bucket)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return err
}
parsedURL.RawQuery = "acl"
var req *http.Request
var bodyReader io.Reader
var contentMD5 string
var sha256Hash string
if input.AccessControlPolicy != nil {
// Use custom ACL body
input.AccessControlPolicy.XMLNS = "http://s3.amazonaws.com/doc/2006-03-01/"
// Ensure namespace is set on Grantees if needed, though usually top level is enough.
// Some S3 implementations require xsi namespace.
for i := range input.AccessControlPolicy.AccessControlList {
input.AccessControlPolicy.AccessControlList[i].Grantee.XMLNS = "http://www.w3.org/2001/XMLSchema-instance"
}
xmlBody, err := xml.Marshal(input.AccessControlPolicy)
if err != nil {
return err
}
bodyReader = bytes.NewReader(xmlBody)
// Calculate MD5
md5Sum := md5.Sum(xmlBody)
contentMD5 = base64.StdEncoding.EncodeToString(md5Sum[:])
// Calculate SHA256
h := sha256.New()
h.Write(xmlBody)
sha256Hash = fmt.Sprintf("%x", h.Sum(nil))
req, err = http.NewRequest(http.MethodPut, parsedURL.String(), bodyReader)
if err != nil {
return err
}
req.ContentLength = int64(len(xmlBody))
req.Header.Set("Content-Type", "application/xml")
req.Header.Set("Content-MD5", contentMD5)
req.Header.Set("x-amz-content-sha256", sha256Hash)
} else {
// Use Canned ACL via header
req, err = http.NewRequest(http.MethodPut, parsedURL.String(), nil)
if err != nil {
return err
}
req.Header.Set("x-amz-acl", input.CannedACL)
// Empty body SHA256
h := sha256.New()
sha256Hash = fmt.Sprintf("%x", h.Sum(nil))
req.Header.Set("x-amz-content-sha256", sha256Hash)
}
req.Header.Set("Host", req.URL.Host)
// Sign the request
if err := s3.signRequest(req); err != nil {
return err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
// Handle non-OK status codes
if res.StatusCode != http.StatusOK {
return fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
return nil
}
// GetBucketAcl gets the Access Control List (ACL) for a bucket.
func (s3 *S3) GetBucketAcl(bucket string) (AccessControlPolicy, error) {
// Validate input
if bucket == "" {
return AccessControlPolicy{}, fmt.Errorf("bucket name is required")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return AccessControlPolicy{}, err
}
// Build URL with ?acl query parameter
baseURL := s3.getURL(bucket)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return AccessControlPolicy{}, err
}
parsedURL.RawQuery = "acl"
// Create GET request
req, err := http.NewRequest(http.MethodGet, parsedURL.String(), nil)
if err != nil {
return AccessControlPolicy{}, err
}
// Sign the request
if err := s3.signRequest(req); err != nil {
return AccessControlPolicy{}, err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return AccessControlPolicy{}, err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return AccessControlPolicy{}, err
}
// Handle non-OK status codes
if res.StatusCode != http.StatusOK {
return AccessControlPolicy{}, fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
// Parse XML response
var policy AccessControlPolicy
if err := xml.Unmarshal(body, &policy); err != nil {
return AccessControlPolicy{}, err
}
return policy, nil
}
// --- Lifecycle Operations ---
// LifecycleConfiguration represents the lifecycle configuration for a bucket.
type LifecycleConfiguration struct {
XMLName xml.Name `xml:"LifecycleConfiguration"`
XMLNS string `xml:"xmlns,attr,omitempty"`
Rules []LifecycleRule `xml:"Rule"`
}
// LifecycleRule represents a single lifecycle rule.
type LifecycleRule struct {
ID string `xml:"ID,omitempty"`
Status string `xml:"Status"` // Enabled or Disabled
Filter *LifecycleFilter `xml:"Filter,omitempty"`
// Legacy Prefix support (S3 supports both, but prefer Filter for V2)
// If Prefix is provided here, do not use Filter.
Prefix *string `xml:"Prefix,omitempty"`
Expiration *LifecycleExpiration `xml:"Expiration,omitempty"`
Transitions []LifecycleTransition `xml:"Transition,omitempty"`
NoncurrentVersionExpiration *LifecycleNoncurrentVersionExpiration `xml:"NoncurrentVersionExpiration,omitempty"`
NoncurrentVersionTransitions []LifecycleNoncurrentVersionTransition `xml:"NoncurrentVersionTransition,omitempty"`
AbortIncompleteMultipartUpload *LifecycleAbortIncompleteMultipartUpload `xml:"AbortIncompleteMultipartUpload,omitempty"`
}
// LifecycleFilter represents the filter for a lifecycle rule.
type LifecycleFilter struct {
Prefix string `xml:"Prefix,omitempty"`
Tag *Tag `xml:"Tag,omitempty"`
And *struct {
Prefix string `xml:"Prefix,omitempty"`
Tags []Tag `xml:"Tag,omitempty"`
} `xml:"And,omitempty"`
}
// LifecycleExpiration represents the expiration action.
type LifecycleExpiration struct {
Date string `xml:"Date,omitempty"` // ISO 8601 format
Days int `xml:"Days,omitempty"`
ExpiredObjectDeleteMarker bool `xml:"ExpiredObjectDeleteMarker,omitempty"`
}
// LifecycleTransition represents a transition action.
type LifecycleTransition struct {
Date string `xml:"Date,omitempty"`
Days int `xml:"Days,omitempty"`
StorageClass string `xml:"StorageClass"`
}
// LifecycleNoncurrentVersionExpiration represents expiration for noncurrent versions.
type LifecycleNoncurrentVersionExpiration struct {
NoncurrentDays int `xml:"NoncurrentDays"`
}
// LifecycleNoncurrentVersionTransition represents transition for noncurrent versions.
type LifecycleNoncurrentVersionTransition struct {
NoncurrentDays int `xml:"NoncurrentDays"`
StorageClass string `xml:"StorageClass"`
}
// LifecycleAbortIncompleteMultipartUpload represents abort action for incomplete uploads.
type LifecycleAbortIncompleteMultipartUpload struct {
DaysAfterInitiation int `xml:"DaysAfterInitiation"`
}
// PutBucketLifecycleInput is passed to PutBucketLifecycle.
type PutBucketLifecycleInput struct {
// Required: The name of the bucket
Bucket string
// Required: The lifecycle configuration
Configuration *LifecycleConfiguration
}
// PutBucketLifecycle sets the lifecycle configuration for a bucket.
func (s3 *S3) PutBucketLifecycle(input PutBucketLifecycleInput) error {
// Validate input
if input.Bucket == "" {
return fmt.Errorf("bucket name is required")
}
if input.Configuration == nil || len(input.Configuration.Rules) == 0 {
return fmt.Errorf("lifecycle configuration with at least one rule is required")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return err
}
// Build URL with ?lifecycle query parameter
baseURL := s3.getURL(input.Bucket)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return err
}
parsedURL.RawQuery = "lifecycle"
// Set XML Namespace
input.Configuration.XMLNS = "http://s3.amazonaws.com/doc/2006-03-01/"
xmlBody, err := xml.Marshal(input.Configuration)
if err != nil {
return err
}
// Calculate MD5
md5Hash := md5.Sum(xmlBody)
contentMD5 := base64.StdEncoding.EncodeToString(md5Hash[:])
// Calculate SHA256
h := sha256.New()
h.Write(xmlBody)
sha256Hash := fmt.Sprintf("%x", h.Sum(nil))
// Create PUT request
req, err := http.NewRequest(http.MethodPut, parsedURL.String(), bytes.NewReader(xmlBody))
if err != nil {
return err
}
req.ContentLength = int64(len(xmlBody))
req.Header.Set("Content-Type", "application/xml")
req.Header.Set("Content-MD5", contentMD5)
req.Header.Set("x-amz-content-sha256", sha256Hash)
req.Header.Set("Host", req.URL.Host)
// Sign the request
if err := s3.signRequest(req); err != nil {
return err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
// Handle non-OK status codes
if res.StatusCode != http.StatusOK {
return fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
return nil
}
// GetBucketLifecycle gets the lifecycle configuration for a bucket.
func (s3 *S3) GetBucketLifecycle(bucket string) (LifecycleConfiguration, error) {
// Validate input
if bucket == "" {
return LifecycleConfiguration{}, fmt.Errorf("bucket name is required")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return LifecycleConfiguration{}, err
}
// Build URL with ?lifecycle query parameter
baseURL := s3.getURL(bucket)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return LifecycleConfiguration{}, err
}
parsedURL.RawQuery = "lifecycle"
// Create GET request
req, err := http.NewRequest(http.MethodGet, parsedURL.String(), nil)
if err != nil {
return LifecycleConfiguration{}, err
}
// Sign the request
if err := s3.signRequest(req); err != nil {
return LifecycleConfiguration{}, err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return LifecycleConfiguration{}, err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return LifecycleConfiguration{}, err
}
// Handle non-OK status codes
if res.StatusCode != http.StatusOK {
// S3 returns 404 if no lifecycle configuration exists
if res.StatusCode == http.StatusNotFound {
// Check if it's actually "NoSuchLifecycleConfiguration"
if strings.Contains(string(body), "NoSuchLifecycleConfiguration") {
return LifecycleConfiguration{}, fmt.Errorf("no lifecycle configuration found")
}
}
return LifecycleConfiguration{}, fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
// Parse XML response
var config LifecycleConfiguration
if err := xml.Unmarshal(body, &config); err != nil {
return LifecycleConfiguration{}, err
}
return config, nil
}
// DeleteBucketLifecycle deletes the lifecycle configuration for a bucket.
func (s3 *S3) DeleteBucketLifecycle(input DeleteBucketInput) error {
// Reuse DeleteBucketInput since it just needs the bucket name
// Validate input
if input.Bucket == "" {
return fmt.Errorf("bucket name is required")
}
// Renew IAM token if needed
if err := s3.renewIAMToken(); err != nil {
return err
}
// Build URL with ?lifecycle query parameter
baseURL := s3.getURL(input.Bucket)
parsedURL, err := url.Parse(baseURL)
if err != nil {
return err
}
parsedURL.RawQuery = "lifecycle"
// Create DELETE request
req, err := http.NewRequest(http.MethodDelete, parsedURL.String(), nil)
if err != nil {
return err
}
// Sign the request
if err := s3.signRequest(req); err != nil {
return err
}
// Execute request
client := s3.getClient()
res, err := client.Do(req)
if err != nil {
return err
}
defer func() {
res.Body.Close()
io.Copy(io.Discard, res.Body)
}()
// Read response body
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
// Handle non-success status codes
// AWS returns 204 No Content on successful deletion
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("status code: %s: %s", res.Status, string(body))
}
return nil
}