diff --git a/sdk/storage/azfile/file/client.go b/sdk/storage/azfile/file/client.go index fff5d75922d9..432f8ae379a1 100644 --- a/sdk/storage/azfile/file/client.go +++ b/sdk/storage/azfile/file/client.go @@ -247,7 +247,7 @@ func (f *Client) GetSASURL(permissions sas.FilePermissions, expiry time.Time, o } st := o.format() - urlParts, err := sas.ParseURL(f.URL()) + urlParts, err := ParseURL(f.URL()) if err != nil { return "", err } @@ -282,7 +282,7 @@ func (f *Client) uploadFromReader(ctx context.Context, reader io.ReaderAt, actua } if log.Should(exported.EventUpload) { - urlParts, err := sas.ParseURL(f.URL()) + urlParts, err := ParseURL(f.URL()) if err == nil { log.Writef(exported.EventUpload, "file name %s actual size %v chunk-size %v chunk-count %v", urlParts.DirectoryOrFilePath, actualSize, o.ChunkSize, ((actualSize-1)/o.ChunkSize)+1) diff --git a/sdk/storage/azfile/file/models.go b/sdk/storage/azfile/file/models.go index 4b8c528971ef..7796ea1e4dd4 100644 --- a/sdk/storage/azfile/file/models.go +++ b/sdk/storage/azfile/file/models.go @@ -13,6 +13,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/internal/exported" "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/internal/generated" "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/internal/shared" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/sas" "io" "time" ) @@ -724,3 +725,13 @@ func (u *UploadStreamOptions) getUploadRangeOptions() *UploadRangeOptions { LeaseAccessConditions: u.LeaseAccessConditions, } } + +// URLParts object represents the components that make up an Azure Storage Container/Blob URL. +// NOTE: Changing any SAS-related field requires computing a new SAS signature. +type URLParts = sas.URLParts + +// ParseURL parses a URL initializing URLParts' fields including any SAS-related & snapshot query parameters. Any other +// query parameters remain in the UnparsedParams field. This method overwrites all fields in the URLParts object. +func ParseURL(u string) (URLParts, error) { + return sas.ParseURL(u) +} diff --git a/sdk/storage/azfile/lease/examples_test.go b/sdk/storage/azfile/lease/examples_test.go new file mode 100644 index 000000000000..f8b61b3ba133 --- /dev/null +++ b/sdk/storage/azfile/lease/examples_test.go @@ -0,0 +1,101 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package lease_test + +import ( + "context" + "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/lease" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share" + "log" + "os" +) + +func handleError(err error) { + if err != nil { + log.Fatal(err.Error()) + } +} + +// This example shows how to perform various lease operations on a share. +// The same lease operations can be performed on individual files as well. +// A lease on a share prevents it from being deleted by others, while a lease on a file +// protects it from both modifications and deletions. +func Example_lease_ShareClient_AcquireLease() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + // Create a unique ID for the lease + // A lease ID can be any valid GUID string format. To generate UUIDs, consider the github.com/google/uuid package + leaseID := "36b1a876-cf98-4eb2-a5c3-6d68489658ff" + shareLeaseClient, err := lease.NewShareClient(shareClient, &lease.ShareClientOptions{LeaseID: to.Ptr(leaseID)}) + handleError(err) + + // Now acquire a lease on the share. + // You can choose to pass an empty string for proposed ID so that the service automatically assigns one for you. + duration := int32(60) + acquireLeaseResponse, err := shareLeaseClient.Acquire(context.TODO(), duration, nil) + handleError(err) + fmt.Println("The share is leased for delete operations with lease ID", *acquireLeaseResponse.LeaseID) + + // The share cannot be deleted without providing the lease ID. + _, err = shareClient.Delete(context.TODO(), nil) + if err == nil { + log.Fatal("delete should have failed") + } + + fmt.Println("The share cannot be deleted while there is an active lease") + + // share can be deleted by providing the lease id + //_, err = shareClient.Delete(context.TODO(), &share.DeleteOptions{ + // LeaseAccessConditions: &share.LeaseAccessConditions{LeaseID: acquireLeaseResponse.LeaseID}, + //}) + + // We can release the lease now and the share can be deleted. + _, err = shareLeaseClient.Release(context.TODO(), nil) + handleError(err) + fmt.Println("The lease on the share is now released") + + // AcquireLease a lease again to perform other operations. + // Duration is still 60 + acquireLeaseResponse, err = shareLeaseClient.Acquire(context.TODO(), duration, nil) + handleError(err) + fmt.Println("The share is leased again with lease ID", *acquireLeaseResponse.LeaseID) + + // We can change the ID of an existing lease. + newLeaseID := "6b3e65e5-e1bb-4a3f-8b72-13e9bc9cd3bf" + changeLeaseResponse, err := shareLeaseClient.Change(context.TODO(), newLeaseID, nil) + handleError(err) + fmt.Println("The lease ID was changed to", *changeLeaseResponse.LeaseID) + + // The lease can be renewed. + renewLeaseResponse, err := shareLeaseClient.Renew(context.TODO(), nil) + handleError(err) + fmt.Println("The lease was renewed with the same ID", *renewLeaseResponse.LeaseID) + + // Finally, the lease can be broken, and we could prevent others from acquiring a lease for a period of time + _, err = shareLeaseClient.Break(context.TODO(), &lease.ShareBreakOptions{BreakPeriod: to.Ptr(int32(60))}) + handleError(err) + fmt.Println("The lease was broken, and nobody can acquire a lease for 60 seconds") +} diff --git a/sdk/storage/azfile/service/examples_test.go b/sdk/storage/azfile/service/examples_test.go new file mode 100644 index 000000000000..bc7a2e4cd6dd --- /dev/null +++ b/sdk/storage/azfile/service/examples_test.go @@ -0,0 +1,308 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package service_test + +import ( + "context" + "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" + "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 Example_service_Client_NewClient() { + 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()) +} + +func Example_service_NewClientFromConnectionString() { + // 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()) +} + +func Example_service_Client_NewShareClient() { + 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()) +} + +func Example_service_Client_CreateShare() { + 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") +} + +func Example_service_Client_DeleteShare() { + 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") +} + +func Example_service_Client_RestoreShare() { + 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) + } + } + } +} + +func Example_service_Client_GetProperties() { + 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) +} + +func Example_service_Client_SetProperties() { + 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) +} + +func Example_service_Client_ListShares() { + 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) + } + } +} + +func Example_service_Client_GetSASURL() { + 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) +} diff --git a/sdk/storage/azfile/share/examples_test.go b/sdk/storage/azfile/share/examples_test.go new file mode 100644 index 000000000000..bb4739e9b151 --- /dev/null +++ b/sdk/storage/azfile/share/examples_test.go @@ -0,0 +1,464 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package share_test + +import ( + "context" + "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/sas" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share" + "log" + "os" + "time" +) + +func handleError(err error) { + if err != nil { + log.Fatal(err.Error()) + } +} + +func Example_share_Client_NewClient() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + fmt.Println(shareClient.URL()) +} + +func Example_share_Client_NewClientFromConnectionString() { + // 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") + } + + shareName := "testshare" + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + fmt.Println(shareClient.URL()) +} + +func Example_share_Client_NewDirectoryClient() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + dirName := "testdirectory" + dirClient := shareClient.NewDirectoryClient(dirName) + + fmt.Println(dirClient.URL()) +} + +func Example_share_Client_NewRootDirectoryClient() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + dirClient := shareClient.NewRootDirectoryClient() + + fmt.Println(dirClient.URL()) +} + +func Example_share_Client_CreateSnapshot() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + snapResp, err := shareClient.CreateSnapshot(context.TODO(), nil) + handleError(err) + shareSnapshot := *snapResp.Snapshot + + snapshotShareClient, err := shareClient.WithSnapshot(shareSnapshot) + handleError(err) + + fmt.Println(snapshotShareClient.URL()) + + _, err = snapshotShareClient.GetProperties(context.TODO(), nil) + handleError(err) +} + +func Example_share_Client_Create() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + _, err = shareClient.Create(context.TODO(), nil) + handleError(err) +} + +func Example_share_Client_Delete() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + _, err = shareClient.Delete(context.TODO(), nil) + handleError(err) +} + +func Example_share_Client_Restore() { + 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") + } + + shareName := "testshare" + 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) + + shareClient := svcClient.NewShareClient(shareName) + + // 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 = shareClient.Restore(context.TODO(), *s.Version, nil) + handleError(err) + } + } + } +} + +func Example_share_Client_GetProperties() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + _, err = shareClient.GetProperties(context.TODO(), nil) + handleError(err) +} + +func Example_share_Client_SetProperties() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + _, err = shareClient.SetProperties(context.TODO(), &share.SetPropertiesOptions{ + Quota: to.Ptr(int32(1000)), + AccessTier: to.Ptr(share.AccessTierHot), + }) + handleError(err) +} + +func Example_share_Client_AccessPolicy() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + permission := share.AccessPolicyPermission{Read: true, Write: true, Create: true, Delete: true, List: true}.String() + permissions := []*share.SignedIdentifier{ + { + ID: to.Ptr("1"), + AccessPolicy: &share.AccessPolicy{ + Start: to.Ptr(time.Now()), + Expiry: to.Ptr(time.Now().Add(time.Hour)), + Permission: &permission, + }, + }} + + _, err = shareClient.SetAccessPolicy(context.TODO(), &share.SetAccessPolicyOptions{ + ShareACL: permissions, + }) + handleError(err) + + resp, err := shareClient.GetAccessPolicy(context.TODO(), nil) + handleError(err) + + fmt.Println(*resp.SignedIdentifiers[0].AccessPolicy.Permission) +} + +func Example_share_Client_CreateGetPermission() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + testSDDL := `O:S-1-5-32-548G:S-1-5-21-397955417-626881126-188441444-512D:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)` + createResp, err := shareClient.CreatePermission(context.TODO(), testSDDL, nil) + handleError(err) + + getResp, err := shareClient.GetPermission(context.TODO(), *createResp.FilePermissionKey, nil) + handleError(err) + fmt.Println(*getResp.Permission) +} + +func Example_share_Client_SetMetadata() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + md := map[string]*string{ + "Foo": to.Ptr("FooValuE"), + "Bar": to.Ptr("bArvaLue"), + } + _, err = shareClient.SetMetadata(context.TODO(), &share.SetMetadataOptions{ + Metadata: md, + }) + handleError(err) + + resp, err := shareClient.GetProperties(context.TODO(), nil) + handleError(err) + for k, v := range resp.Metadata { + fmt.Printf("%v : %v\n", k, *v) + } +} + +func Example_share_Client_GetStatistics() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + getStats, err := shareClient.GetStatistics(context.Background(), nil) + handleError(err) + fmt.Println(*getStats.ShareUsageBytes) +} + +func Example_share_Client_GetSASURL() { + 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") + } + + shareName := "testshare" + shareURL := fmt.Sprintf("https://%s.file.core.windows.net/%s", accountName, shareName) + + cred, err := service.NewSharedKeyCredential(accountName, accountKey) + handleError(err) + + shareClient, err := share.NewClientWithSharedKeyCredential(shareURL, cred, nil) + handleError(err) + + permissions := sas.SharePermissions{ + Read: true, + Write: true, + Delete: true, + List: true, + Create: true, + } + expiry := time.Now().Add(time.Hour) + + shareSASURL, err := shareClient.GetSASURL(permissions, expiry, nil) + handleError(err) + + fmt.Println("SAS URL: ", shareSASURL) + + shareSASClient, err := share.NewClientWithNoCredential(shareSASURL, nil) + handleError(err) + + var dirs, files []string + pager := shareSASClient.NewRootDirectoryClient().NewListFilesAndDirectoriesPager(nil) + for pager.More() { + resp, err := pager.NextPage(context.Background()) + handleError(err) + + for _, d := range resp.Segment.Directories { + dirs = append(dirs, *d.Name) + } + for _, f := range resp.Segment.Files { + files = append(files, *f.Name) + } + } + + fmt.Println("Directories:") + for _, d := range dirs { + fmt.Println(d) + } + + fmt.Println("Files:") + for _, f := range files { + fmt.Println(f) + } +}