| copyright |
|
||
|---|---|---|---|
| lastupdated | 2018-08-16 |
{:new_window: target="_blank"} {:shortdesc: .shortdesc} {:screen: .screen} {:codeblock: .codeblock} {:pre: .pre}
{: #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}
Be sure that you have the following prerequisites ready to go:
-
You must have an {{site.data.keyword.cloud_notm}} account
.
-
You must have the {{site.data.keyword.cos_short}} SDK for Node.js
.
-
You must have Node 4.x+.
-
Locate the credential key values to be used later for SDK initialization:
- endpoint - The public endpoint for your Cloud Object Storage. The endpoint is available from the {{site.data.keyword.cloud_notm}} Dashboard
.
- api-key - The API key that is generated when the service credentials are created. Write access is required for creation and deletion examples.
- resource-instance-id - The resource ID for your Cloud Object Storage. The resource ID is available through the {{site.data.keyword.cloud_notm}} CLI or {{site.data.keyword.cloud_notm}} Dashboard
.
- endpoint - The public endpoint for your Cloud Object Storage. The endpoint is available from the {{site.data.keyword.cloud_notm}} Dashboard
{: #create-instance}
- In the {{site.data.keyword.cloud_notm}} catalog, select the Storage category, and click {{site.data.keyword.cos_short}}. The service configuration page opens.
- Give your service instance a name, or use the preset name.
- Select your pricing plan and click Create. Your Object Storage instance page opens.
- In the navigation menu, select Service credentials.
- In the Service credentials page, click New credential.
- 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.
{: #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}
{: #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.
- Load the client library by adding the following
requiredefinitions to yourserver.jsfile.
var objectStore = require('ibm-cos-sdk');{: codeblock}
- 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}
- Add the following code to your
server.jsfile.
var cos = new objectStore(config);{: codeblock}
{: #basic_operations}
function doCreateBucket() {
console.log('Creating bucket');
return cos.createBucket({
Bucket: 'my-bucket',
CreateBucketConfiguration: {
LocationConstraint: 'us-standard'
},
}).promise();
}{: codeblock}
function doCreateObject() {
console.log('Creating object');
return cos.putObject({
Bucket: 'my-bucket',
Key: 'foo',
Body: 'bar'
}).promise();
}{: codeblock}
function doGetObject() {
console.log('Getting object');
return cos.getObject({
Bucket: 'my-bucket',
Key: 'foo'
}).createReadStream().pipe(fs.createWriteStream('./MyObject'));
}{: codeblock}
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.
{: #test}
Is everything set up correctly? Test it out!
- Run your application, making sure to start the initialization and respective operations, such as creating a bucket and adding data to the bucket.
- Return to the {{site.data.keyword.cos_short}} service instance that you previously created in your web browser, and open the service dashboard.
- 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 notoc}
Great job! You added a level of secure persistence to your app. Keep the momentum by trying one of the following options:
- View the {{site.data.keyword.cos_short}} SDK for Node.js
source code.
- Check out the example code for bucket and object operations
- Starter Kits are one of the fastest ways to use the capabilities of {{site.data.keyword.cloud_notm}}. View the available starter kits in the Mobile developer dashboard
. Download the code. Run the app!
- To learn more about and take advantage of all of the features that {{site.data.keyword.cos_short}} offers, check out the docs!