是否可以使用服务主体来访问 blob 存储?
Is it possible to use service principal to access blob storage?
我现在尝试使用服务主体来访问节点中的 azure blob 存储,而不是使用连接字符串。
我所做(并成功)的是使用如下连接字符串:
// connect via connection string
const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
现在我想使用 Service Principal 而不是连接字符串,但我似乎无法让它工作。我可以看到一些使用令牌凭据的示例,例如
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
defaultAzureCredential
);
是否可以通过这种方式使用服务主体凭据,或者是否有其他方法可以做到这一点?
试试这个:
const { BlobServiceClient } = require("@azure/storage-blob");
const { ClientSecretCredential } = require("@azure/identity");
const account = '<your accounr name>'
//Using Service Principal
const appID = ""
const appSec = ""
const tenantID = ""
const clientCred = new ClientSecretCredential(tenantID,appID,appSec)
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
clientCred
);
//try to list all containers in stroage account to check if success
blobServiceClient.listContainers().byPage().next().then(result =>{
result.value.containerItems.forEach(element => {
console.log(element.name);
});
})
结果:
注:
在你 运行 这个演示之前,请确保你已经向你的服务主体授予了所需的权限,详情 see this official doc。
我现在尝试使用服务主体来访问节点中的 azure blob 存储,而不是使用连接字符串。
我所做(并成功)的是使用如下连接字符串:
// connect via connection string
const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
现在我想使用 Service Principal 而不是连接字符串,但我似乎无法让它工作。我可以看到一些使用令牌凭据的示例,例如
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
defaultAzureCredential
);
是否可以通过这种方式使用服务主体凭据,或者是否有其他方法可以做到这一点?
试试这个:
const { BlobServiceClient } = require("@azure/storage-blob");
const { ClientSecretCredential } = require("@azure/identity");
const account = '<your accounr name>'
//Using Service Principal
const appID = ""
const appSec = ""
const tenantID = ""
const clientCred = new ClientSecretCredential(tenantID,appID,appSec)
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
clientCred
);
//try to list all containers in stroage account to check if success
blobServiceClient.listContainers().byPage().next().then(result =>{
result.value.containerItems.forEach(element => {
console.log(element.name);
});
})
结果:
注:
在你 运行 这个演示之前,请确保你已经向你的服务主体授予了所需的权限,详情 see this official doc。