copyright | lastupdated | ||
---|---|---|---|
|
2017-10-27 |
{:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
This tutorial walks you through how to host and serve website assets (images, videos, documents) and user generated content in a Cloud Object Storage, and how to use a Content Delivery Network (CDN) for fast and secure delivery to users around the world.
{: #objectives}
- Create a Cloud Object Storage S3 bucket.
- Upload files to a bucket.
- Make the content globally available with a CDN.
- Expose files by using a Cloud Foundry web application.
This tutorial uses the following products:
{: #prereqs}
-
Contact your Infrastructure master user to get the following permissions:
- Manage CDN Account
- Manage Storage
- API Key
These permissions are required to be able to view and use the Storage and CDN services.
-
Ensure that you have access to storage resources in the {{site.data.keyword.Bluemix}} console:
- Go to https://control.bluemix.net.
- Confirm that you can see the Storage > Object Storage sections.
{: #get_code}
This section uses a simple web application that links to the files (css, images and videos) served by a CDN.
To start, retrieve the application code:
git clone https://github.com/IBM-Cloud/webapp-with-cos-and-cdn
{: pre}
{: #create_cos}
Cloud Object Storage provides flexible, cost-effective, and scalable cloud storage for unstructured data.
- Go to the catalog in the console, and select Object Storage from the Storage section.
- Click Create and Create.
- Click Create Bucket.
- Set the bucket name to
mywebsite
and click Create. Avoid dots (.) in the bucket name.
{: #upload}
In this section, we'll use the command line tool curl to upload files to the bucket.
- Log in to {{site.data.keyword.Bluemix_notm}} from the CLI and get a token from IBM Cloud IAM.
{: pre}
bx login bx iam oauth-tokens
- Copy the token from the output of the command in the previous step.
{: screen}
IAM token: Bearer <token>
- Set the value of the token and bucket name to an environment variable for easy access.
{: pre}
export IAM_TOKEN=<REPLACE_WITH_TOKEN> export BUCKET_NAME=<REPLACE_WITH_BUCKET_NAME>
- Upload the files named
a-css-file.css
,a-picture.png
, anda-video.mp4
from the content directory of the web application code you downloaded previously. Upload the files to the root of the bucket.
cd content
{: pre}
curl -X "PUT" \
"https://s3-api.us-geo.objectstorage.softlayer.net/$BUCKET_NAME/a-picture.png" \
-H "x-amz-acl: public-read" \
-H "Authorization: Bearer $IAM_TOKEN" \
-H "Content-Type: image/png" \
-T a-picture.png
{: pre}
curl -X "PUT" \
"https://s3-api.us-geo.objectstorage.softlayer.net/$BUCKET_NAME/a-css-file.css" \
-H "x-amz-acl: public-read" \
-H "Authorization: Bearer $IAM_TOKEN" \
-H "Content-Type: text/css" \
-T a-css-file.css
{: pre}
curl -X "PUT" \
"https://s3-api.us-geo.objectstorage.softlayer.net/$BUCKET_NAME/a-video.mp4" \
-H "x-amz-acl: public-read" \
-H "Authorization: Bearer $IAM_TOKEN" \
-H "Content-Type: video/mp4" \
-T a-video.mp4
{: pre}
5. View your files from your dashboard.
6. Access the files through your browser by using a link similar to the following example:
http://s3-api.us-geo.objectstorage.softlayer.net/YOUR_BUCKET_NAME/a-picture.png
In this section, we will create a CDN service. The CDN service distributes content where it is needed. The first time content is requested, it’s pulled from the host server (our bucket in Cloud Object Storage) to the network and stays there for other users to access it quickly without the network latency to reach the host server again.
- Go to the catalog in the console, and select Content Delivery Network from the Network section. This CDN is powered by Akamai.
- Create a Content Delivery Network instance.
- Select Akamai as the CDN Provider and click Start Provision.
- Set the hostname for the CDN to your custom domain. Although you set a custom domain, you can still access the CDN contents through the IBM provided CNAME. So if you don't plan to use custom domain, you can set an arbitrary name.
- Set the Custom CNAME prefix. Do not use dots "." in the name.
- Leave the path empty.
- Select Object Storage as Origin.
- Set the endpoint to your bucket API endpoint, such as s3-api.us-geo.objectstorage.softlayer.net.
- Set the bucket name to your-bucket-name.
- Enable both HTTP (80) and HTTPS (443) ports.
- Click Create.
- Select the CDN instance in the list at https://control.bluemix.net/network/cdn.
- The Details panel shows the CNAME for your CDN.
- Access your file with https://your-cdn-cname.cdnedge.bluemix.net/a-picture.png. If you omit the file name, you should see the S3 ListBucketResult instead.
The application contains a public/index.html web page that includes references to the files now hosted in the Cloud Object Storage. The backend app.js
serves this web page and replaces a placeholder with the actual location of your CDN. This way, all assets that are used by the web page are served by the CDN.
- From a terminal, go in the directory where you checked out the code.
{: pre}
cd webapp-with-cos-and-cdn
- Push the application without starting it.
{: pre}
bx cf push --no-start
- Configure the CDN_NAME environment variable so the app can reference the CDN contents.
{: pre}
bx cf set-env webapp-with-cos-and-cdn CDN_CNAME your-cdn.cdnedge.bluemix.net
- Start the app.
{: pre}
bx cf start webapp-with-cos-and-cdn
- Access the app with your web browser, the page stylesheet, a picture and a video are loaded from the CDN.
Using a CDN with an Object Storage is a powerful combination which lets you host files and serve them to users from around the world. You can also use Object Storage to store any files your users upload to your application.