gpt4 book ai didi

azure - 如何将捆绑和缩小的文件上传到 Windows Azure CDN

转载 作者:行者123 更新时间:2023-12-03 10:14:34 24 4
gpt4 key购买 nike

我正在 Microsoft.AspNet.Web.Optimization 中使用 ASP.NET MVC 4 捆绑和缩小功能命名空间(例如 @Styles.Render("~/content/static/css"))。

我想将其与 Windows Azure CDN 结合使用。

我考虑编写自定义 BundleTransform 但内容尚未优化。

我还研究了在运行时解析和上传优化流,但这对我来说感觉像是一种黑客行为,我不太喜欢它:

@StylesCdn.Render(Url.AbsoluteContent(
Styles.Url("~/content/static/css").ToString()
));

public static IHtmlString Render(string absolutePath)
{
// get the version hash
string versionHash = HttpUtility.ParseQueryString(
new Uri(absolutePath).Query
).Get("v");

// only parse and upload to CDN if version hash is different
if (versionHash != _versionHash)
{
_versionHash = versionHash;

WebClient client = new WebClient();
Stream stream = client.OpenRead(absolutePath);

UploadStreamToAzureCdn(stream);
}

var styleSheetLink = String.Format(
"<link href=\"{0}://{1}/{2}/{3}?v={4}\" rel=\"stylesheet\" type=\"text/css\" />",
cdnEndpointProtocol, cdnEndpointUrl, cdnContainer, cdnCssFileName, versionHash
);

return new HtmlString(styleSheetLink);
}

如何将捆绑版本和精简版本自动上传到我的 Windows Azure CDN?

最佳答案

按照Hao的建议,我扩展了Bundle和IBundleTransform。

将 AzureScriptBundle 或 AzureStyleBundle 添加到 bundle ;

bundles.Add(new AzureScriptBundle("~/bundles/modernizr.js", "cdn").Include("~/Scripts/vendor/modernizr.custom.68789.js"));

结果;

<script src="//127.0.0.1:10000/devstoreaccount1/cdn/modernizr.js?v=g-XPguHFgwIb6tGNcnvnI_VY_ljCYf2BDp_NS5X7sAo1"></script>

如果未设置 CdnHost,它将使用 blob 的 Uri 而不是 CDN。

类别

using System;
using System.Text;
using System.Web;
using System.Web.Optimization;
using System.Security.Cryptography;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace SiegeEngineWebRole.BundleExtentions
{
public class AzureScriptBundle : Bundle
{
public AzureScriptBundle(string virtualPath, string containerName, string cdnHost = "")
: base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new AzureBlobUpload { ContainerName = containerName, CdnHost = cdnHost } })
{
ConcatenationToken = ";";
}
}

public class AzureStyleBundle : Bundle
{
public AzureStyleBundle(string virtualPath, string containerName, string cdnHost = "")
: base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new AzureBlobUpload { ContainerName = containerName, CdnHost = cdnHost } })
{
}
}

public class AzureBlobUpload : IBundleTransform
{
public string ContainerName { get; set; }
public string CdnHost { get; set; }

static AzureBlobUpload()
{
}

public virtual void Process(BundleContext context, BundleResponse response)
{
var file = VirtualPathUtility.GetFileName(context.BundleVirtualPath);

if (!context.BundleCollection.UseCdn)
{
return;
}
if (string.IsNullOrWhiteSpace(ContainerName))
{
throw new Exception("ContainerName Not Set");
}

var conn = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
var blob = conn.CreateCloudBlobClient()
.GetContainerReference(ContainerName)
.GetBlobReference(file);

blob.Properties.ContentType = response.ContentType;
blob.UploadText(response.Content);

var uri = string.IsNullOrWhiteSpace(CdnHost) ? blob.Uri.AbsoluteUri.Replace("http:", "").Replace("https:", "") : string.Format("//{0}/{1}/{2}", CdnHost, ContainerName, file);

using (var hashAlgorithm = CreateHashAlgorithm())
{
var hash = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content)));
context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}?v={1}", uri, hash);
}
}

private static SHA256 CreateHashAlgorithm()
{
if (CryptoConfig.AllowOnlyFipsAlgorithms)
{
return new SHA256CryptoServiceProvider();
}

return new SHA256Managed();
}
}
}

关于azure - 如何将捆绑和缩小的文件上传到 Windows Azure CDN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12047981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com