InitializeS3Client creates or updates S3 client for a user
(userID string, config *S3Config)
| 101 | |
| 102 | // InitializeS3Client creates or updates S3 client for a user |
| 103 | func (m *S3Manager) InitializeS3Client(userID string, config *S3Config) error { |
| 104 | if !config.Enabled { |
| 105 | m.RemoveClient(userID) |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | m.mu.Lock() |
| 110 | defer m.mu.Unlock() |
| 111 | |
| 112 | // Create custom credentials provider |
| 113 | credProvider := credentials.NewStaticCredentialsProvider( |
| 114 | config.AccessKey, |
| 115 | config.SecretKey, |
| 116 | "", |
| 117 | ) |
| 118 | |
| 119 | // Configure S3 client |
| 120 | cfg := aws.Config{ |
| 121 | Region: config.Region, |
| 122 | Credentials: credProvider, |
| 123 | } |
| 124 | |
| 125 | if config.Endpoint != "" { |
| 126 | customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { |
| 127 | if service == s3.ServiceID { |
| 128 | return aws.Endpoint{ |
| 129 | URL: config.Endpoint, |
| 130 | HostnameImmutable: config.PathStyle, |
| 131 | }, nil |
| 132 | } |
| 133 | return aws.Endpoint{}, &aws.EndpointNotFoundError{} |
| 134 | }) |
| 135 | cfg.EndpointResolverWithOptions = customResolver |
| 136 | } |
| 137 | |
| 138 | // Create S3 client |
| 139 | client := s3.NewFromConfig(cfg, func(o *s3.Options) { |
| 140 | o.UsePathStyle = config.PathStyle |
| 141 | }) |
| 142 | |
| 143 | m.clients[userID] = client |
| 144 | m.configs[userID] = config |
| 145 | |
| 146 | log.Info().Str("userID", userID).Str("bucket", config.Bucket).Msg("S3 client initialized") |
| 147 | return nil |
| 148 | } |
| 149 | |
| 150 | // RemoveClient removes S3 client for a user |
| 151 | func (m *S3Manager) RemoveClient(userID string) { |
no test coverage detected