service

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 15 Imported by: 8

Documentation

Overview

Example (Service_Client_CreateShare)
package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	shareName := "testShare"
	_, err = svcClient.CreateShare(context.TODO(), shareName, nil)
	handleError(err)
	fmt.Println("Share created")
}
Output:

Example (Service_Client_DeleteShare)
package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	shareName := "testShare"
	_, err = svcClient.DeleteShare(context.TODO(), shareName, nil)
	handleError(err)
	fmt.Println("Share deleted")
}
Output:

Example (Service_Client_GetProperties)
package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	_, err = svcClient.GetProperties(context.TODO(), nil)
	handleError(err)
}
Output:

Example (Service_Client_GetSASURL)
package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/sas"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
	"time"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	resources := sas.AccountResourceTypes{
		Object:    true,
		Service:   true,
		Container: true,
	}
	permissions := sas.AccountPermissions{
		Read:   true,
		Write:  true,
		Delete: true,
		List:   true,
		Create: true,
	}
	expiry := time.Now().Add(time.Hour)
	sasUrl, err := svcClient.GetSASURL(resources, permissions, expiry, nil)
	handleError(err)

	fmt.Println("SAS URL: ", sasUrl)

	svcSASClient, err := service.NewClientWithNoCredential(sasUrl, nil)
	handleError(err)

	_, err = svcSASClient.GetProperties(context.TODO(), nil)
	handleError(err)
}
Output:

Example (Service_Client_ListShares)
package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	pager := svcClient.NewListSharesPager(nil)

	for pager.More() {
		resp, err := pager.NextPage(context.Background())
		handleError(err)
		for _, s := range resp.Shares {
			fmt.Println(*s.Name)
		}
	}
}
Output:

Example (Service_Client_NewClient)
package main

import (
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	fmt.Println(svcClient.URL())
}
Output:

Example (Service_Client_NewShareClient)
package main

import (
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	shareName := "testShare"
	shareClient := svcClient.NewShareClient(shareName)

	fmt.Println(shareClient.URL())
}
Output:

Example (Service_Client_RestoreShare)
package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	// get share version for restore operation
	pager := svcClient.NewListSharesPager(&service.ListSharesOptions{
		Include: service.ListSharesInclude{Deleted: true}, // Include deleted shares in the result
	})

	for pager.More() {
		resp, err := pager.NextPage(context.Background())
		handleError(err)
		for _, s := range resp.Shares {
			if s.Deleted != nil && *s.Deleted {
				_, err = svcClient.RestoreShare(context.TODO(), *s.Name, *s.Version, nil)
				handleError(err)
			}
		}
	}
}
Output:

Example (Service_Client_SetProperties)
package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
	}
	accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
	if !ok {
		panic("AZURE_STORAGE_ACCOUNT_KEY could not be found")
	}

	serviceURL := fmt.Sprintf("https://%s.file.core.windows.net/", accountName)

	cred, err := service.NewSharedKeyCredential(accountName, accountKey)
	handleError(err)

	svcClient, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil)
	handleError(err)

	setPropertiesOpts := service.SetPropertiesOptions{
		HourMetrics: &service.Metrics{
			Enabled:     to.Ptr(true),
			IncludeAPIs: to.Ptr(true),
			RetentionPolicy: &service.RetentionPolicy{
				Enabled: to.Ptr(true),
				Days:    to.Ptr(int32(2)),
			},
		},
		MinuteMetrics: &service.Metrics{
			Enabled:     to.Ptr(true),
			IncludeAPIs: to.Ptr(false),
			RetentionPolicy: &service.RetentionPolicy{
				Enabled: to.Ptr(true),
				Days:    to.Ptr(int32(2)),
			},
		},
		CORS: []*service.CORSRule{
			{
				AllowedOrigins:  to.Ptr("*"),
				AllowedMethods:  to.Ptr("PUT"),
				AllowedHeaders:  to.Ptr("x-ms-client-request-id"),
				ExposedHeaders:  to.Ptr("x-ms-*"),
				MaxAgeInSeconds: to.Ptr(int32(2)),
			},
		},
	}
	_, err = svcClient.SetProperties(context.TODO(), &setPropertiesOpts)
	handleError(err)
}
Output:

Example (Service_NewClientFromConnectionString)
package main

import (
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
	"log"
	"os"
)

func handleError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}

func main() {
	// Your connection string can be obtained from the Azure Portal.
	connectionString, ok := os.LookupEnv("AZURE_STORAGE_CONNECTION_STRING")
	if !ok {
		log.Fatal("the environment variable 'AZURE_STORAGE_CONNECTION_STRING' could not be found")
	}

	svcClient, err := service.NewClientFromConnectionString(connectionString, nil)
	handleError(err)

	fmt.Println(svcClient.URL())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CORSRule

type CORSRule = generated.CORSRule

CORSRule - CORS is an HTTP feature that enables a web application running under one domain to access resources in another domain. Web browsers implement a security restriction known as same-origin policy that prevents a web page from calling APIs in a different domain; CORS provides a secure way to allow one domain (the origin domain) to call APIs in another domain.

type Client

Client represents a URL to the Azure File Storage service allowing you to manipulate file shares.

func NewClient added in v1.1.0

func NewClient(serviceURL string, cred azcore.TokenCredential, options *ClientOptions) (*Client, error)

NewClient creates an instance of Client with the specified values.

  • serviceURL - the URL of the storage account e.g. https://<account>.file.core.windows.net/
  • cred - an Azure AD credential, typically obtained via the azidentity module
  • options - client options; pass nil to accept the default values

Note that service-level operations do not support token credential authentication. This constructor exists to allow the construction of a share.Client that has token credential authentication. Also note that ClientOptions.FileRequestIntent is currently required for token authentication.

func NewClientFromConnectionString

func NewClientFromConnectionString(connectionString string, options *ClientOptions) (*Client, error)

NewClientFromConnectionString creates an instance of Client with the specified values.

  • connectionString - a connection string for the desired storage account
  • options - client options; pass nil to accept the default values

func NewClientWithNoCredential

func NewClientWithNoCredential(serviceURL string, options *ClientOptions) (*Client, error)

NewClientWithNoCredential creates an instance of Client with the specified values. This is used to anonymously access a storage account or with a shared access signature (SAS) token.

  • serviceURL - the URL of the storage account e.g. https://<account>.file.core.windows.net/?<sas token>
  • options - client options; pass nil to accept the default values

func NewClientWithSharedKeyCredential

func NewClientWithSharedKeyCredential(serviceURL string, cred *SharedKeyCredential, options *ClientOptions) (*Client, error)

NewClientWithSharedKeyCredential creates an instance of Client with the specified values.

  • serviceURL - the URL of the storage account e.g. https://<account>.file.core.windows.net/
  • cred - a SharedKeyCredential created with the matching storage account and access key
  • options - client options; pass nil to accept the default values

func (*Client) CreateShare

func (s *Client) CreateShare(ctx context.Context, shareName string, options *CreateShareOptions) (CreateShareResponse, error)

CreateShare is a lifecycle method to creates a new share under the specified account. If the share with the same name already exists, a ResourceExistsError will be raised. This method returns a client with which to interact with the newly created share. For more information see, https://learn.microsoft.com/en-us/rest/api/storageservices/create-share.

func (*Client) DeleteShare

func (s *Client) DeleteShare(ctx context.Context, shareName string, options *DeleteShareOptions) (DeleteShareResponse, error)

DeleteShare is a lifecycle method that marks the specified share for deletion. The share and any files contained within it are later deleted during garbage collection. If the share is not found, a ResourceNotFoundError will be raised. For more information see, https://learn.microsoft.com/en-us/rest/api/storageservices/delete-share.

func (*Client) GetProperties

func (s *Client) GetProperties(ctx context.Context, options *GetPropertiesOptions) (GetPropertiesResponse, error)

GetProperties operation gets the properties of a storage account's File service. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/get-file-service-properties.

func (*Client) GetSASURL

func (s *Client) GetSASURL(resources sas.AccountResourceTypes, permissions sas.AccountPermissions, expiry time.Time, o *GetSASURLOptions) (string, error)

GetSASURL is a convenience method for generating a SAS token for the currently pointed at account. It can only be used if the credential supplied during creation was a SharedKeyCredential.

func (*Client) NewListSharesPager

func (s *Client) NewListSharesPager(options *ListSharesOptions) *runtime.Pager[ListSharesSegmentResponse]

NewListSharesPager operation returns a pager of the shares under the specified account. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/list-shares

func (*Client) NewShareClient

func (s *Client) NewShareClient(shareName string) *share.Client

NewShareClient creates a new share.Client object by concatenating shareName to the end of this Client's URL. The new share.Client uses the same request policy pipeline as the Client.

func (*Client) RestoreShare

func (s *Client) RestoreShare(ctx context.Context, deletedShareName string, deletedShareVersion string, options *RestoreShareOptions) (RestoreShareResponse, error)

RestoreShare restores soft-deleted share. Operation will only be successful if used within the specified number of days set in the delete retention policy. For more information see, https://learn.microsoft.com/en-us/rest/api/storageservices/restore-share.

func (*Client) SetProperties

func (s *Client) SetProperties(ctx context.Context, options *SetPropertiesOptions) (SetPropertiesResponse, error)

SetProperties operation sets properties for a storage account's File service endpoint. For more information, see https://learn.microsoft.com/en-us/rest/api/storageservices/set-file-service-properties.

func (*Client) URL

func (s *Client) URL() string

URL returns the URL endpoint used by the Client object.

type ClientOptions

type ClientOptions base.ClientOptions

ClientOptions contains the optional parameters when creating a Client.

type CreateShareOptions

type CreateShareOptions = share.CreateOptions

CreateShareOptions contains the optional parameters for the share.Client.Create method.

type CreateShareResponse

type CreateShareResponse = generated.ShareClientCreateResponse

CreateShareResponse contains the response from method share.Client.Create.

type DeleteShareOptions

type DeleteShareOptions = share.DeleteOptions

DeleteShareOptions contains the optional parameters for the share.Client.Delete method.

type DeleteShareResponse

type DeleteShareResponse = generated.ShareClientDeleteResponse

DeleteShareResponse contains the response from method share.Client.Delete.

type GetPropertiesOptions

type GetPropertiesOptions struct {
}

GetPropertiesOptions provides set of options for Client.GetProperties

type GetPropertiesResponse

type GetPropertiesResponse = generated.ServiceClientGetPropertiesResponse

GetPropertiesResponse contains the response from method Client.GetProperties.

type GetSASURLOptions

type GetSASURLOptions struct {
	StartTime *time.Time
}

GetSASURLOptions contains the optional parameters for the Client.GetSASURL method.

type ListSharesInclude

type ListSharesInclude struct {
	// Tells the service whether to return metadata for each share.
	Metadata bool

	// Tells the service whether to return soft-deleted shares.
	Deleted bool

	// Tells the service whether to return share snapshots.
	Snapshots bool
}

ListSharesInclude indicates what additional information the service should return with each share.

type ListSharesIncludeType

type ListSharesIncludeType = generated.ListSharesIncludeType

ListSharesIncludeType defines values for ListSharesIncludeType

func PossibleListSharesIncludeTypeValues

func PossibleListSharesIncludeTypeValues() []ListSharesIncludeType

PossibleListSharesIncludeTypeValues returns the possible values for the ListSharesIncludeType const type.

type ListSharesOptions

type ListSharesOptions struct {
	// Include this parameter to specify one or more datasets to include in the responseBody.
	Include ListSharesInclude

	// A string value that identifies the portion of the list to be returned with the next list operation. The operation returns
	// a marker value within the responseBody body if the list returned was not complete.
	// The marker value may then be used in a subsequent call to request the next set of list items. The marker value is opaque
	// to the client.
	Marker *string

	// Specifies the maximum number of entries to return. If the request does not specify maxresults, or specifies a value greater
	// than 5,000, the server will return up to 5,000 items.
	MaxResults *int32

	// Filters the results to return only entries whose name begins with the specified prefix.
	Prefix *string
}

ListSharesOptions contains the optional parameters for the Client.NewListSharesPager method.

type ListSharesResponse

type ListSharesResponse = generated.ListSharesResponse

ListSharesResponse - An enumeration of shares.

type ListSharesSegmentResponse

type ListSharesSegmentResponse = generated.ServiceClientListSharesSegmentResponse

ListSharesSegmentResponse contains the response from method Client.NewListSharesPager.

type Metrics

type Metrics = generated.Metrics

Metrics - Storage Analytics metrics for file service.

type ProtocolSettings

type ProtocolSettings = generated.ProtocolSettings

ProtocolSettings - Protocol settings

type RestoreShareOptions

type RestoreShareOptions = share.RestoreOptions

RestoreShareOptions contains the optional parameters for the share.Client.Restore method.

type RestoreShareResponse

type RestoreShareResponse = generated.ShareClientRestoreResponse

RestoreShareResponse contains the response from method share.Client.Restore.

type RetentionPolicy

type RetentionPolicy = generated.RetentionPolicy

RetentionPolicy - The retention policy.

type SMBMultichannel

type SMBMultichannel = generated.SMBMultichannel

SMBMultichannel - Settings for SMB multichannel

type SMBSettings

type SMBSettings = generated.SMBSettings

SMBSettings - Settings for SMB protocol.

type SetPropertiesOptions

type SetPropertiesOptions struct {
	// The set of CORS rules.
	CORS []*CORSRule

	// A summary of request statistics grouped by API in hourly aggregates for files.
	HourMetrics *Metrics

	// A summary of request statistics grouped by API in minute aggregates for files.
	MinuteMetrics *Metrics

	// Protocol settings
	Protocol *ProtocolSettings
}

SetPropertiesOptions provides set of options for Client.SetProperties

type SetPropertiesResponse

type SetPropertiesResponse = generated.ServiceClientSetPropertiesResponse

SetPropertiesResponse contains the response from method Client.SetProperties.

type Share

type Share = generated.Share

Share - A listed Azure Storage share item.

type ShareProperties

type ShareProperties = generated.ShareProperties

ShareProperties - Properties of a share.

type ShareRootSquash

type ShareRootSquash = generated.ShareRootSquash

ShareRootSquash defines values for the root squashing behavior on the share when NFS is enabled. If it's not specified, the default is NoRootSquash.

func PossibleShareRootSquashValues

func PossibleShareRootSquashValues() []ShareRootSquash

PossibleShareRootSquashValues returns the possible values for the RootSquash const type.

type ShareTokenIntent added in v1.1.0

type ShareTokenIntent = generated.ShareTokenIntent

ShareTokenIntent is required if authorization header specifies an OAuth token.

const (
	ShareTokenIntentBackup ShareTokenIntent = generated.ShareTokenIntentBackup
)

func PossibleShareTokenIntentValues added in v1.1.0

func PossibleShareTokenIntentValues() []ShareTokenIntent

PossibleShareTokenIntentValues returns the possible values for the ShareTokenIntent const type.

type SharedKeyCredential

type SharedKeyCredential = exported.SharedKeyCredential

SharedKeyCredential contains an account's name and its primary or secondary key.

func NewSharedKeyCredential

func NewSharedKeyCredential(accountName, accountKey string) (*SharedKeyCredential, error)

NewSharedKeyCredential creates an immutable SharedKeyCredential containing the storage account's name and either its primary or secondary key.

type StorageServiceProperties

type StorageServiceProperties = generated.StorageServiceProperties

StorageServiceProperties - Storage service properties.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL