Cloud computing Design Pattern (22)--Static content hosting mode

Source: Internet
Author: User
Tags hosting

Deploy static content to a cloud-based storage service that you can provide directly to your customers. This pattern can reduce the need for potentially expensive compute instances.

Landscape and problems


Web applications typically include elements of static content. This static content can include HTML pages and images and other resources available to the client's files, either as part of an HTML page (such as an embedded image, style sheet, and client-side JavaScript file) or as a separate download (such as a PDF document).

Despite the Web server as well as tuning requirements for efficient dynamic execution of page code and output caching optimizations, they still have to process requests to download static content. This absorption can often be better utilized in the processing cycle.

Solution Solutions


In most cloud-hosted environments, it can minimize the requirements for computing instances (for example, using smaller instances or less), by locating the resources of some applications and storing services in static Web pages. Charges for cloud-hosted storage are typically much less than compute instances.

When a host is part of an app for a storage service, the primary consideration is with the deployment of the application and the resources that ensure that it is not intended to be provided to anonymous users.

Issues and considerations


When deciding how to implement this pattern, consider the following points:
• The managed storage service must be exposed, and users can access HTTP endpoints that download static resources. Some storage services also support HTTPS, which means that it is capable of hosting business resources that require the use of SSL in storage.
• For maximum performance and availability, consider using content distribution networks (if any) to cache content in storage containers in multiple datacenters around the world. However, this will incur an additional cost of using the content delivery network.
• Storage accounts tend to geo-replication by default, providing resiliency to events that may affect the datacenter. This means that their IP address may change, but the URL will remain the same.
• Managed compute instances where content resides on a storage account can become more challenging to deploy and update applications. This may be necessary so that when the static content includes a script file or a user interface component to manage it, it is easier, especially for the deployment to execute separately, as well as the version of the application and content. However, if only static resources are to be updated they can simply be uploaded to the storage account without redeploying the application package.
• The storage service may not support the use of custom domain names. In this case, it is necessary to link the full URL of the specified resource, because they will be dynamically generated from the content containing the link in a different domain.
• The storage container must be configured for public read permissions, but it is essential to ensure that they are not configured for citizen write access to prevent users from being able to upload content. Consider using a valet key or token to control access to resources should not be anonymous, see more information about the main mode of valet.

when to use this mode


This model is ideal for:
• Minimize the site and include hosting fees for some static resource apps.
• Minimized sites contain only static content and hosting costs for resources. Depending on the capabilities of the storage system of the different managed service providers, it is possible to host a full static Web page within the entire content storage account.
• Expose the contents of static resources and other hosted environments or applications running on the local server.
• Locate content in multiple geographic regions by using content distribution networks in multiple datacenters around the world through the contents of the cached storage account.
• Monitor the use of costs and bandwidth. Some or all of the separate storage accounts that use a piece of static content allow more easily resolving costs from hosting and running.

This mode may not be suitable for the following situations:
• The application needs to do some processing of the static content before passing it to the client. For example, it may be necessary to add a timestamp to the document.
• The number of static content is very small. Retrieving the cost of the content from a separate store may be more expensive than separating it from the computational resources.

Attention:

It is sometimes possible to store a full web site that contains only static content, such as HTML pages, pictures, stylesheets, client-side JavaScript files, downloaded files, such as stored PDF files hosted in the cloud. For more information, see the effective way to deploy a static website at Microsoft Azure in the Infosys blog.

Example


BLOBs in Azure store static content and can be accessed directly from a Web browser. Azure provides storage on an HTTP-based interface that can be exposed to customers publicly. For example, a URL that uses the form of content in an Azure blob storage container is exposed:

HTTP://[Storage account name].blob.core.windows.net/[container name]/[file name]

When you upload the contents of the application, you must create one or more blobs of containers to save the files and documents. Note that the default permissions for a new container are private, and you must change the content that the public allows the client to access. If it is necessary to prevent anonymous access to the content, you can implement the valet main mode, so the user must download the resource sequentially to produce a valid token.

Attention:

The concept of a page BLOB service on an azure website contains blob storage information, and you can access it and use it in a way.



The link in each page will specify the URL of the resource, and the client will access the resource directly from the storage service. Figure 1 shows this approach.

Figure 1-The static part of the application delivered from the storage service, directly


The link to the page that is delivered to the client must specify the full URL of the BLOB container and resource. For example, a page that contains a linked image in a public container may contain the following content.

[HTML]View Plaincopy
    1. <img src="http://mystorageaccount.blob.core.windows.net/myresources/image1.png"
    2. alt="My image" />


Attention:

If the resource is protected by using a valet key, such as Azure shared access signature (SAS), the signature must be included in the URL of the link.


The examples that apply to this guide include a solution named Staticcontenthosting that demonstrates the use of externally stored static resources. The Staticcontenthosting.cloud project contains a configuration file that specifies the storage account and container that holds the static content.

[HTML]View Plaincopy
    1. <Setting name="staticcontent.storageconnectionstring"
    2. value="usedevelopmentstorage=true" />
    3. <Setting name="Staticcontent.container" value="static-content" />


In the Staticcontenthosting.web project, the file Settings.cs Settings (Settings) class contains a method to extract these values and establish a string value that contains the URL of the container for the cloud storage account.

[CSharp]View Plaincopy
  1. Public class Settings
  2. {
  3. public Static string staticcontentstorageconnectionstring {
  4. Get
  5. {
  6. return Roleenvironment.getconfigurationsettingvalue (
  7. "staticcontent.storageconnectionstring");
  8. }
  9. }
  10. public static string Staticcontentcontainer
  11. {
  12. Get
  13. {
  14. return Roleenvironment.getconfigurationsettingvalue ("Staticcontent.container");
  15. }
  16. }
  17. public static string Staticcontentbaseurl
  18. {
  19. Get
  20. {
  21. var account = Cloudstorageaccount.parse (staticcontentstorageconnectionstring);
  22. return string. Format ("{0}/{1}", account. Blobendpoint.tostring (). TrimEnd ('/'),
  23. Staticcontentcontainer.trimstart ('/'));
  24. }
  25. }
  26. }


The Staticcontenturlhtmlhelper class in the file StaticContentUrlHtmlHelper.cs exposes the named staticContentUrl if it is passed to its URL with the ASP. NET root path character (?). Starts the method of generating the URL of the cloud storage account that contains the path.

[CSharp]View Plaincopy
  1. Public static class Staticcontenturlhtmlhelper
  2. {
  3. public static string staticContentUrl (thishtmlhelper helper, string contentpath) /c4>
  4. {
  5. if (Contentpath.startswith ("~"))
  6. {
  7. Contentpath = contentpath.substring (1);
  8. }
  9. Contentpath = string. Format ("{0}/{1}", Settings.StaticContentBaseUrl.TrimEnd ('/'),
  10. Contentpath.trimstart ('/'));
  11. var url = New Urlhelper (helper.  Viewcontext.requestcontext);
  12. return URL.  Content (Contentpath);
  13. }
  14. }


The browse file index.cshtml\ home folder contains an image element that uses the staticContentUrl method to create the URL of its src attribute.

[HTML]View Plaincopy
    1. <img src="@Html. staticContentUrl (" ~/images/orderedlist1.png ")" alt="Test Image" />


This article is translated from msdn:http://msdn.microsoft.com/en-us/library/dn589776.aspx

Cloud computing Design Pattern (22)--Static content hosting mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.