<commit_before>/*
Copyright 2020 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package azblobbackupstorage implements the BackupStorage interface
// for Azure Blob Storage
package azblobbackupstorage

import (
	"context"
	"flag"
	"fmt"
	"io"
	"net/url"
	"os"
	"strings"
	"sync"
	"time"

	"github.com/Azure/azure-storage-blob-go/azblob"
	"vitess.io/vitess/go/vt/concurrency"
	"vitess.io/vitess/go/vt/log"
	"vitess.io/vitess/go/vt/mysqlctl/backupstorage"
)

var (
	// This is the account name
	accountName = flag.String("azblob_backup_account_name", "", "Azure Storage account name for backups, if this flag is unset the environment paramater VITESS_AZBLOB_ACCOUNT_NAME will be used")

	// This is the private access key
	accountKey = flag.String("azblob_backup_account_key", "", "Azure Storage account key,if this flag is unset the environment paramater VITESS_AZBLOB_ACCOUNT_KEY will be used")

	// This is the name of the container that will store the backups
	containerName = flag.String("azblob_backup_container_name", "", "Azure Blob Container Name")

	// This is an optional previx to prepend to all files
	storageRoot = flag.String("azblob_backup_storage_root", "", "Azure Blob storage root")

	azBlobParallelism = flag.Int("azblob_backup_parallelism", 1, "Azure blob operation parallelism (requires extra memory when increased)")
)

const (
	defaultRetryCount = 5
	delimiter         = "/"
)

// Return a Shared credential from the available credential sources.
// We will use credentials in the following order
// 1. Direct Command Line Flag (azblob_backup_account_name, azblob_backup_account_key)
// 2. Environment variables
func azCredentials() (*azblob.SharedKeyCredential, error) {
	actName := *accountName
	if len(actName) == 0 {
		// Check the Environmental Value
		actName = os.Getenv("VITESS_AZBLOB_ACCOUNT_NAME")
	}

	actKey := *accountKey
	if len(actKey) == 0 {
		actKey = os.Getenv("VITESS_AZBLOB_ACCOUNT_KEY")
	}

	if len(actName) == 0 || len(actKey) == 0 {
		return nil, fmt.Errorf("can not get Azure Storage account credentials from CLI or Environment variables")
	}
	return azblob.NewSharedKeyCredential(*accountName, *accountKey)
}<commit_msg>Moving accuont key to a file<commit_after>/*
Copyright 2020 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package azblobbackupstorage implements the BackupStorage interface
// for Azure Blob Storage
package azblobbackupstorage

import (
	"context"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"net/url"
	"os"
	"strings"
	"sync"
	"time"

	"github.com/Azure/azure-storage-blob-go/azblob"
	"vitess.io/vitess/go/vt/concurrency"
	"vitess.io/vitess/go/vt/log"
	"vitess.io/vitess/go/vt/mysqlctl/backupstorage"
)

var (
	// This is the account name
	accountName = flag.String("azblob_backup_account_name", "", "Azure Storage account name for backups, if this flag is unset the environment paramater VITESS_AZBLOB_ACCOUNT_NAME will be used")

	// This is the private access key
	accountKeyFile = flag.String("azblob_backup_account_key_file", "", "A file containing the Azure Storage account key,if this flag is unset the environment paramater VITESS_AZBLOB_ACCOUNT_KEY will be used")

	// This is the name of the container that will store the backups
	containerName = flag.String("azblob_backup_container_name", "", "Azure Blob Container Name")

	// This is an optional previx to prepend to all files
	storageRoot = flag.String("azblob_backup_storage_root", "", "Azure Blob storage root")

	azBlobParallelism = flag.Int("azblob_backup_parallelism", 1, "Azure blob operation parallelism (requires extra memory when increased)")
)

const (
	defaultRetryCount = 5
	delimiter         = "/"
)

// Return a Shared credential from the available credential sources.
// We will use credentials in the following order
// 1. Direct Command Line Flag (azblob_backup_account_name, azblob_backup_account_key)
// 2. Environment variables
func azCredentials() (*azblob.SharedKeyCredential, error) {
	actName := *accountName
	if len(actName) == 0 {
		// Check the Environmental Value
		actName = os.Getenv("VITESS_AZBLOB_ACCOUNT_NAME")
	}

	var actKey string
	if len(*accountKeyFile) > 0 {
		if dat, err := ioutil.ReadFile(*accountKeyFile); err != nil {
			actKey = string(dat)
		}
	} else {
		actKey = os.Getenv("VITESS_AZBLOB_ACCOUNT_KEY")
	}

	if len(actName) == 0 || len(actKey) == 0 {
		return nil, fmt.Errorf("can not get Azure Storage account credentials from CLI or Environment variables")
	}
	return azblob.NewSharedKeyCredential(actName, actKey)
}
{'fraction_non_alphanumeric': 0.09219858156028368, 'fraction_numerical': 0.005819239861793053, 'mean_word_length': 4.635245901639344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6071821', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1665, 'n_words': 580}