-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
472 additions
and
30 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package azureexporter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" | ||
"github.com/thomaspoignant/go-feature-flag/exporter" | ||
"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter" | ||
"github.com/thomaspoignant/go-feature-flag/utils/fflog" | ||
) | ||
|
||
type Exporter struct { | ||
// Container is the name of your Azure Blob Storage Container similar to Buckets in S3. | ||
Container string | ||
|
||
// Storage Account Name and Key | ||
AccountName string | ||
AccountKey string | ||
|
||
Format string | ||
Path string | ||
Filename string | ||
CsvTemplate string | ||
ParquetCompressionCodec string | ||
} | ||
|
||
func (f *Exporter) initializeAzureClient() (*azblob.Client, error) { | ||
url := fmt.Sprintf("https://%s.blob.core.windows.net/", f.AccountName) | ||
|
||
if f.AccountKey == "" { | ||
cred, err := azidentity.NewDefaultAzureCredential(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return azblob.NewClient(url, cred, nil) | ||
} | ||
cred, err := azblob.NewSharedKeyCredential(f.AccountName, f.AccountKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return azblob.NewClientWithSharedKeyCredential(url, cred, nil) | ||
} | ||
|
||
func (f *Exporter) Export(ctx context.Context, logger *fflog.FFLogger, featureEvents []exporter.FeatureEvent) error { | ||
client, err := f.initializeAzureClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if f.Container == "" { | ||
return fmt.Errorf("you should specify a container. %v is invalid", f.Container) | ||
} | ||
|
||
outputDir, err := os.MkdirTemp("", "go_feature_flag_AzureBlobStorage_export") | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { _ = os.Remove(outputDir) }() | ||
|
||
fileExporter := fileexporter.Exporter{ | ||
Format: f.Format, | ||
OutputDir: outputDir, | ||
Filename: f.Filename, | ||
CsvTemplate: f.CsvTemplate, | ||
ParquetCompressionCodec: f.ParquetCompressionCodec, | ||
} | ||
err = fileExporter.Export(ctx, logger, featureEvents) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
files, err := os.ReadDir(outputDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range files { | ||
fileName := file.Name() | ||
of, err := os.Open(outputDir + fileName) | ||
if err != nil { | ||
logger.Error("[Azure Exporter] impossible to open file", slog.String("path", outputDir+"/"+fileName)) | ||
continue | ||
} | ||
defer func() { _ = of.Close() }() | ||
|
||
// prepend the path | ||
source := fileName | ||
if f.Path != "" { | ||
source = f.Path + "/" + fileName | ||
} | ||
|
||
_, err = client.UploadFile(context.Background(), f.Container, source, of, nil) | ||
if err != nil { | ||
logger.Error("[Azure Exporter] failed to upload file", slog.String("path", outputDir+"/"+fileName)) | ||
return err | ||
} | ||
|
||
logger.Info("[Azure Exporter] file uploaded.", slog.String("location", f.Container+"/"+fileName)) | ||
} | ||
return nil | ||
} | ||
|
||
func (f *Exporter) IsBulk() bool { | ||
return true | ||
} |
Oops, something went wrong.