SDK Installation
Environmental Difference
You can use JavaScript SDK in Jode.js environment and browser environment, but limited by the function limit and security limit of browser environment itself, in the browser environment, some functions of SDK cannot be used. The details are as follows;
- Read and write: of local files Local files cannot be operated directly in browser, so PutobjectFromFile and Getobject and other relevant APIs of BOS service cannot be used. Similarly, Node.js environment also does not have Blob object, so the PutobjectFromBlob of BOS service is exclusively developed for browser environment.
- Bucket related operation: Limited by cross-origin access of browser, corresponding CORS configuration needs to be set on server for cross-origin access. Baidu AI Cloud supports CORS configuration of object in specific bucket, but does not cross-region access to API of bucket level, so ListBuckets and other relevant APIs cannot be used in browser end; for API of bucket level, please see Bucket.
Node.js Environment
Node.js version Supported
- Version Node 4.x and above
Installation Steps
JavaScript package has been uploaded to npm manager; use npm directly to install development kit of SDK:
npm install @baiducloud/sdk
2.Then use the following in your program:
import {BosClient} from '@baiducloud/sdk';
const config = {
endpoint: <EndPoint>, //Introduce into domain name of the region where the bucket is.
credentials: {
ak: <AccessKeyID>, //Your AccessKey
sk: <SecretAccessKey> //Your AccessKey
}
};
let bucket = 'my-bucket';
let key = 'hello.js';
let client = new BosClient(config);
client.putObjectFromFile(bucket, key, __filename)
.then(response => console.log(response)) // Success
.catch(error => console.error(error)); // Failed
The interface returns a promise object
Browser Environment
Browser Version Supported
Browser type | Lowest 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
1.Bower is recommended for installation:
npm install @baiducloud/sdk
2.Load baidubce-sdk.bundle.min.js
to page via <script>
:
<script src="node_modules/@baiducloud/sdk/dist/baidubce-sdk.bundle.min.js"></script>
When js is loaded to page, the functions of sdk can be used via global variable baidubce.sdk
.
3.Then use the following in your program:
const config = {
endpoint: <EndPoint>, //Introduce into domain name of the region where the bucket is.
credentials: {
ak: <AccessKeyID>, //YourAccessKey
sk: <SecretAccessKey> //Your SecretAccessKey
}
};
let bucket = 'my-bucket';
let key = 'hello.js';
let client = new baidubce.sdk.BosClient(config);
client.putObjectFromString(bucket, key, 'hello world')
.then(response => console.log(response)) // Success
.catch(error => console.error(error)); // Failed