Azure blob 存储 - 下载资源时控制名称

Azure blob storage - controlling name when resource is downloaded

我们有数千个文件要存储在 Azure blob 存储中。这些文件可以是任何类型,但最常见的是 PDF 文件、图像和 Excel。通过锚标记中的 blob URI,我们系统的用户可以将文件直接从 Azure 下载到用户的计算机。一切都很好。

我们希望 blob 名称是文件本身的名称,这样当用户下载文件时,原始文件名是完整的。但是,我们必须考虑容器中不可避免的文件名冲突。使用 GUID 作为 blob 名称是一个可行的解决方案,但是当用户单击下载文件时,他们将获得一个文件名作为 GUID blob 名称(相对于原始文件名)。

有没有办法使用 GUID 作为 blob 名称,但是当用户点击下载文件时,返回的文件具有原始文件名?我意识到我们可以将文件下载到我们的服务器并将正确命名的文件流式传输给用户,但我们正在努力减轻我们的网络服务器的负载以进行文件下载。

那么是否有一个带有 blob 的配置设置可以使文件以正确的文件名返回?我们可以为 blob 设置 MIME 类型,我们正在为文件名部分寻找类似的设置。

Is there a way to use a GUID for the blob name, but when the user clicks to download the file, the returned file has the original filename? I realize we can download the file to our server and stream a properly named file to the user, but we are trying to keep the load off our web server for file downloads.

您可以使用 Content-Disposition 属性 的 blob 来实现此目的。您可以做的是将 blob 的名称设置为 GUID,但是将 blob 的 content-disposition 属性 设置为 attachment; filename="original-file-name"。如果你想让文件一直被下载,你可以永久设置这个属性。但是,如果您只想在下载文件时设置此名称,您可以在具有 Read 权限的 blob 上创建一个 Shared Access Signature 并覆盖此 header。我在 my blog sometime ago 中写过这个。来自我的博客 post:

Assume a scenario where you want your users to download the files from your storage account but you wanted to give those files a user friendly name. Furthermore, you want your users to get prompted for saving the file instead of displaying the file in browser itself (say a PDF file opening up automatically in the browser only). To accomplish this, earlier you would need to first fetch the file from your blob storage on to your server and then write the data of that file in the response stream by setting “Content-Disposition” header. In fact, I spent a good part of last week implementing the same solution. Only if I had known that this feature is coming in storage itself:).

Now you don’t need to do that. What you could do is specify a content-disposition property on the blob and set that as “attachment; filename=yourdesiredfilename” and when your user tries to access that through a browser, they will be presented with file download option.

Now you may ask, what if I have an image file which I want to show inline also and also as a downloadable item also. Very valid requirement. Well, the smart guys in the storage team has already thought about that :). Not only you can set content-disposition as a blob property but you can override this property in a SAS URL (more on it in a bit).