Install SDK
Updated at:2025-11-03
Environmental differences
You can utilize the JavaScript SDK in both Node.js and browser environments. However, due to the functional and security restrictions inherent to the browser environment, certain features of the SDK will not be available in the browser. The details are as follows:
- Local file operations: Browsers do not have direct access to local files, rendering certain BOS service APIs like PutObjectFromFile and GetObject unavailable. On the other hand, the Node.js environment lacks support for Blob objects, which led to the creation of PutObjectFromBlob for exclusive use in the browser environment.
- Bucket-related operations: Due to browser cross-origin access restrictions, corresponding CORS configurations need to be set on the server side to enable cross-origin access. Baidu AI Cloud supports CORS configurations for objects in specific buckets but does not support cross-origin access to bucket-level APIs. Therefore, APIs such as ListBuckets cannot be used on the browser side. For Bucket-level APIs, please refer to [Bucket](BOS/API Reference/Bucket-Related Interface/Event notification/PutNotification.md).
Node.js environment
Supported Node.js Versions
- Node 4.x and higher versions
Installation steps
- The JavaScript package has been uploaded to the npm package manager. Install the SDK directly using npm:
npm install @baiducloud/sdk
-
Then use it in your program:
JavaScript1var sdk = require('@baiducloud/sdk'); 2var BosClient = sdk.BosClient; 3const config = { 4 endpoint: <EndPoint>, //Pass in the domain name of the region where the bucket is located 5 credentials: { 6 ak: <AccessKeyID>, //Your AccessKey 7 sk: <SecretAccessKey> //Your SecretAccessKey 8 } 9}; 10let bucket = 'my-bucket'; 11let key = 'hello.js'; 12let client = new BosClient(config); 13client.putObjectFromFile(bucket, key, __filename) 14 .then(response => console.log(response)) // Success 15 .catch(error => console.error(error)); // Failure
The API will return a Promise object.
Browser environment
Supported Browser Versions
| Browser type | Minimum version |
|---|---|
| Google Chrome | 28.0+ |
| Microsoft Internet Explorer | 10.0 |
| Mozilla Firefox | 23.0+ |
| Apple Safari | 5.1+ |
| Opera | 17.0+ |
| Android Browser | 4.3+ |
Installation steps
- It is recommended to install using npm:
npm install @baiducloud/sdk
Plain Text
1Or directly reference it via CDN
2
3 <script src="https://bce.bdstatic.com/lib/@baiducloud/sdk/1.0.0-rc.40/baidubce-sdk.bundle.min.js"></script>
-
If installing via npm, you need to use
<script>to loadbaidubce-sdk.bundle.min.jsinto the page:Plain Text1<script src="node_modules/@baiducloud/sdk/dist/baidubce-sdk.bundle.min.js"></script>After the JS is loaded into the page, you can use the SDK functions through the global variable
baidubce.sdk. -
Then use it in your program:
JavaScript1const config = { 2 endpoint: <EndPoint>, //Pass in the domain name of the region where the bucket is located 3 credentials: { 4 ak: <AccessKeyID>, //Your AccessKey 5 sk: <SecretAccessKey> //Your SecretAccessKey 6 } 7}; 8let bucket = 'my-bucket'; 9let key = 'hello.js'; 10let client = new baidubce.sdk.BosClient(config); 11client.putObjectFromString(bucket, key, 'hello world') 12 .then(response => console.log(response)) // Success 13 .catch(error => console.error(error)); // Failure
