Last active
April 14, 2022 12:40
-
-
Save takekazuomi/9a00dc28133229883edbc15aca924350 to your computer and use it in GitHub Desktop.
storage access with azidentity(0.13)
This file contains hidden or 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
module webwas | |
go 1.18 | |
require ( | |
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.23.0 | |
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.14.0 | |
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.3.0 | |
github.com/gorilla/handlers v1.5.1 | |
) | |
require ( | |
github.com/Azure/azure-sdk-for-go/sdk/internal v0.9.1 // indirect | |
github.com/AzureAD/microsoft-authentication-library-for-go v0.4.0 // indirect | |
github.com/felixge/httpsnoop v1.0.1 // indirect | |
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect | |
github.com/google/uuid v1.1.1 // indirect | |
github.com/kylelemons/godebug v1.1.0 // indirect | |
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 // indirect | |
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect | |
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect | |
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect | |
golang.org/x/text v0.3.7 // indirect | |
) | |
// https://github.com/Azure/azure-sdk-for-go/issues/17472#issuecomment-1092926620 | |
replace ( | |
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.23.0 => github.com/Azure/azure-sdk-for-go/sdk/azcore v0.22.0 | |
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.14.0 => github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.13.0 | |
github.com/Azure/azure-sdk-for-go/sdk/internal v0.9.2 => github.com/Azure/azure-sdk-for-go/sdk/internal v0.9.1 | |
) |
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log" | |
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | |
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" | |
"github.com/gorilla/handlers" | |
) | |
var ( | |
host string = "" | |
port int = 5000 | |
) | |
var ( | |
subscription string = "******************************" | |
resourceGroupName string = "******************************" | |
storageAccountName string = "******************************" | |
) | |
func GetCredential() (*azidentity.DefaultAzureCredential, error) { | |
cred, err := azidentity.NewDefaultAzureCredential(nil) | |
if err != nil { | |
return nil, fmt.Errorf("azidentity.NewDefaultAzureCredential is error: %w", err) | |
} | |
return cred, nil | |
} | |
func ListContainer(ctx context.Context, cred *azidentity.DefaultAzureCredential) ([]string, error) { | |
serviceClient, err := azblob.NewServiceClient(fmt.Sprintf("https://%v.blob.core.windows.net", storageAccountName), cred, nil) | |
if err != nil { | |
return nil, err | |
} | |
pager := serviceClient.ListContainers(nil) | |
ret := []string{} | |
for pager.NextPage(ctx) { | |
resp := pager.PageResponse() | |
for _, v := range resp.ContainerItems { | |
ret = append(ret, *v.Name) | |
} | |
} | |
return ret, nil | |
} | |
func setAzLogging() { | |
azlog.SetListener(func(cls azlog.Event, msg string) { | |
log.Println(msg) // printing log out to the console | |
}) | |
azlog.SetEvents(azlog.EventRequest, azlog.EventResponse) | |
} | |
func main() { | |
http.HandleFunc("/msicheck", func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "text/plain") | |
cred, err := GetCredential() | |
if err != nil { | |
m := fmt.Sprintf("GetCredential error: %v", err) | |
log.Println(m) | |
http.Error(w, m, http.StatusInternalServerError) | |
} | |
c, err := ListContainer(r.Context(), cred) | |
for _, v := range c { | |
w.Write([]byte(v)) | |
w.Write([]byte("\n")) | |
} | |
}) | |
addr := fmt.Sprintf("%v:%v", host, port) | |
log.Printf("Listen %v\n", addr) | |
setAzLogging() | |
err := http.ListenAndServe(addr, handlers.LoggingHandler(os.Stdout, http.DefaultServeMux)) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment