You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.6 KiB

  1. package main
  2. import (
  3. "context"
  4. "github.com/aws/aws-sdk-go-v2/aws"
  5. "github.com/aws/aws-sdk-go-v2/aws/retry"
  6. "github.com/aws/aws-sdk-go-v2/config"
  7. "github.com/aws/aws-sdk-go-v2/credentials"
  8. "github.com/aws/aws-sdk-go-v2/service/s3"
  9. "time"
  10. )
  11. func main() {
  12. cfg := MyConfig{
  13. Key: "any",
  14. Secret: "any",
  15. Region: "US",
  16. Endpoint: MyEndpoint{
  17. URL: "http://localhost:8333",
  18. },
  19. Bucket: MyBucketConfig{
  20. Name: "newbucket",
  21. Versioning: false,
  22. },
  23. MaxBackoffDelay: aws.Int(int(time.Second * 5)),
  24. MaxRetryAttempts: aws.Int(1),
  25. }
  26. awsCfg, err := MyAwsConfig(cfg)
  27. if err != nil {
  28. panic(err)
  29. }
  30. svc := s3.NewFromConfig(*awsCfg, func(o *s3.Options) {
  31. o.UsePathStyle = true
  32. })
  33. // Use the S3 client to interact with SeaweedFS
  34. // ...
  35. // Example: List all buckets
  36. result, err := svc.ListBuckets(context.Background(), &s3.ListBucketsInput{})
  37. // no errors - got list of buckets
  38. if err != nil {
  39. panic(err)
  40. }
  41. // Print the list of buckets
  42. for _, bucket := range result.Buckets {
  43. println(*bucket.Name)
  44. }
  45. bucket := "bucket1"
  46. _, err = svc.HeadBucket(context.Background(), &s3.HeadBucketInput{Bucket: &bucket})
  47. // ERROR HERE
  48. if err != nil {
  49. println(err)
  50. }
  51. }
  52. // === helpers
  53. func MyAwsConfig(cfg MyConfig) (*aws.Config, error) {
  54. cred := aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(cfg.Key, cfg.Secret, ""))
  55. customResolver := aws.EndpointResolverWithOptionsFunc(
  56. func(service, region string, options ...interface{}) (aws.Endpoint, error) {
  57. return aws.Endpoint{
  58. URL: cfg.Endpoint.URL,
  59. SigningRegion: cfg.Region,
  60. }, nil
  61. })
  62. awsCfg, err := config.LoadDefaultConfig(context.TODO(),
  63. config.WithRegion(cfg.Region),
  64. config.WithCredentialsProvider(cred),
  65. config.WithEndpointResolverWithOptions(customResolver),
  66. config.WithRetryer(func() aws.Retryer {
  67. r := retry.AddWithMaxAttempts(retry.NewStandard(), *cfg.MaxRetryAttempts)
  68. return retry.AddWithMaxBackoffDelay(r, time.Duration(*cfg.MaxBackoffDelay*1000*1000))
  69. }))
  70. return &awsCfg, err
  71. }
  72. type MyConfig struct {
  73. // Access key of S3 AWS.
  74. Key string
  75. // Access secret of S3 AWS.
  76. Secret string
  77. // Region.
  78. Region string
  79. // AWS endpoint.
  80. Endpoint MyEndpoint
  81. // Bucket configuration.
  82. Bucket MyBucketConfig
  83. // File access.
  84. FileAccess MyFileAccessType
  85. // Maximum backoff delay (ms, default: 20 sec).
  86. MaxBackoffDelay *int
  87. // Maximum attempts to retry operation on error (default: 5).
  88. MaxRetryAttempts *int
  89. }
  90. type MyBucketConfig struct {
  91. // Name of bucket
  92. Name string
  93. // Enable or not versioning
  94. Versioning bool
  95. }
  96. type MyEndpoint struct {
  97. URL string
  98. }
  99. type MyFileAccessType byte