Initialization
Confirm Endpoint
- Before confirming the Endpoint you configured when using the SDK, read the section of the Developer Guide on BOS Access Domain Name to understand Endpoint-related concepts.
- Baidu AI Cloud currently has opened access to multi-region support, please refer to Region Selection Instructions.
- Currently, it supports "North China-Beijing", "South China-Guangzhou" and "East China-Suzhou". Beijing:
http://bj.bcebos.com
; Guangzhou:http://gz.bcebos.com
; Suzhou:http://su.bcebos.com
The corresponding information is:
Access region | Corresponding Endpoint |
---|---|
BJ | bj.bcebos.com |
GZ | gz.bcebos.com |
SU | su.bcebos.com |
Get the Key
To use Baidu AI Cloud BOS, you need to have a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. AK/SK is assigned to users by the system and is a string to identify users and verify signatures for accessing BOS.
You can get and understand your AK/SK information through the following steps:
1.Register Baidu AI Cloud Account
Create BOSClient
BosClient is the client of BOS service, providing methods for developers to interact with BOS service.
Use AK/SK to Create BosClient
Access BOS through AK/SK. You can refer to the following code to create BosClient:
#Use Ruby SDK, and introduce bos_client and Baidubce module
require 'baidubce/services/bos/bos_client'
include Baidubce
#Configure client parameters
credentials = Auth:BceCredentials.new(
"accessKeyId",
"secretAccessKey"
)
conf = BceClientConfiguration.new(
credentials,
"ENDPOINT"
)
#Create BOSClient.
client = Services:BosClient.new(conf)
Note:
1.In the codes above,
accessKeyId
corresponds to "Access Key ID" in console andsecretAccessKey
corresponds to "Access Key Secret" in console; for the way of obtaining, please see the <Operation Guide Manage ACCESSKEY>. 2.You can specify their own domain name by introducing ENDPOINT parameter, which needs to be defined with the domain name of specified region, for example, if the service region is Beijing, the domain name ishttp://bj.bcebos.com
.
Create BosClient with STS
Apply for STS Token
BOS can implement temporary authorized access by a third party through the STS mechanism. STS (Security Token Service) is a temporary authorization service provided by Baidu AI Cloud. 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 BOS via STS, users need to apply for an authentication string via the client of STS; for the application method, see Introduction to Use of Baidu AI Cloud STS.
Create a BOSClient with STS Token
After applying for STS, configure STStoken to BosClient, and users can create a BosClient by reference to the following codes:
1.First perform endpoint configuration for STS. The configuration example of STS is as follows:
```
require 'baidubce/services/sts/sts_client'
require 'baidubce/services/bos/bos_client'
credentials = Baidubce:Auth:BceCredentials.new(
"your ak",
"your sk"
)
sts_conf = Baidubce:BceClientConfiguration.new(
credentials,
"http://sts.bj.baidubce.com"
)
```
2.The sample code of StsClient is as follows:
```
#Create StsClient
sts_client = Baidubce:Services:StsClient.new(sts_conf)
acl = {
id: '8c47a952db4444c5a097b41be3f24c94',
accessControlList: [
{
eid: 'shj',
service: 'bce:bos',
region: 'bj',
effect: 'Allow',
resource: ["bos-demo"],
permission: ["READ"]
}
]
}
#The DurationSeconds Refers to the Expiration Time, and if It Is a Non-int Value or Such Parameter Is not Set, the Default Value of 12 Hours Is Used as the Expiration Time
#sts_client.get_session_token(acl, "test")
#sts_client.get_session_token(acl, 1024)
sts_response = sts_client.get_session_token(acl)
sts_ak = sts_response["accessKeyId"]
sts_sk = sts_response['secretAccessKey']
token = sts_response['sessionToken']
```
Note: Where acl refers to the user defined acl, please see Access control for grammar.
3.Apply accessKeyID/secretAccessKey/sessionToken obtained to create a new BosClient.
```
# Use ak, sk and token obtained to create BosClient to access BOS
sts_credentials = Baidubce:Auth:BceCredentials.new(
sts_ak,
sts_sk,
token
)
conf = Baidubce:BceClientConfiguration.new(
sts_credentials,
"http://bj.bcebos.com",
)
client = Baidubce:Services:BosClient.new(conf)
```
Note: Currently, when STS is used to configure client, wherever the endpoint of the corresponding BOS service is, endpoint needs to be configured as http://sts.bj.baidubce.com
.
Configure HTTPS to Access BOS
BOS supports HTTPS transport protocol, you can use HTTPS to access BOS service in BOS Ruby SDK through the following two modes:
-
Specify HTTPS in endpoint.
#Configure client parameters credentials = Auth:BceCredentials.new( "accessKeyId", "secretAccessKey" ) conf = BceClientConfiguration.new( credentials, "https://bj.bcebos.com" ) #Create BOSClient client = Services:BosClient.new(conf)
-
Set HTTPS protocol by specifying
https
inprotocol
#Configure client parameters credentials = Auth:BceCredentials.new( "accessKeyId", "secretAccessKey" ) options = { 'protocol' => 'https' } conf = BceClientConfiguration.new( credentials, "bj.bcebos.com", options ) #Create BOSClient client = Services:BosClient.new(conf)
Note: If you specify scheme of endpoint while specifying protocol parameter, endpoint should prevail.
Configure Customized Domain Name/Backup Domain Name to Access BOS
Use the customized domain name
If you want to use the customized domain name as the ENDPOINT to access BOS, after binding the customized domain name and a certain bucket of BOS in the console, you can configure ENDPOINT as the customized domain name and open the switch of cname_enabled
, such as cdn-test.cdn.bcebos.com
, and configure the codes as follows:
require 'baidubce/services/bos/bos_client'
include Baidubce
#Configure client parameters
credentials = Auth::BceCredentials.new(
"accessKeyId",
"secretAccessKey"
)
options = {
'cname_enabled' => true
}
conf = BceClientConfiguration.new(
credentials,
"cdn-test.cdn.bcebos.com",
options
)
#Create a BOSClient.
client = Services::BosClient.new(conf)
Use the backup domain name
On the basis of using the official domain name and customized domain name as ENDPOINT to access BOS, SDK supports the retry of backup domain name mechanism further when you fail in using ENDPOINT main domain name to access BOS, with the use method shown as follows:
// If the access to BOS through ENDPOINT fails, SDK will retry backup_endpoint domain name automatically
options = {
'backup_endpoint' => "bj.bcebos.com"
}
conf = BceClientConfiguration.new(
credentials,
"cdn.bcebos.com",
options
)
#Create a BOSClient.
client = Services::BosClient.new(conf)
Configure BosClient
Set Custom Parameters
Ruby SDK sets some basic parameters by default, if the user wants to modify the values of parameters, and create their own parameter configuration, and introduce them when constructing BosClient, with the introduction code reference as follows:
#Configure customized parameters
options = {
'protocol' => 'https',
'read_timeout_in_millis' => 1000 * 60,
'region' => 'bj'
}
conf = BceClientConfiguration.new(
credentials,
"http://bj.bcebos.com",
options
)
#Create BOSClient
client = Services:BosClient.new(conf)
The parameters are described as follows:
Parameter | Description | Default value |
---|---|---|
protocol | Protocol | http |
region | Region | bj |
open_timeout_in_millis | Request timeout (unit: millisecond) | 50 * 1000 |
read_timeout_in_millis | Timeout for transmitting data over open connections (unit: millisecond) | 10_60_1000 (The file size and network speed need to be evaluate during the setting; otherwise, the timeout occurs when large files are uploaded) |
send_buf_size | Send buffer size | 1024 * 1024 |
recv_buf_size | Receive buffer size | 10 1024 1024 |
Set Optional Parameters
BosClient can encapsulate the optional parameters into options
. Please refer to the application method for specific interface for the optional parameters of each method. Now, take the put_object_from_string
as an example, and refer to the following code to set the optional parameters:
#Use options to introduce the specified parameter when uploading object
user_metadata = { "key1" => "value1" }
options = { Http:CONTENT_TYPE => 'string',
"key2" => "value2",
'Content-Disposition' => 'inline',
'user-metadata' => user_metadata
}
client.put_object_from_string(bucket_name, object_name, "obj_str", options)