Skip to content

Latest commit

 

History

History
169 lines (134 loc) · 8.01 KB

File metadata and controls

169 lines (134 loc) · 8.01 KB
copyright
years
2018
lastupdated 2018-08-16

{:new_window: target="_blank"} {:shortdesc: .shortdesc} {:screen: .screen} {:codeblock: .codeblock} {:pre: .pre}

Storing static content in Object Storage

{: #object}

{{site.data.keyword.cos_full_notm}} is a fundamental component of cloud computing and provides powerful capabilities to Apple developers and their applications. Unlike storing information in a file hierarchy (such as Block or File storage), an object store consists only of the files and their metadata. These files are stored in collections that are known as buckets. By definition, these objects are immutable, which makes them perfect for data such as images, videos, and other static documents. For data that either changes often or is relational data, you can use the {{site.data.keyword.cloudant_short_notm}} database service.

{{site.data.keyword.cos_short}} (COS) is a storage system that can be used to store unstructured data that is flexible, cost-effective, and scalable. The data is accessible through SDKs or by using the IBM user interface. You can use {{site.data.keyword.cos_short}} to access your unstructured data through a self-service portal that is backed by RESTful APIs and SDKs.

Before you begin

{: #before}

Be sure that you have the following prerequisites ready to go:

  1. You must have an {{site.data.keyword.cloud_notm}} account External link icon.

  2. You must have the {{site.data.keyword.cos_short}} SDK for Node.js External link icon.

  3. You must have Node 4.x+.

  4. Locate the credential key values to be used later for SDK initialization:

Step 1. Creating an instance of {{site.data.keyword.cos_short}}

{: #create-instance}

  1. In the {{site.data.keyword.cloud_notm}} catalog, select the Storage category, and click {{site.data.keyword.cos_short}}. The service configuration page opens.
  2. Give your service instance a name, or use the preset name.
  3. Select your pricing plan and click Create. Your Object Storage instance page opens.
  4. In the navigation menu, select Service credentials.
  5. In the Service credentials page, click New credential.
  6. In the Add new credential page, be sure that the role is set to Writer, and then click Add. The new credential is created and shown on the Service credentials page.

Step 2. Installing the SDK

{: #install}

Install the {{site.data.keyword.cos_short}} SDK for Node.js by using the npm package manager from the command line:

npm install ibm-cos-sdk

{: pre}

Step 3. Initializing the SDK

{: #initialize}

After you initialize the SDK in your app, you can by using {{site.data.keyword.cos_short}} to store data. Initialize your connection by supplying your credentials and providing a callback function to run when everything is ready.

  1. Load the client library by adding the following require definitions to your server.js file.
var objectStore = require('ibm-cos-sdk');

{: codeblock}

  1. Initialize the client library by supplying your credentials.
var config = {
  endpoint: '<endpoint>',
  apiKeyId: '<api-key>',
  ibmAuthEndpoint: 'https://iam.ng.bluemix.net/oidc/token',
  serviceInstanceId: '<resource-instance-id>',
};

{: codeblock}

If you need help finding the credential key values for your app, check step 4 of the Before you begin section for details on where to find them. {: tip}

  1. Add the following code to your server.js file.
var cos = new objectStore(config);

{: codeblock}

Managing data with basic operations

{: #basic_operations}

Creating a bucket

function doCreateBucket() {
    console.log('Creating bucket');
    return cos.createBucket({
        Bucket: 'my-bucket',
        CreateBucketConfiguration: {
          LocationConstraint: 'us-standard'
        },
    }).promise();
}

{: codeblock}

Creating/uploading or overwriting an object

function doCreateObject() {
    console.log('Creating object');
    return cos.putObject({
        Bucket: 'my-bucket',
        Key: 'foo',
        Body: 'bar'
    }).promise();
}

{: codeblock}

Downloading an object

function doGetObject() {
 console.log('Getting object');
 return cos.getObject({
   Bucket: 'my-bucket',
   Key: 'foo'
 }).createReadStream().pipe(fs.createWriteStream('./MyObject'));
}

{: codeblock}

Deleting an object

function doDeleteObject() {
    console.log('Deleting object');
    return cos.deleteObject({
        Bucket: 'my-bucket',
        Key: 'foo'
    }).promise();
}

{: codeblock}

Check out the full documentation for multipart uploads, security features, and other operations.

Step 4. Testing your app

{: #test}

Is everything set up correctly? Test it out!

  1. Run your application, making sure to start the initialization and respective operations, such as creating a bucket and adding data to the bucket.
  2. Return to the {{site.data.keyword.cos_short}} service instance that you previously created in your web browser, and open the service dashboard.
  3. Select the bucket that is used, and you see your newly created objects in the dashboard.

Having trouble? Check out the {{site.data.keyword.cos_short}} API Reference{:new_window}.

Next steps

{: #next notoc}

Great job! You added a level of secure persistence to your app. Keep the momentum by trying one of the following options: