Baidu AI Cloud
中国站

百度智能云

Object Storage

Initialization

Determine Endpoint

BOS access domain name is:

Region Access to Endpoint Protocol
BJ bj.bcebos.com HTTP, HTTPS
GZ gz.bcebos.com HTTP, HTTPS
SU su.bcebos.com HTTP, HTTPS

Node.js End Getting Started

1.Initialize a BosClient.

BosClient is a client interacting with BOS service, and all BOS operations of BOS JavaScript SDK are completed via BosClient.

Sample code:

var bce = require('@baiducloud/sdk');
var config = {
 credentials: {
      ak: 'ak',       //Your AK
      sk: 'sk'        //Your SK
   },
 endpoint: <EndPoint> // 'https://bj.bcebos.com'
};

2.Create a bucket.

Bucket is a namespace on BOS. It is equivalent to a container of data and can store several data entities (objects). Before uploading data, you must create a bucket.

Sample code:

let newBucketName = <BucketName>;    // Create a bucket and specify the name of the bucket 
client.createBucket(newBucketName)
  .then(function() {
         // Created successfully, add your own code; 
   })
   .catch(function(error) {
        // Failed to create, add your own code to handle exception 
   });

3.Upload object.

Object is the most basic data unit in BOS. You can understand object as a file. OS provides you with four ways to upload an object: File upload, data stream upload, binary string upload and string upload.

Sample code:

function done(response) {
  // Uploaded successfully
}
function fail(fail) {
  // Failed to upload
}

// Upload in string form
client.putObjectFromString(bucket, object, 'hello world')
  .then(done)
  .catch(fail);

//  Upload in buffer form 
var buffer = new Buffer('hello world'); client.putObject(bucket, object, buffer)
  .then(done)
  .catch(fail);

// Upload in file form only supports Node.js environment 
client.putObjectFromFile(bucket, object, <path-to-file>)
  .then(done)
  .catch(fail);

// Upload in blob form only supports browser environment
client.putObjectFromBlob(bucket, object, <blob对象>)
  .then(done)
  .catch(fail);

4.View the object in bucket.

client.listObjects(<bucketName>)
  .then(function (response) {
      var contents = response.body.contents;
      for (var i = 0, l = contents.length; i < l; i++) {
          console.log(contents[i].key);
      }
  })
  .catch(function (error) {
      // Failed to inquire 
  });

Create BOSClient

BosClient is the JavaScript client of BOS service, providing a series of methods for developer to interact with BOS service. Before using SDK to initiate a request to BOS, you need to initialize a BosClient instance and make some necessary settings to it.

  • Browser end: let BosClient = baidubce.sdk.BosClient
  • Node.js end: import {BosClient} from '@baiducloud/sdk'

Access to BOS via AK/SK

1.Determine the EndPoint. EndPoint is the address of BOS service in various regions, with default domain name Beijing. http://bj.bcebos.com. 2.Introduce your AK/SK. 3.Introduce the configured config to BosClient.

Sample Code

You can create a BosClient by referring to the following code:

let config = {
    endpoint: <EndPoint>,
    credentials: {
        ak: <AccessKeyID>,           //Your AK
        sk: <SecretAccessKey>       //Your SK
    }

let client = new BosClient(config);

Note: EndPoint parameter is the BOS access domain name, which can only be defined with the domain name of specified region, and if no domain name is specified, it is Beijing by default. BOS access domain name supports 2 calling methods, HTTP and HTTPS, and to improve the data security, it is recommended to use https://bj.bcebos.com. Baidu AI Cloud currently has opened access to multi-region support, please refer to Region Selection Description.

Access to BOS via sts

BOS can implement temporary authorized access by a third party through the STS mechanism. STS (Security Token Service) is a temporary authorized service provided by Baidu AI Cloud; for more information, please see Introduction to Use of Baidu AI Cloud STS. With STS, you can issue a third-party user with a custom time-based and privileged access credential. The third-party user can use the access credential to directly call API or SDK of Baidu AI Cloud to access its resources.

To access to BOS via sts, users apply for a set of AK, SK and token via sts-client first, and then configure that set of parameters to BosClient, and users can create a BosClient by reference to the following codes:

  • Node.js end:
const {BosClient} = require('@baiducloud/sdk');

var bosConfig = {
    credentials: {
        ak: '{accessKeyId}', // Temporary ak issued by STS server 
        sk: '{secretAccessKey}' // Temporary sk issued by STS server 
    },
    sessionToken: '{sessionToken}',  // Temporary sessionToken issued by STS server 
    endpoint: 'http://bj.bcebos.com'
};

var client = new BosClient(bosConfig);
  • Browser end:
var bosConfig = {
    credentials: {
        ak: '{accessKeyId}', // Temporary ak issued by STS server 
        sk: '{secretAccessKey}' // Temporary sk issued by STS server 
    },
    sessionToken: '{sessionToken}',  // Temporary sessionToken issued by STS server 
    endpoint: 'http://bj.bcebos.com'
};

var client = new baidubce.sdk.BosClient(bosConfig);
Previous
SDK Installation
Next
Bucket Management