Created
August 6, 2023 23:00
-
-
Save Semior001/976d0dbc9bd8a1405b18ab2db3da0729 to your computer and use it in GitHub Desktop.
s3 removing bucket setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/credentials" | |
s3session "github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"github.com/jessevdk/go-flags" | |
) | |
var opts struct { | |
S3 struct { | |
Endpoint string `long:"endpoint" env:"ENDPOINT" description:"S3 endpoint" required:"true"` | |
Region string `long:"region" env:"REGION" description:"S3 region" required:"true"` | |
SSL bool `long:"ssl" env:"SSL" description:"Turn on S3 ssl"` | |
AccessKey struct { | |
ID string `long:"id" env:"ID" description:"S3 access key id" required:"true"` | |
Secret string `long:"secret" env:"SECRET" description:"S3 secret access key" required:"true"` | |
} `group:"AccessKey" namespace:"access-key" env-namespace:"ACCESS_KEY"` | |
Bucket string `long:"bucket" env:"BUCKET" description:"S3 bucket" required:"true"` | |
} `group:"S3" namespace:"s3" env-namespace:"S3"` | |
} | |
func main() { | |
if _, err := flags.Parse(&opts); err != nil { | |
log.Fatalf("[ERROR] failed to parse flags: %v", err) | |
} | |
if err := run(context.Background()); err != nil { | |
log.Fatalf("[ERROR] failed to run: %v", err) | |
} | |
} | |
func run(ctx context.Context) error { | |
cfg := &aws.Config{ | |
Credentials: credentials.NewStaticCredentials(opts.S3.AccessKey.ID, opts.S3.AccessKey.Secret, ""), | |
Endpoint: aws.String(opts.S3.Endpoint), | |
Region: aws.String(opts.S3.Region), | |
DisableSSL: aws.Bool(!opts.S3.SSL), | |
S3ForcePathStyle: aws.Bool(true), | |
HTTPClient: &http.Client{Timeout: 5 * time.Second}, | |
} | |
se, err := s3session.NewSession(cfg) | |
if err != nil { | |
return fmt.Errorf("create s3 session: %w", err) | |
} | |
cl := s3.New(se) | |
out, err := cl.PutBucketLifecycleConfiguration(&s3.PutBucketLifecycleConfigurationInput{ | |
Bucket: aws.String(opts.S3.Bucket), | |
LifecycleConfiguration: &s3.BucketLifecycleConfiguration{Rules: []*s3.LifecycleRule{{ | |
ID: aws.String("remove-temporary-objects"), | |
Status: aws.String("Enabled"), | |
Expiration: &s3.LifecycleExpiration{Days: aws.Int64(1)}, | |
Filter: &s3.LifecycleRuleFilter{ | |
Tag: &s3.Tag{Key: aws.String("fcache-temporary"), Value: aws.String("true")}, | |
}, | |
}}}, | |
}) | |
if err != nil { | |
return fmt.Errorf("update bucket lifecycle configuration: %w", err) | |
} | |
log.Printf("[INFO] received response from s3: %v", out) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment