简体   繁体   English

无法使用 SAS 从 Azure Blob 存储检索文件

[英]Unable to retrieve file from Azure Blob Storage using SAS

We have a web based application that allows users to send emails.我们有一个基于网络的应用程序,允许用户发送电子邮件。 Every email body contains an image which represents the signature block.每个电子邮件正文都包含一个表示签名块的图像。 The image is stored in Azure Blob storage in a private container.图像存储在私有容器中的 Azure Blob 存储中。 To pull the image from storage and place it in the email body, as the email is being constructed by the user, we generate a Uri using the following C# function:为了从存储中提取图像并将其放入电子邮件正文中,因为用户正在构建电子邮件,我们使用以下 C# 函数生成一个 Uri:

public string GenerateBlobDownloadUri(string containerName, string blobName)
        {
            var policy = new SharedAccessBlobPolicy();

            // Get a reference to a container if it exists.
            var container = GetContainer(containerName, false);
            if (container == null)
                return string.Empty;

            // Retrieve reference to the blob
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
            if (!blockBlob.Exists())
                return string.Empty;

            policy.SharedAccessExpiryTime = DateTime.Now.AddYears(10);
            policy.Permissions = SharedAccessBlobPermissions.Read;

            var token = blockBlob.GetSharedAccessSignature(policy);

            return blockBlob.Uri + token;
        }

Using this function, we're able to successfully pull the image from Azure into our email body as the email is being constructed and have it display correctly.使用此函数,我们能够在构建电子邮件时成功地将图像从 Azure 拉入我们的电子邮件正文并使其正确显示。

However, if I were to look at the underlying HTML code of the email, copy the Uri generated and paste it into the browser, the image will not display or download.但是,如果我查看电子邮件的底层 HTML 代码,复制生成的 Uri 并将其粘贴到浏览器中,图像将不会显示或下载。 I just get a Json message saying the image couldn't be found.我刚刚收到一条 Json 消息,说找不到图像。

The actualy Uri being generated by the above function is this:上述函数生成的实际 Uri 是这样的:

https://xxxxx.blob.core.windows.net/winterscontainer/55-Email Signature Block.png?sv=2018-03-28&sr=b&sig=8zkwcK7naE%2BPZRfo6GtBYVfHJLzlCBLR6IaYymTIOGo%3D&se=2030-10-17T12%3A30%3A56Z&sp=r

Again, this Uri works from within our application and correctly pulls in the image and displays correctly while the email is being created.同样,这个 Uri 在我们的应用程序中工作,并在创建电子邮件时正确拉入图像并正确显示。 However, when the email is sent, the recipient just sees a placeholder for the image.但是,发送电子邮件时,收件人只会看到图像的占位符。

If I paste the Uri into my browser, I get this:如果我将 Uri 粘贴到浏览器中,则会得到以下信息:

<Error>
<Code>ResourceNotFound</Code>
<Message>The specified resource does not exist. RequestId:4ca4e68b-b01e-0130-5781-a4a67e000000 Time:2020-10-17T12:33:33.0601864Z</Message>
</Error>

If you check your Uri you will notice something a bit "strange" on the query part, ampersands are being converted to HTML entities, instead of & you are receiving &amp;如果您检查您的 Uri,您会注意到查询部分有点“奇怪”,&符号正在转换为 HTML 实体,而不是&您正在接收&amp; . . Something in the chain that process the message until it arrives to your clients (your api, mail server, web browser, mail client... there is a lot of possibilities) is encoding it (it could be that you already received it encoded but I doubt it, I also use Azure blob storage and never received it encoded).链中处理消息直到它到达您的客户端(您的 api、邮件服务器、Web 浏览器、邮件客户端......我对此表示怀疑,我也使用 Azure blob 存储,但从未收到过编码)。

If you manually decode those escaped chars you will notice that the Uri works fine, so now you must find where in the chain is being encoded, it's a totally different scenario.如果您手动解码那些转义的字符,您会注意到 Uri 工作正常,因此现在您必须找到链中被编码的位置,这是一个完全不同的场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM