我正在尝试使用refit从 Xamarin iOS 应用程序上传到 azure blob 存储。这是我用于改装的接口配置:
[Headers("x-ms-blob-type: BlockBlob")]
[Put("/{fileName}")]
Task<bool> UploadAsync([Body]byte[] content, string sasTokenKey,
[Header("Content-Type")] string contentType);
sasTokenKey 参数如下所示:
"/content-default/1635839001660743375-66f93195-e923-4c8b-a3f1-5f3f9ba9dd32.jpeg?sv=2015-04-05&sr=b&sig=Up26vDxQikFqo%2FDQjRB08YtmK418rZfKx1IHbYKAjIE%3D&se=2015-11-23T18:59:26Z&sp=w"
这就是我使用 Refit 调用 azure blob 服务器的方式:
var myRefitApi = RestService.For<IMyRefitAPI>("https://myaccount.blob.core.windows.net");
myRefitApi.UploadAsync(photoBytes, sasTokenKey, "image/jpeg"
但是我收到以下错误:
Response status code does not indicate success: 403 (Server failed to
authenticate the request. Make sure the value of Authorization header is
formed correctly including the signature.)
如果我像这样直接调用它,SAS url 工作正常
var content = new StreamContent(stream);
content.Headers.Add("Content-Type", "jpeg");
content.Headers.Add("x-ms-blob-type", "BlockBlob");
var task = HttpClient.PutAsync(new Uri(sasTokenUrl), content);
task.Wait();
所以基本上我只是想用 Refit 做同样的事情。知道如何让 Refit 与 Azure Blob 存储一起使用吗?
谢谢!
[更新] 我现在可以将字节上传到 azure blob 服务器,但字节数据似乎有问题,因为我无法查看图像。这是我用来转换为字节数组的代码。
byte[] bytes;
using (var ms = new MemoryStream())
{
stream.Position = 0;
stream.CopyTo(ms);
ms.Position = 0;
bytes = ms.ToArray();
}
[更新] 通过使用流而不是字节数组来修复它!