【问题标题】:Generating Shared Access Signature URI from console application for Azure Storage Account Access > how to output results to file?从控制台应用程序为 Azure 存储帐户访问生成共享访问签名 URI > 如何将结果输出到文件?
【发布时间】:2014-11-05 06:23:32
【问题描述】:

问题

我正在使用下面的代码为 Azure 存储帐户生成一个 SAS URI,这是一种享受。

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

问题

希望简单的问题,必须在下面的代码中添加什么才能将 URI 输出到文本文件?

{

使用 System.IO;

使用 Microsoft.WindowsAzure;

使用 Microsoft.WindowsAzure.Storage;

使用 Microsoft.WindowsAzure.Storage.Blob;

使用系统;

使用 System.Collections.Generic;

使用 System.Linq;

使用 System.Text; 使用 System.Threading.Tasks;

命名空间 SAS

{ 课堂节目 { 静态无效主要(字符串 [] 参数) {

//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;
    }

}

} }

【问题讨论】:

    标签: visual-studio-2012 azure console azure-storage


    【解决方案1】:

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

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

    然后,您使用 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
    

    【讨论】:

    • 非常感谢。应该是我最初的方法,控制台代码 sn-ps 是我发现帮助为第三方生成 SAS uri 的第一件事。
    • 执行过程中出错。如果可能的话,您能否详细说明此 PS 脚本以包含生成签名所需的所有内容(包括 subscriptionID 、存储帐户、容器名称示例)。感谢您的帮助。
    • 更新一下关于生成上下文的脚本
    • 谢谢,帮了大忙。这正是我所需要的。最好的问候。
    【解决方案2】:

    不确定这是否是您要寻找的,但答案很简单。要将输出保存到文件中,您不能这样做吗:

                File.WriteAllText(@"C:\output.txt", "Container SAS URI: " + GetContainerSasUri(container))
    

    如果您想从命令提示符复制内容,您需要执行以下操作:

    1. 右键单击命令提示符。
    2. 点击Mark
    3. 拖动光标选择文本。
    4. Enter键将选中的文本复制到剪贴板。

    或者……我完全误解了你的问题吗?

    【讨论】:

    • 感谢您的回复。 Console.exe 右键单击​​功能由于某种原因被禁用,因此发布。感谢您的 File.Write 行,这正是我所需要的。 Powershell 脚本是一个有用的解决方案,可能应该是我最初的方法。
    猜你喜欢
    • 2016-06-16
    • 2017-02-17
    • 2023-04-05
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2018-04-23
    相关资源
    最近更新 更多