Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于操纵Azure blob容器的共享访问签名_Azure_Azure Storage - Fatal编程技术网

用于操纵Azure blob容器的共享访问签名

用于操纵Azure blob容器的共享访问签名,azure,azure-storage,Azure,Azure Storage,我希望将SAS URI提供给其他应用程序,以允许它们将文件发送到blob容器。但是,当尝试上载文件时,代码失败,导致出现异常,该异常状态为: 服务器无法对请求进行身份验证。确保包括签名在内的授权标头的值格式正确 我正在使用aspnetcore和WindowsAzure.Storage版本9.1.0 var acc = CloudStorageAccount.Parse("mysecrets"); CloudBlobClient client = acc.Creat

我希望将SAS URI提供给其他应用程序,以允许它们将文件发送到blob容器。但是,当尝试上载文件时,代码失败,导致出现异常,该异常状态为:

服务器无法对请求进行身份验证。确保包括签名在内的授权标头的值格式正确

我正在使用aspnetcore和WindowsAzure.Storage版本9.1.0

        var acc = CloudStorageAccount.Parse("mysecrets");
        CloudBlobClient client = acc.CreateCloudBlobClient();
        var container = client.GetContainerReference("myblobcontainer");
        await container.CreateIfNotExistsAsync();
        var permissions = container.GetPermissionsAsync().Result;
        permissions.SharedAccessPolicies.Add("writepolicy",
            new SharedAccessBlobPolicy
            {
                Permissions =
                    SharedAccessBlobPermissions.Add |
                    SharedAccessBlobPermissions.Create |
                    SharedAccessBlobPermissions.List |
                    SharedAccessBlobPermissions.Write,
            });
        await container.SetPermissionsAsync(permissions);
        var sas = container.GetSharedAccessSignature(null, "writepolicy");
        var accessUri = container.Uri.ToString() + sas;

        var cloudBlobContainer = new CloudBlobContainer(new Uri(accessUri));
        var blob = cloudBlobContainer.GetBlockBlobReference("myfile.txt");
        await blob.UploadFromFileAsync("foo.txt");
我还尝试过创建cloudBlobContainer,如下所示:

var cloudBlobContainer = 
             new CloudBlobContainer(container.Uri, new StorageCredentials(sas));

本质上,问题在于您的
共享访问签名(SAS)
缺少
到期日期
,即SAS到期的日期/时间

当我运行你的代码时,我得到了同样的错误。但当我通过Fiddler跟踪请求/响应时,存储服务返回了以下内容:

<?xml version="1.0" encoding="utf-8"?>
  <Error>
    <Code>AuthenticationFailed</Code>
    <Message>
      Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
      RequestId:320035be-801e-0040-6e5e-c30407000000
      Time:2018-03-24T10:57:38.7791868Z
    </Message>
    <AuthenticationErrorDetail>
      Signed expiry must be specified in signature or SAS identifier
    </AuthenticationErrorDetail>
  </Error>
您需要做的是添加到期日期/时间。您可以添加到共享访问策略,也可以在创建SAS时指定此策略(但不能同时在两个位置)

请使用以下代码创建SAS:

    var policy = new SharedAccessBlobPolicy()
    {
        SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(15)
    };
    var sas = container.GetSharedAccessSignature(policy, "writepolicy");

谢谢,这让我发疯了。没有想到运行fiddler来查看请求。fiddler在任何情况下都是您最好的朋友:)。您可以立即获得有关该操作的大量信息,当Storage SDK不直接公开错误详细信息时,这一信息尤其有用(您必须深入SDK才能找到这些信息)。