在C#中创建一个共享帐户签名 [英] create a Share Account signature in c#

查看:65
本文介绍了在C#中创建一个共享帐户签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问一个位于天蓝色文件共享中的文件,将其复制到Blob存储中.当我通过使用Microsoft Azure存储资源管理器通过右键单击文件并执行整个过程,然后在代码中对生成的值进行硬编码来创建共享访问签名时,它将起作用.但是我无法使用此C#代码生成此值.

I would like to access a file located in azure file shares to copy it into blob storage. It works when I create Shared Access Signature by using the Microsoft Azure Storage Explorer by right-clicking on the file and going through the process and then hardcode generated value in the code. But I cannot generate this value using this C# code.

var sharedAccessFilePolicy = new SharedAccessFilePolicy()
{
    Permissions = SharedAccessFilePermissions.Read,
    SharedAccessStartTime = DateTime.Now,
    SharedAccessExpiryTime = DateTime.Now.AddDays(1)
};

此代码还会生成SAS,但仍然无法正常工作.任何的想法? 顺便说一句,我正在使用Microsoft.Azure.Storage NuGet

This code also generates an SAS but it does not work anyways. any idea? BTW I'm using Microsoft.Azure.Storage NuGet

sv = 2019-02-02,sr = f,sig = ****,se = ****,sp = r"

sv=2019-02-02, sr=f, sig=****, se=****, sp=r"

st = 2019-11-06T10 ****,se = 2019-11-07T10 **

st=2019-11-06T10****, se=2019-11-07T10**1**, sp=rl, sv=2018-03-28, sr=f, sig=****

第一个是由代码生成的,第二个是来自Microsoft Azure Storage Explorer的代码

the first one is generated by the code and the second one is the one that is comming from Microsoft Azure Storage Explorer

推荐答案

我测试过以下代码,它可以生成文件sas令牌并将文件复制到blob.

The below code I have test, it could generate the file sas token and copy the file to blob.

static async System.Threading.Tasks.Task Main(string[] args)
        {
            var accountName = "accountname";
            var accountKey = "your account key";
            var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
            var fileClient = account.CreateCloudFileClient();
            var share = fileClient.GetShareReference("windows");
            CloudFileDirectory rootDir = share.GetRootDirectoryReference();
            var file = rootDir.GetFileReference("test.json");

            var sasToken = file.GetSharedAccessSignature(new Microsoft.WindowsAzure.Storage.File.SharedAccessFilePolicy()
            {
                Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write,
                SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.AddDays(1))
            });

            var uri = file.StorageUri.PrimaryUri + sasToken;
            Uri fileuri = new Uri(uri);
            Console.WriteLine(uri);
            var blobclient = account.CreateCloudBlobClient();
            var  containerClient =blobclient.GetContainerReference("test");
            var blob=containerClient.GetBlockBlobReference("test.json");
            await blob.StartCopyAsync(fileuri);

        }

希望这可以为您提供帮助,如果仍然有其他问题,请随时告诉我.

Hope this could help you, if you still have other problem please feel free to let me know.

这篇关于在C#中创建一个共享帐户签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆