gpt4 book ai didi

visual-studio-2012 - 从控制台应用程序生成用于 Azure 存储帐户访问的共享访问签名 URI > 如何将结果输出到文件?

转载 作者:行者123 更新时间:2023-12-03 05:09:10 25 4
gpt4 key购买 nike

问题

我正在使用下面的代码为 Azure 存储帐户生成 SAS URI,这很有效。

但是,执行代码时,生成的控制台应用程序窗口会显示相关的 URI 字符串,但我无法复制该值。

问题

问题很简单,需要在下面的代码中添加什么才能将 URI 输出到文本文件?

{

using System.IO;

using Microsoft.WindowsAzure;

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Blob;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text; using System.Threading.Tasks;

namespace SAS

{ class Program { static void Main(string[] args) {

//Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

//Create the blob client object.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

//Get a reference to a container to use for the sample code, and create it if it does > not exist.
CloudBlobContainer container = blobClient.GetContainerReference("sascontainer");
container.CreateIfNotExists();

//Insert calls to the methods created below here...


//Generate a SAS URI for the container, without a stored access policy.
Console.WriteLine("Container SAS URI: " + GetContainerSasUri(container));
Console.WriteLine();

//Require user input before closing the console window.
Console.ReadLine();
}



static string GetContainerSasUri(CloudBlobContainer container)
{
//Set the expiry time and permissions for the container.
//In this case no start time is specified, so the shared access signature > becomes valid immediately.
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(4);
sasConstraints.Permissions = SharedAccessBlobPermissions.Write | > SharedAccessBlobPermissions.List;

//Generate the shared access signature on the container, setting the > constraints directly on the signature.
string sasContainerToken = > container.GetSharedAccessSignature(sasConstraints);

//Return the URI string for the container, including the SAS token.
return container.Uri + sasContainerToken;
}

}

} }

最佳答案

如果您只想从控制台获取 SAS 字符串,Powershell 将是一种更快的方法。

首先确保您安装了 Azure PowerShell,并通过 Add-AzureAccount 或 Import-AzurePublicSettingsFile 验证您的 Azure PowerShell session

然后,您已使用 account_name 创建了存储帐户,并使用 container_name 创建了容器:

$context = New-AzureStorageContext -StorageAccountName account_name -StorageAccountKey (Get-AzureStorageKey -StorageAccountName account_name).Primary
$container = (Get-AzureStorageContainer -Name container_name -Context $context).CloudBlobContainer
$sp = New-Object -TypeName Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy
$sp.SharedAccessExpiryTime = [System.DateTime]::Now.AddHours(4)
$sp.Permissions = ([Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions]::Write -bor [Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions]::List)
$uriwithsas = $container.Uri.AbsoluteUri + $container.GetSharedAccessSignature($sp)
echo $uriwithsas

关于visual-studio-2012 - 从控制台应用程序生成用于 Azure 存储帐户访问的共享访问签名 URI > 如何将结果输出到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25785138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com