如何在门户的 Azure BLOB 存储中设置 CORS?

2023-04-18前端开发问题
16

本文介绍了如何在门户的 Azure BLOB 存储中设置 CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我们在 Windows Azure 上有一个 blob 存储.

We have a blob storage on Windows Azure.

http://mytest.blob.core.windows.net/forms

我使用 CloudBerry 将一些文件上传到存储.我可以通过浏览器成功下载文件.这些文件是简单的文本文件,但具有不同的文件扩展名.例如,

I uploaded a few files to the storage using CloudBerry. And I can download the files by browsers successfully. These files are simple text files, but with different file extensions. For example,

http://mytest.blob.core.windows.net/forms/f001.etx

我想通过 jquery ($.get) 下载文件,但是由于 CORS 失败.

I want to download the files via jquery ($.get), however, it failed because of CORS.

如何在 Portal 的 Azure BLOB 存储中配置 CORS?

How can I configure CORS in Azure BLOB Storage in Portal?

而且,我也应该在客户端为 CORS 做点什么吗?

And, should I do something for CORS in the client side too?

推荐答案

更新: 在回答此问题时,Azure 门户没有此功能.它现在按照此处概述的方式进行.下面概述了在添加 UI 之前执行此操作的方法.

UPDATE: At the time of this answer the Azure Portal did not have this feature. It does now as outlined here. The following outlines the way to do this before the UI was added.

如何在 Portal 的 Azure BLOB 存储中配置 CORS?

How can I configure CORS in Azure BLOB Storage in Portal?

如果您愿意,您可以随时以编程方式设置 Blob 存储的 CORS 规则.如果您使用的是 .Net 存储客户端库,请查看存储团队的这篇博文:http://blogs.msdn.com/b/windowsazurestorage/archive/2014/02/03/windows-azure-storage-introducing-cors.aspx.从该博客文章中设置 CORS 设置的代码:

If you want you can always set the CORS rules for blob storage programmatically. If you're using .Net Storage Client library, check out this blog post from storage team: http://blogs.msdn.com/b/windowsazurestorage/archive/2014/02/03/windows-azure-storage-introducing-cors.aspx. Code for setting CORS setting from that blog post:

private static void InitializeCors()
{
     // CORS should be enabled once at service startup
     // Given a BlobClient, download the current Service Properties 
     ServiceProperties blobServiceProperties = BlobClient.GetServiceProperties();
     ServiceProperties tableServiceProperties = TableClient.GetServiceProperties();

     // Enable and Configure CORS
     ConfigureCors(blobServiceProperties);
     ConfigureCors(tableServiceProperties);

     // Commit the CORS changes into the Service Properties
     BlobClient.SetServiceProperties(blobServiceProperties);
     TableClient.SetServiceProperties(tableServiceProperties);
}

private static void ConfigureCors(ServiceProperties serviceProperties)
{
    serviceProperties.Cors = new CorsProperties();
    serviceProperties.Cors.CorsRules.Add(new CorsRule()
    {
        AllowedHeaders = new List<string>() { "*" },
        AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post,
        AllowedOrigins = new List<string>() { "*" },
        ExposedHeaders = new List<string>() { "*" },
        MaxAgeInSeconds = 1800 // 30 minutes
     });
}

如果您正在寻找一种工具来做同样的事情,一些存储资源管理器支持配置 CORS - Azure 存储资源管理器、Cerebrata Azure Management Studio、Cloud Portam(披露 - 我正在构建 Cloud Portam 实用程序).

If you're looking for a tool to do the same, a few storage explorers have support for configuring CORS - Azure Storage Explorer, Cerebrata Azure Management Studio, Cloud Portam (Disclosure - I'm building Cloud Portam utility).

正确配置 CORS 后,您可以使用 Rory 的回答中提到的代码从 blob 存储下载文件.正如 Rory 所说,您不必在客户端做任何特别的事情.

Once the CORS is configured properly, you can use the code mentioned in Rory's answer to download the file from blob storage. You don't have to do anything special on the client side as mentioned by Rory.

这篇关于如何在门户的 Azure BLOB 存储中设置 CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13