【发布时间】:2020-11-20 22:57:59
【问题描述】:
我想使用来自 Azure Functions 的系统分配的标识进行身份验证并从存储帐户读取。我得到了 .NET 的以下代码。我一直在寻找 Java 中的等效代码。提前致谢。
public static class Function1
{
[FunctionName("WebHook-Func")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://storage.azure.com/");
TokenCredential creds = new TokenCredential(accessToken);
log.LogInformation($"Token: {accessToken}");
StorageCredentials storageCreds = new StorageCredentials(creds);
try
{
CloudBlobClient client = new CloudBlobClient(new StorageUri(new Uri("https://<storageAccount>.blob.core.windows.net")), storageCreds);
CloudBlobContainer container = client.GetContainerReference("fltd");
CloudBlockBlob blob = container.GetBlockBlobReference("shopping.txt");
string content = await blob.DownloadTextAsync();
return (ActionResult)new OkObjectResult($"File contents: {content}");
}catch(Exception ex)
{
return new BadRequestObjectResult($"Exception when calling web hook: {ex.StackTrace} {ex.Message}");
}
}
}
【问题讨论】:
-
代码似乎是正确的。您面临什么问题?
-
@ThiagoCustodia,我正在为此寻找等效的 Java 实现。
-
您还有其他顾虑吗?如果您没有其他顾虑,可以accept it as an answer吗?
标签: java azure azure-functions azure-blob-storage azure-managed-identity