Skip to content

Instantly share code, notes, and snippets.

@buchizo
Created September 17, 2019 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buchizo/0651eb9d9b4775a5514f2fabc659825a to your computer and use it in GitHub Desktop.
Save buchizo/0651eb9d9b4775a5514f2fabc659825a to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Storage.Blobs;
using Azure.Identity;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Sas;
namespace sastest
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
var accountName = "xxxx";
var containerName = "test";
var blobName = "test.txt";
string blobEndpoint = string.Format("https://{0}.blob.core.windows.net", accountName);
var blobClient = new BlobServiceClient(new Uri(blobEndpoint), new DefaultAzureCredential());
UserDelegationKey key = await blobClient.GetUserDelegationKeyAsync(DateTimeOffset.UtcNow.AddMinutes(-1),
DateTimeOffset.UtcNow.AddDays(7));
log.LogInformation("User delegation key properties:");
log.LogInformation("Key signed start: {0}", key.SignedStart);
log.LogInformation("Key signed expiry: {0}", key.SignedExpiry);
log.LogInformation("Key signed object ID: {0}", key.SignedOid);
log.LogInformation("Key signed tenant ID: {0}", key.SignedTid);
log.LogInformation("Key signed service: {0}", key.SignedService);
log.LogInformation("Key signed version: {0}", key.SignedVersion);
BlobSasBuilder builder = new BlobSasBuilder()
{
ContainerName = containerName,
BlobName = blobName,
Permissions = "r", // read
Resource = "b", // blob
StartTime = DateTimeOffset.UtcNow.AddMinutes(-1),
ExpiryTime = DateTimeOffset.UtcNow.AddMinutes(10),
Version = key.SignedVersion
};
string sasToken = builder.ToSasQueryParameters(key, accountName).ToString();
UriBuilder fullUri = new UriBuilder()
{
Scheme = "https",
Host = string.Format("{0}.blob.core.windows.net", accountName),
Path = string.Format("{0}/{1}", containerName, blobName),
Query = sasToken
};
log.LogInformation("URL: {0}", fullUri);
return (ActionResult)new OkObjectResult($"url: {fullUri}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment