提问人:Asif Khokhar 提问时间:11/13/2023 更新时间:11/13/2023 访问量:80
使用服务 SAS 令牌在 Azure 队列中插入消息
Insert message in Azure Queue using Service SAS Token
问:
这是我第一次与SAS合作。我正在尝试使用服务 Sas 令牌访问 azure 队列并尝试插入一条消息,但它不起作用。当我尝试从浏览器访问生成的 URI 时,我出现以下错误
“该资源不支持指定的 Http 谓词。请求 Id:cc1f2356-f003-0015-78b6-15f337000000 时间:2023-11-12T22:23:52.3942771Z”。 可能是什么问题?下面是我的示例代码
注意:我将用原始值替换 queueName、AccountName 和 AccountKey。
public async Task<ApiResponse<BlobServiceClient>> GetServiceQueueSasToken()
{
ApiResponse<BlobServiceClient> result = new ApiResponse<BlobServiceClient>();
try
{
string queueName = "queueName";
string accountName = "accountName";
string accountKey = "accountKey";
StorageSharedKeyCredential storageSharedKeyCredential = new(accountName, accountKey);
var serviceQueueUri = new Uri($"https://{accountName}.queue.core.windows.net/{queueName}");
string sasToken = await CreateServiceQueueSASToken(storageSharedKeyCredential, queueName);
BlobServiceClient blobServiceClientAccountSAS = new BlobServiceClient(new Uri($"{serviceQueueUri}? {sasToken}"));
SendMessageUsingSasToken(blobServiceClientAccountSAS.Uri queueName, sasToken);
result.Data = blobServiceClientAccountSAS;
result.IsSucceed = true;
}
catch (Exception ex)
{
result.IsSucceed = false;
result.Exception = ex;
result.ErrorMessages = new List<string>();
result.ErrorMessages.Add(ex.Message);
}
return result;
}
public static async Task<string> CreateServiceQueueSASToken(StorageSharedKeyCredential sharedKey, string queueName)
{
var queueSasBuilder = new QueueSasBuilder()
{
QueueName = queueName,
ExpiresOn = DateTimeOffset.UtcNow.AddDays(1),
Protocol = SasProtocol.Https
};
queueSasBuilder.SetPermissions(QueueSasPermissions.Add | QueueSasPermissions.Read);
string sasToken = queueSasBuilder.ToSasQueryParameters(sharedKey).ToString();
return sasToken;
}
static void SendMessageUsingSasToken(Uri serviceUri, string queueName, string sasToken)
{
var queueClient = new QueueClient(serviceUri, queueName, sasToken);
queueClient.SendMessage("Your message content", sasToken);
Console.WriteLine("Message sent successfully.");
}
答:
0赞
Sampath
11/13/2023
#1
在方法中使用 SAS 令牌时出现的问题。构造函数已期望 SAS 令牌包含在 URI 中,因此在创建 .SendMessageUsingSasToken
QueueClient
QueueClient
下面的代码为 Azure 存储队列生成共享访问签名 (SAS) 令牌,并使用该令牌将消息发送到指定的队列。
using Azure.Storage;
using Azure.Storage.Queues;
using Azure.Storage.Sas;
using Azure.Storage.Shared;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class ApiResponse<T>
{
public T Data { get; set; } = default!;
public bool IsSucceed { get; set; }
public Exception? Exception { get; set; }
public List<string> ErrorMessages { get; set; } = new List<string>();
}
class Program
{
static void Main(string[] args)
{
var response = GetServiceQueueSasToken().Result; // Blocking call
if (response.IsSucceed)
{
Console.WriteLine("SAS token generated successfully.");
}
else
{
Console.WriteLine($"Error: {response.Exception?.Message}");
}
}
public static async Task<ApiResponse<QueueServiceClient>> GetServiceQueueSasToken()
{
ApiResponse<QueueServiceClient> result = new ApiResponse<QueueServiceClient>();
try
{
string queueName = "AzurequeueName";
string accountName = "AzureStorageAccountName ";
string accountKey = "AzureStorageAccountAccountKey";
StorageSharedKeyCredential storageSharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
var serviceQueueUri = new Uri($"https://{accountName}.queue.core.windows.net");
string sasToken = CreateServiceQueueSASToken(storageSharedKeyCredential, queueName);
QueueServiceClient queueServiceClient = new QueueServiceClient(new Uri($"{serviceQueueUri}?{sasToken}"));
SendMessageUsingSasToken(queueServiceClient, queueName);
result.Data = queueServiceClient;
result.IsSucceed = true;
}
catch (Exception ex)
{
result.IsSucceed = false;
result.Exception = ex;
result.ErrorMessages = new List<string> { ex.Message };
}
return result;
}
public static string CreateServiceQueueSASToken(StorageSharedKeyCredential sharedKey, string queueName)
{
var queueSasBuilder = new QueueSasBuilder
{
QueueName = queueName,
ExpiresOn = DateTimeOffset.UtcNow.AddDays(1),
Protocol = SasProtocol.Https
};
queueSasBuilder.SetPermissions(QueueSasPermissions.Add | QueueSasPermissions.Read);
string sasToken = queueSasBuilder.ToSasQueryParameters(sharedKey).ToString();
return sasToken;
}
static void SendMessageUsingSasToken(QueueServiceClient queueServiceClient, string queueName)
{
var queueClient = queueServiceClient.GetQueueClient(queueName);
queueClient.SendMessage("Your message content");
Console.WriteLine("Message sent successfully.");
}
}
输出:
评论