在 Xamarin 中连接到 Azure 存储表
Connecting to Azure Storage Tables in Xamarin
我有一个小型的 IoT 项目,我正在逐渐减少它,它基本上只是从我的 "smart" 家中获取我最新的传感器遥测数据,并在一个非常标准的 Xamarin 应用程序中将其显示在表单上。
我的问题是关于 Xamarin 和 Azure.Storage 的使用:
我在我的应用程序中下载了 3 个 table:
public DeviceReadingsReader()
{
_deviceReadingsTable = TableClient.GetTableReference("DeviceReadings");
_locationsTable = TableClient.GetTableReference("Locations");
_goalsTable = TableClient.GetTableReference("TemperatureGoals");
}
如您所知,它们都引用了 TableClient,我发现它使用的是较旧的 PCL 兼容的 NuGet 包(一个问题:为什么不是最新的 azure.storage 包可以安装在 Xamarin.Forms??)
在 UWP 和 iOS 上,我这样做没问题:
var cloudStorageAccount = CloudStorageAccount.Parse(connectionstring);
_tableClient = storageAccount.CreateCloudTableClient();
但是,当我在 Android 上 运行 这段代码时,我得到一个异常要求我改用 SAS 令牌,所以我在 Azure 门户中为存储帐户创建了一个 SaS 令牌,有 2 年的持续时间,并像这样使用它:
但是,运行在 Android、iOS 或 UWP 上执行此操作会给我一条授权失败消息,表明我的 SaS 令牌无效。
有谁知道我做错了什么,以及如何编写一段可以在所有 3 个平台上实际执行的代码?
var sas = "https://0000000000000.table.core.windows.net/?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-03-08T23:51:51Z&st=2017-03-08T15:51:51Z&spr=https&sig=0000000000000000000000000000000000000000000000&comp=list&restype=table";
StorageCredentials creds = new StorageCredentials(sas);
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(creds, null, null, new Uri("https://0000000000000.table.core.windows.net/"), null);
_tableClient = cloudStorageAccount.CreateCloudTableClient();
编辑:我知道我可以制作一个云 API 来检索相同的值,所有这些都会消失,但这只是从存储中获取值 table ,而且我真的不认为有必要为此创建中间 WebApi
例外情况:
<RequestResult>
<HTTPStatusCode>403</HTTPStatusCode>
<HttpStatusMessage>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</HttpStatusMessage>
<TargetLocation>Primary</TargetLocation>
<ServiceRequestID>926f8520-0002-0033-02b2-98bab7000000</ServiceRequestID>
<ContentMd5 />
<Etag />
<RequestDate>Thu, 09 Mar 2017 09:55:29 GMT</RequestDate>
<StartTime>Thu, 09 Mar 2017 08:55:28 GMT</StartTime>
<EndTime>Thu, 09 Mar 2017 08:55:30 GMT</EndTime>
<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:926f8520-0002-0033-02b2-98bab7000000
Time:2017-03-09T08:55:32.7622395Z</Message>
</Error>
<ExceptionInfo>
<Type />
<HResult>-2147467259</HResult>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</Message>
<Source>Microsoft.WindowsAzure.Storage</Source>
<StackTrace> at Microsoft.WindowsAzure.Storage.Core.Executor.Executor+<ExecuteAsyncInternal>d__0`1[T].MoveNext () [0x007eb] in <03db1448eaed4d018343fcf0153c030d>:0 </StackTrace>
</ExceptionInfo>
</RequestResult>
所以您使用 SAS 的方式存在问题。您正在使用 SAS URL。请仅使用查询字符串部分:
所以你的 SAS 应该是这样的:
var sas =
"?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-03-08T23:51:51Z&st=2017-03-08T15:51:51Z&spr=https&sig=0000000000000000000000000000000000000000000000";
试试这个。
我有一个小型的 IoT 项目,我正在逐渐减少它,它基本上只是从我的 "smart" 家中获取我最新的传感器遥测数据,并在一个非常标准的 Xamarin 应用程序中将其显示在表单上。
我的问题是关于 Xamarin 和 Azure.Storage 的使用:
我在我的应用程序中下载了 3 个 table:
public DeviceReadingsReader()
{
_deviceReadingsTable = TableClient.GetTableReference("DeviceReadings");
_locationsTable = TableClient.GetTableReference("Locations");
_goalsTable = TableClient.GetTableReference("TemperatureGoals");
}
如您所知,它们都引用了 TableClient,我发现它使用的是较旧的 PCL 兼容的 NuGet 包(一个问题:为什么不是最新的 azure.storage 包可以安装在 Xamarin.Forms??)
在 UWP 和 iOS 上,我这样做没问题:
var cloudStorageAccount = CloudStorageAccount.Parse(connectionstring);
_tableClient = storageAccount.CreateCloudTableClient();
但是,当我在 Android 上 运行 这段代码时,我得到一个异常要求我改用 SAS 令牌,所以我在 Azure 门户中为存储帐户创建了一个 SaS 令牌,有 2 年的持续时间,并像这样使用它:
但是,运行在 Android、iOS 或 UWP 上执行此操作会给我一条授权失败消息,表明我的 SaS 令牌无效。
有谁知道我做错了什么,以及如何编写一段可以在所有 3 个平台上实际执行的代码?
var sas = "https://0000000000000.table.core.windows.net/?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-03-08T23:51:51Z&st=2017-03-08T15:51:51Z&spr=https&sig=0000000000000000000000000000000000000000000000&comp=list&restype=table";
StorageCredentials creds = new StorageCredentials(sas);
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(creds, null, null, new Uri("https://0000000000000.table.core.windows.net/"), null);
_tableClient = cloudStorageAccount.CreateCloudTableClient();
编辑:我知道我可以制作一个云 API 来检索相同的值,所有这些都会消失,但这只是从存储中获取值 table ,而且我真的不认为有必要为此创建中间 WebApi
例外情况:
<RequestResult>
<HTTPStatusCode>403</HTTPStatusCode>
<HttpStatusMessage>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</HttpStatusMessage>
<TargetLocation>Primary</TargetLocation>
<ServiceRequestID>926f8520-0002-0033-02b2-98bab7000000</ServiceRequestID>
<ContentMd5 />
<Etag />
<RequestDate>Thu, 09 Mar 2017 09:55:29 GMT</RequestDate>
<StartTime>Thu, 09 Mar 2017 08:55:28 GMT</StartTime>
<EndTime>Thu, 09 Mar 2017 08:55:30 GMT</EndTime>
<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:926f8520-0002-0033-02b2-98bab7000000
Time:2017-03-09T08:55:32.7622395Z</Message>
</Error>
<ExceptionInfo>
<Type />
<HResult>-2147467259</HResult>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</Message>
<Source>Microsoft.WindowsAzure.Storage</Source>
<StackTrace> at Microsoft.WindowsAzure.Storage.Core.Executor.Executor+<ExecuteAsyncInternal>d__0`1[T].MoveNext () [0x007eb] in <03db1448eaed4d018343fcf0153c030d>:0 </StackTrace>
</ExceptionInfo>
</RequestResult>
所以您使用 SAS 的方式存在问题。您正在使用 SAS URL。请仅使用查询字符串部分:
所以你的 SAS 应该是这样的:
var sas = "?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-03-08T23:51:51Z&st=2017-03-08T15:51:51Z&spr=https&sig=0000000000000000000000000000000000000000000000";
试试这个。