c# - 正确解码 Azure Blob SAS Url 以避免签名格式不正确错误

标签 c# azure neo4j azure-blob-storage

为了在 Neo4j 中进行进一步处理,我需要对 SAS 编码的 URL 进行未转义,因为 Neo4j 会再次进行编码并导致 403 http 错误代码。

将 json 文件上传到 Azure Blob 存储并获取编码的 SAS Url 并使用 DecodeUrlString 对其进行解码的代码:

BlobContainerClient containerClient = new BlobContainerClient(absConnectionString, absContainerName);
await containerClient.CreateIfNotExistsAsync();

string fileName = fileKey + ".json";

// Get a reference to a blob
BlobClient blobClient = containerClient.GetBlobClient(fileName);

await using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
    response = await blobClient.UploadAsync(ms, new BlobUploadOptions
    {
        HttpHeaders = new BlobHttpHeaders
        {
            ContentType = "application/json",
            ContentEncoding = "UTF-8"
        }
    });
}

var absoluteUrl = string.Empty;
if (response.GetRawResponse().Status == 201)
{
    // 1 hour valid
    var originalUrl = blobClient.GenerateSasUri(BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddHours(1)).AbsoluteUri;
    originalUrl = DecodeUrlString(originalUrl);
}


private static string DecodeUrlString(string url)
    {
        string newUrl;
        while ((newUrl = Uri.UnescapeDataString(url)) != url)
            url = newUrl;
        
        return newUrl;
    }

有时解码的 URL 会起作用,有时我会在 Neo4j 和浏览器中遇到此错误(取决于 SAS 编码的 URL,尤其是签名字段 => 以 &sig= 开头):

AuthenticationFailed Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:552242c2-301e-003e-55d9-457612000000 Time:2022-04-01T15:04:20.4824344Z Signature fields not well formed.

原始 Blob URL:

https://[ACCOUNTNAME].blob.core.windows.net/[CONTAINER]/file.json?sv=2021-04-10&se=2022-04-01T10%3A10%3A46Z&sr=b&sp=r&sig=%2BTCB%2FwyaV5RfI7X7%2Fb7yoOlGZSzd5OjgXw2GDOCkB04%3D

解码后的网址:

https://[ACCOUNTNAME].blob.core.windows.net/[CONTAINER]/file.json?sv=2021-04-10&se=2022-04-01T10:10:46Z&sr=b&sp=r&sig=+TCB/wyaV5RfI7X7/b7yoOlGZSzd5OjgXw2GDOCkB04=

Neo4j 的问题描述如下:apoc.load.jsonParams with Azure Blob signed URL leads to http 403 error

如何正确转义 SAS URL 以避免此类错误?

最佳答案

现在 Neo4j v4.4.7 和 apoc v4.4.0.5 版本已经解决了这个问题。

关于c# - 正确解码 Azure Blob SAS Url 以避免签名格式不正确错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71709357/

相关文章:

c# - Azure Functions 的 HostingEnviornment.QueueBackgroundWorkItem 等效项

AzureDevops - 如何根据依赖于先前代理的自定义条件执行发布管道代理

Azure 流量管理器对 Azure Function Apps 的支持

neo4j - 如果节点不存在,如何根据特定属性创建节点,否则返回现有节点

c# - 显示月份名称的 DataAnnotation

c# - WCF 中的异步操作

c# - 公共(public)静态任务运行(Func<任务>函数);

c# - 调用 Web 服务时出现类型未定义错误

neo4j - py2neo-使用具有唯一性约束的新属性更新现有节点(merge_one)

java - Neo4j 子图遍历