From 4f3162e8d8d92120bd1b1c293ab724cf9afcefbe Mon Sep 17 00:00:00 2001 From: Tamer Sherif Date: Tue, 25 Apr 2023 14:41:21 -0700 Subject: [PATCH 1/8] added files samples --- sdk/storage/azfile/file/examples_test.go | 281 +++++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 sdk/storage/azfile/file/examples_test.go diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go new file mode 100644 index 000000000000..0d613df76cc5 --- /dev/null +++ b/sdk/storage/azfile/file/examples_test.go @@ -0,0 +1,281 @@ +//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 file_test + +import ( + "bytes" + "context" + "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/file" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share" + "io" + "log" + "os" + "strings" +) + +func handleError(err error) { + if err != nil { + log.Fatal(err.Error()) + } +} + +const random64BString string = "2SDgZj6RkKYzJpu04sweQek4uWHO8ndPnYlZ0tnFS61hjnFZ5IkvIGGY44eKABov" + +func generateData(sizeInBytes int) (io.ReadSeekCloser, []byte) { + data := make([]byte, sizeInBytes) + _len := len(random64BString) + if sizeInBytes > _len { + count := sizeInBytes / _len + if sizeInBytes%_len != 0 { + count = count + 1 + } + copy(data[:], strings.Repeat(random64BString, count)) + } else { + copy(data[:], random64BString) + } + return streaming.NopCloser(bytes.NewReader(data)), data +} + +func Example_client_NewClient_CreateShare_CreateDir_CreateFile() { + 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) + + client, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil) + handleError(err) + + shareClient := client.NewShareClient("testShare") + fmt.Println(shareClient.URL()) + + dirClient := shareClient.NewDirectoryClient("testDir") + fmt.Println(dirClient.URL()) + + fileClient := dirClient.NewFileClient("testFile") + fmt.Println(fileClient.URL()) + +} + +func Example_directory_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" + filePath := "testDir/testFile" + fileClient, err := file.NewClientFromConnectionString(connectionString, shareName, filePath, nil) + handleError(err) + fmt.Println(fileClient.URL()) +} + +func Example_fileClient_CreateAndDelete() { + // 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" + fileName := "testFile" + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + fileClient := shareClient.NewRootDirectoryClient().NewFileClient(fileName) + _, err = fileClient.Create(context.Background(), 5, nil) + handleError(err) + + _, err = fileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_GetProperties() { + // 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" + fileName := "testFile" + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + fileClient := shareClient.NewRootDirectoryClient().NewFileClient(fileName) + _, err = fileClient.Create(context.Background(), 5, nil) + handleError(err) + + _, err = fileClient.GetProperties(context.Background(), nil) + handleError(err) + + _, err = fileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) + +} + +func Example_fileClient_SetAndGetMetadata() { + // 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" + fileName := "testFile" + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + fileClient := shareClient.NewRootDirectoryClient().NewFileClient(fileName) + _, err = fileClient.Create(context.Background(), 5, nil) + handleError(err) + + opts := file.SetMetadataOptions{Metadata: map[string]*string{"hello": to.Ptr("world")}} + _, err = fileClient.SetMetadata(context.Background(), &opts) + handleError(err) + + get, err := fileClient.GetProperties(context.Background(), nil) + handleError(err) + + if get.Metadata == nil { + log.Fatal("No metadata returned") + } + for k, v := range get.Metadata { + fmt.Print(k + "=" + *v + "\n") + } + + _, err = fileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_UploadBuffer() { + // 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" + fileName := "testFile" + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + fileClient := shareClient.NewRootDirectoryClient().NewFileClient(fileName) + _, err = fileClient.Create(context.Background(), 5, nil) + handleError(err) + + data := []byte{'h', 'e', 'l', 'l', 'o'} + err = fileClient.UploadBuffer(context.Background(), data, nil) + handleError(err) + + _, err = fileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_UploadStream() { + // 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" + fileName := "testFile" + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + fileClient := shareClient.NewRootDirectoryClient().NewFileClient(fileName) + _, err = fileClient.Create(context.Background(), 5, nil) + handleError(err) + + err = fileClient.UploadStream( + context.TODO(), + streaming.NopCloser(strings.NewReader("Some text")), + nil, + ) + handleError(err) + + _, err = fileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_UploadAndClearRange() { + // 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" + fileName := "testFile" + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + fileClient := shareClient.NewRootDirectoryClient().NewFileClient(fileName) + _, err = fileClient.Create(context.Background(), 5, nil) + handleError(err) + + contentR, _ := generateData(5) + + _, err = fileClient.UploadRange(context.Background(), 0, contentR, nil) + handleError(err) + + rangeList, err := fileClient.GetRangeList(context.Background(), nil) + handleError(err) + fmt.Println(rangeList.Ranges) + + _, err = fileClient.ClearRange(context.Background(), file.HTTPRange{Offset: 0, Count: int64(5)}, nil) + handleError(err) + + rangeList2, err := fileClient.GetRangeList(context.Background(), nil) + handleError(err) + + fmt.Println(rangeList2.Ranges, 0) + _, err = fileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} From a1e7ffc7a21403b90aaf4a967e011b5537e57051 Mon Sep 17 00:00:00 2001 From: Tamer Sherif <69483382+tasherif-msft@users.noreply.github.com> Date: Tue, 2 May 2023 16:02:30 -0700 Subject: [PATCH 2/8] added more samples --- sdk/storage/azfile/file/examples_test.go | 272 ++++++++++++++++++++++- 1 file changed, 271 insertions(+), 1 deletion(-) diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go index 0d613df76cc5..d0effc195f4b 100644 --- a/sdk/storage/azfile/file/examples_test.go +++ b/sdk/storage/azfile/file/examples_test.go @@ -13,12 +13,16 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming" "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/file" + "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" "io" + "io/ioutil" "log" + "math/rand" "os" "strings" + "time" ) func handleError(err error) { @@ -72,7 +76,7 @@ func Example_client_NewClient_CreateShare_CreateDir_CreateFile() { } -func Example_directory_NewClientFromConnectionString() { +func Example_file_NewClientFromConnectionString() { // Your connection string can be obtained from the Azure Portal. connectionString, ok := os.LookupEnv("AZURE_STORAGE_CONNECTION_STRING") if !ok { @@ -279,3 +283,269 @@ func Example_fileClient_UploadAndClearRange() { _, err = shareClient.Delete(context.Background(), nil) handleError(err) } + +func Example_fileClient_StartCopyFromURL() { + // 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" + srcFileName := "testFile" + dstFileName := "testFile2" + fileSize := int64(5) + + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + dstFileClient := shareClient.NewRootDirectoryClient().NewFileClient(dstFileName) + + contentR, _ := generateData(int(fileSize)) + + _, err = srcFileClient.UploadRange(context.Background(), 0, contentR, nil) + handleError(err) + + _, err = dstFileClient.StartCopyFromURL(context.Background(), srcFileClient.URL(), nil) + handleError(err) + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = dstFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_DownloadStream() { + // 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" + srcFileName := "testFile" + fileSize := int64(5) + + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + contentR, _ := generateData(int(fileSize)) + + _, err = srcFileClient.UploadRange(context.Background(), 0, contentR, nil) + handleError(err) + + // validate data copied + resp, err := srcFileClient.DownloadStream(context.Background(), &file.DownloadStreamOptions{ + Range: file.HTTPRange{Offset: 0, Count: fileSize}, + }) + handleError(err) + + content1, err := io.ReadAll(resp.Body) + handleError(err) + fmt.Println(content1) + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_DownloadBuffer() { + // 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" + srcFileName := "testFile" + fileSize := int64(5) + + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + content := make([]byte, fileSize) + _, err = rand.Read(content) + handleError(err) + + err = srcFileClient.UploadBuffer(context.Background(), content, nil) + handleError(err) + + destBuffer := make([]byte, fileSize) + _, err = srcFileClient.DownloadBuffer(context.Background(), destBuffer, &file.DownloadBufferOptions{ + ChunkSize: 10 * 1024 * 1024, + Concurrency: 5, + }) + handleError(err) + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_DownloadFile() { + // 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" + srcFileName := "testFile" + fileSize := int64(5) + + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + content := make([]byte, fileSize) + _, err = rand.Read(content) + handleError(err) + + err = srcFileClient.UploadBuffer(context.Background(), content, nil) + handleError(err) + + destFileName := "file.bin" + destFile, err := os.Create(destFileName) + handleError(err) + defer func(name string) { + err = os.Remove(name) + handleError(err) + }(destFileName) + defer func(destFile *os.File) { + err = destFile.Close() + handleError(err) + }(destFile) + + _, err = srcFileClient.DownloadFile(context.Background(), destFile, nil) + handleError(err) + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_UploadFile() { + // 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" + srcFileName := "testFile" + fileSize := int64(5) + + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + _, content := generateData(int(fileSize)) + err = ioutil.WriteFile(srcFileName, content, 0644) + handleError(err) + defer func() { + err = os.Remove(srcFileName) + handleError(err) + }() + fh, err := os.Open(srcFileName) + handleError(err) + defer func(fh *os.File) { + err := fh.Close() + handleError(err) + }(fh) + + err = srcFileClient.UploadFile(context.Background(), fh, nil) + + destFileName := "file.bin" + destFile, err := os.Create(destFileName) + handleError(err) + defer func(name string) { + err = os.Remove(name) + handleError(err) + }(destFileName) + defer func(destFile *os.File) { + err = destFile.Close() + handleError(err) + }(destFile) + + _, err = srcFileClient.DownloadFile(context.Background(), destFile, nil) + handleError(err) + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_file_ClientGetSASURL() { + // 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" + srcFileName := "testFile" + fileSize := int64(5) + + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + permission := sas.FilePermissions{Read: true} + start := time.Now() + expiry := start.AddDate(1, 0, 0) + options := file.GetSASURLOptions{StartTime: &start} + sasURL, err := srcFileClient.GetSASURL(permission, expiry, &options) + handleError(err) + _ = sasURL + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} From 6dd02f392417fe87d26ea2b974fe0c3f908cf971 Mon Sep 17 00:00:00 2001 From: Tamer Sherif <69483382+tasherif-msft@users.noreply.github.com> Date: Tue, 2 May 2023 16:32:53 -0700 Subject: [PATCH 3/8] changed to crypto --- sdk/storage/azfile/file/examples_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go index d0effc195f4b..9748227676a5 100644 --- a/sdk/storage/azfile/file/examples_test.go +++ b/sdk/storage/azfile/file/examples_test.go @@ -19,7 +19,7 @@ import ( "io" "io/ioutil" "log" - "math/rand" + "crypto/rand" "os" "strings" "time" From de232dc57b294cc5c35c31310c4e03df4ffff24b Mon Sep 17 00:00:00 2001 From: Tamer Sherif <69483382+tasherif-msft@users.noreply.github.com> Date: Tue, 2 May 2023 20:42:11 -0700 Subject: [PATCH 4/8] added final two samples --- sdk/storage/azfile/file/examples_test.go | 119 ++++++++++++++++++++++- 1 file changed, 114 insertions(+), 5 deletions(-) diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go index 9748227676a5..8013cd2ee9c8 100644 --- a/sdk/storage/azfile/file/examples_test.go +++ b/sdk/storage/azfile/file/examples_test.go @@ -9,6 +9,7 @@ package file_test import ( "bytes" "context" + "crypto/rand" "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming" "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" @@ -19,7 +20,6 @@ import ( "io" "io/ioutil" "log" - "crypto/rand" "os" "strings" "time" @@ -312,6 +312,7 @@ func Example_fileClient_StartCopyFromURL() { _, err = srcFileClient.UploadRange(context.Background(), 0, contentR, nil) handleError(err) + // you can also use AbortCopy to abort copying _, err = dstFileClient.StartCopyFromURL(context.Background(), srcFileClient.URL(), nil) handleError(err) @@ -395,10 +396,7 @@ func Example_fileClient_DownloadBuffer() { handleError(err) destBuffer := make([]byte, fileSize) - _, err = srcFileClient.DownloadBuffer(context.Background(), destBuffer, &file.DownloadBufferOptions{ - ChunkSize: 10 * 1024 * 1024, - Concurrency: 5, - }) + _, err = srcFileClient.DownloadBuffer(context.Background(), destBuffer, nil) handleError(err) _, err = srcFileClient.Delete(context.Background(), nil) @@ -549,3 +547,114 @@ func Example_file_ClientGetSASURL() { _, err = shareClient.Delete(context.Background(), nil) handleError(err) } + +func Example_fileClient_Resize() { + // 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" + srcFileName := "testFile" + fileSize := int64(5) + + shareClient, err := share.NewClientFromConnectionString(connectionString, shareName, nil) + handleError(err) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + resp1, err := srcFileClient.GetProperties(context.Background(), nil) + handleError(err) + fmt.Println(*resp1.ContentLength) + + _, err = srcFileClient.Resize(context.Background(), 6, nil) + handleError(err) + + resp1, err = srcFileClient.GetProperties(context.Background(), nil) + handleError(err) + fmt.Println(*resp1.ContentLength) + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} + +func Example_fileClient_UploadRangeFromURL() { + 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) + + client, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil) + handleError(err) + + shareClient := client.NewShareClient("testShare") + shareName := "testShare" + srcFileName := "testFile" + dstFileName := "testFile2" + fileSize := int64(5) + + _, err = shareClient.Create(context.Background(), nil) + handleError(err) + + srcFileClient := shareClient.NewRootDirectoryClient().NewFileClient(srcFileName) + _, err = srcFileClient.Create(context.Background(), fileSize, nil) + handleError(err) + + dstFileClient := shareClient.NewRootDirectoryClient().NewFileClient(dstFileName) + + contentR, _ := generateData(int(fileSize)) + + _, err = srcFileClient.UploadRange(context.Background(), 0, contentR, nil) + handleError(err) + + contentSize := 1024 * 8 // 8KB + content := make([]byte, contentSize) + body := bytes.NewReader(content) + rsc := streaming.NopCloser(body) + + _, err = srcFileClient.UploadRange(context.Background(), 0, rsc, nil) + handleError(err) + + perms := sas.FilePermissions{Read: true, Write: true} + sasQueryParams, err := sas.SignatureValues{ + Protocol: sas.ProtocolHTTPS, // Users MUST use HTTPS (not HTTP) + ExpiryTime: time.Now().UTC().Add(48 * time.Hour), // 48-hours before expiration + ShareName: shareName, + FilePath: srcFileName, + Permissions: perms.String(), + }.SignWithSharedKey(cred) + handleError(err) + + srcFileSAS := srcFileClient.URL() + "?" + sasQueryParams.Encode() + + destFClient := shareClient.NewRootDirectoryClient().NewFileClient(dstFileName) + _, err = destFClient.Create(context.Background(), fileSize, nil) + handleError(err) + + _, err = destFClient.UploadRangeFromURL(context.Background(), srcFileSAS, 0, 0, int64(contentSize), nil) + + _, err = srcFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = dstFileClient.Delete(context.Background(), nil) + handleError(err) + + _, err = shareClient.Delete(context.Background(), nil) + handleError(err) +} From cbfd444067ef1fe242b743666f65b925e28dc77b Mon Sep 17 00:00:00 2001 From: Tamer Sherif <69483382+tasherif-msft@users.noreply.github.com> Date: Tue, 2 May 2023 20:44:07 -0700 Subject: [PATCH 5/8] small fix --- sdk/storage/azfile/file/examples_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go index 8013cd2ee9c8..5b60d497a9ce 100644 --- a/sdk/storage/azfile/file/examples_test.go +++ b/sdk/storage/azfile/file/examples_test.go @@ -603,12 +603,12 @@ func Example_fileClient_UploadRangeFromURL() { client, err := service.NewClientWithSharedKeyCredential(serviceURL, cred, nil) handleError(err) - shareClient := client.NewShareClient("testShare") shareName := "testShare" srcFileName := "testFile" dstFileName := "testFile2" fileSize := int64(5) + shareClient := client.NewShareClient(shareName) _, err = shareClient.Create(context.Background(), nil) handleError(err) From 2b503d60ce3ee537aa97dbb9620496e326a165d1 Mon Sep 17 00:00:00 2001 From: Tamer Sherif <69483382+tasherif-msft@users.noreply.github.com> Date: Tue, 2 May 2023 20:58:21 -0700 Subject: [PATCH 6/8] err handle --- sdk/storage/azfile/file/examples_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go index 5b60d497a9ce..547144f5851f 100644 --- a/sdk/storage/azfile/file/examples_test.go +++ b/sdk/storage/azfile/file/examples_test.go @@ -648,7 +648,8 @@ func Example_fileClient_UploadRangeFromURL() { handleError(err) _, err = destFClient.UploadRangeFromURL(context.Background(), srcFileSAS, 0, 0, int64(contentSize), nil) - + handleError(err) + _, err = srcFileClient.Delete(context.Background(), nil) handleError(err) From 8619c0e736d62966bbc45c6ea4922453ecde7af1 Mon Sep 17 00:00:00 2001 From: Tamer Sherif <69483382+tasherif-msft@users.noreply.github.com> Date: Tue, 2 May 2023 21:15:42 -0700 Subject: [PATCH 7/8] lint fix --- sdk/storage/azfile/file/examples_test.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go index 547144f5851f..fb6742829587 100644 --- a/sdk/storage/azfile/file/examples_test.go +++ b/sdk/storage/azfile/file/examples_test.go @@ -649,13 +649,4 @@ func Example_fileClient_UploadRangeFromURL() { _, err = destFClient.UploadRangeFromURL(context.Background(), srcFileSAS, 0, 0, int64(contentSize), nil) handleError(err) - - _, err = srcFileClient.Delete(context.Background(), nil) - handleError(err) - - _, err = dstFileClient.Delete(context.Background(), nil) - handleError(err) - - _, err = shareClient.Delete(context.Background(), nil) - handleError(err) } From 26ffccddd01da016ad51277608e89aabc6b5c079 Mon Sep 17 00:00:00 2001 From: Tamer Sherif <69483382+tasherif-msft@users.noreply.github.com> Date: Tue, 2 May 2023 21:25:17 -0700 Subject: [PATCH 8/8] fix ci --- sdk/storage/azfile/file/examples_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/sdk/storage/azfile/file/examples_test.go b/sdk/storage/azfile/file/examples_test.go index fb6742829587..b54aa5aab215 100644 --- a/sdk/storage/azfile/file/examples_test.go +++ b/sdk/storage/azfile/file/examples_test.go @@ -616,8 +616,6 @@ func Example_fileClient_UploadRangeFromURL() { _, err = srcFileClient.Create(context.Background(), fileSize, nil) handleError(err) - dstFileClient := shareClient.NewRootDirectoryClient().NewFileClient(dstFileName) - contentR, _ := generateData(int(fileSize)) _, err = srcFileClient.UploadRange(context.Background(), 0, contentR, nil)