I am trying to download some data from Azure Blob Storage in Go via the official azure-sdk-for-go
.
To setup my development environment I have successfully logged in via az login
. I have verified that the blob can be accessed via CLI:
az storage blob download --container-name [container-name] --name [blob-name] --account-name [storage-account-name] -f out.txt
This works as expected. To fetch the file unsing go I am using the following snippet (as a reproducer):
func getBlob(account, container, object string) ([]byte, error) {
blobPath := fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", uri.Host, container, object)
ctx := context.Background()
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return []byte{}, err
}
blobClient, err := azblob.NewBlockBlobClient(blobPath, credential, nil)
if err != nil {
return []byte{}, err
}
get, err := blobClient.Download(ctx, nil)
if err != nil {
return []byte{}, err
}
downloadedData := &bytes.Buffer{}
reader := get.Body(&azblob.RetryReaderOptions{})
_, err = downloadedData.ReadFrom(reader)
if err != nil {
return []byte{}, err
}
err = reader.Close()
if err != nil {
return []byte{}, err
}
data = downloadedData.Bytes()
return data, nil
}
Being logged in via az login
I would expect azidentity.NewDefaultAzureCredential(nil)
to use this session/cerdentials (see https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#-option-3-sign-in-with-azure-cli), however that appears not no work as expected. The error I get is the following:
===== RESPONSE ERROR (ErrorCode=AuthorizationPermissionMismatch) =====
Description=This request is not authorized to perform this operation using this permission.
RequestId:b078ec61-xxxx-xxxx-xxxx-604682000000
Time:2022-05-05T10:24:18.8093649Z, Details: (none)
exit status 255
What am I missing?
(I am coming from a AWS background so chances are I am making assumptions on how things should work based on that experience.)
az
CLI. With the same permissions (I am working as owner) I would expect my go code to work, at least from a permission perspective. However chances are thatazidentity.NewDefaultAzureCredential(nil)
is not reusing the credentials fromaz login
in the manner I currently assume.