mirror of
https://github.com/yaakov-h/Shamir.git
synced 2025-01-18 08:46:33 +00:00
Simplify SAS command to generate full URL, only add SAS if needed.
This commit is contained in:
parent
71a2030a05
commit
4d35fee21e
2 changed files with 24 additions and 18 deletions
|
@ -3,13 +3,14 @@ using System.Diagnostics;
|
|||
using System.Threading.Tasks;
|
||||
using Azure.Storage;
|
||||
using Azure.Storage.Blobs;
|
||||
using Azure.Storage.Blobs.Models;
|
||||
using Azure.Storage.Sas;
|
||||
using CommandLine;
|
||||
using Microsoft.Azure.Storage;
|
||||
|
||||
namespace Shamir.Console
|
||||
{
|
||||
public class StorageSasOptions
|
||||
public class StorageGetUrlOptions
|
||||
{
|
||||
[Option("connection-string", Required = false, HelpText = "Azure Storage connection string for the Storage Account backing the CDN.")]
|
||||
public string? ConnectionString { get; set; }
|
||||
|
@ -24,13 +25,13 @@ namespace Shamir.Console
|
|||
public string? Path { get; set; }
|
||||
}
|
||||
|
||||
public sealed class StorageSasCommand : ParsedArgumentsCommand<StorageSasOptions>
|
||||
public sealed class StorageGetUrlCommand : ParsedArgumentsCommand<StorageGetUrlOptions>
|
||||
{
|
||||
public override string Name => "sas";
|
||||
public override string Name => "get-url";
|
||||
|
||||
public override string Description => "Generate a SAS token for a file in Storage.";
|
||||
public override string Description => "Get the URL for a file in Storage, with a SAS token if required.";
|
||||
|
||||
public override ValueTask<int> ExecuteAsync(IServiceProvider serviceProvider, StorageSasOptions options)
|
||||
public override async ValueTask<int> ExecuteAsync(IServiceProvider serviceProvider, StorageGetUrlOptions options)
|
||||
{
|
||||
var connectionString = options.ConnectionString ?? Environment.GetEnvironmentVariable("AZURE_CONNECTION_STRING");
|
||||
var account = CloudStorageAccount.Parse(connectionString);
|
||||
|
@ -42,8 +43,17 @@ namespace Shamir.Console
|
|||
? (options.Path[..delimiterIndex], options.Path[(delimiterIndex + 1)..])
|
||||
: (options.Path, string.Empty);
|
||||
|
||||
var client = new BlobServiceClient(connectionString);
|
||||
var uri = new UriBuilder
|
||||
{
|
||||
Scheme = "https",
|
||||
Host = options.HostName ?? account.BlobStorageUri.PrimaryUri.Host,
|
||||
Path = options.Path,
|
||||
};
|
||||
|
||||
var client = new BlobServiceClient(connectionString);
|
||||
var containerPolicy = await client.GetBlobContainerClient(containerName).GetAccessPolicyAsync();
|
||||
if (containerPolicy.Value.BlobPublicAccess == PublicAccessType.None)
|
||||
{
|
||||
var builder = new BlobSasBuilder(BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddDays(options.ValidityPeriodDays));
|
||||
builder.BlobContainerName = containerName;
|
||||
builder.BlobName = path;
|
||||
|
@ -52,17 +62,13 @@ namespace Shamir.Console
|
|||
|
||||
var key = new StorageSharedKeyCredential(account.Credentials.AccountName, account.Credentials.ExportBase64EncodedKey());
|
||||
var parameters = builder.ToSasQueryParameters(key);
|
||||
var uri = new UriBuilder
|
||||
{
|
||||
Scheme = "https",
|
||||
Host = options.HostName ?? account.BlobStorageUri.PrimaryUri.Host,
|
||||
Path = options.Path,
|
||||
Query = parameters.ToString(),
|
||||
};
|
||||
|
||||
uri.Query = parameters.ToString();
|
||||
}
|
||||
|
||||
System.Console.WriteLine(uri.Uri.AbsoluteUri);
|
||||
|
||||
return ValueTask.FromResult(0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -30,7 +30,7 @@ namespace Shamir.Console
|
|||
ImmutableArray.Create<ICommand>(
|
||||
new StorageLsCommand(),
|
||||
new StorageCopyCommand(),
|
||||
new StorageSasCommand()
|
||||
new StorageGetUrlCommand()
|
||||
))
|
||||
),
|
||||
ImmutableArray<ICommand>.Empty
|
||||
|
|
Loading…
Reference in a new issue