Help Center/ Object Storage Service/ Best Practices/ Accessing OBS from ECS Instances Using Temporary Security Credentials of Agencies
Updated on 2026-11-17 GMT+08:00

Accessing OBS from ECS Instances Using Temporary Security Credentials of Agencies

Scenarios

Assume that you are a developer who uses OBS SDK to work on an application that runs on an ECS instance. The code in the application needs to call OBS APIs. OBS requires access credentials from requesters. Therefore, your API calls will need to provide access credentials.

There are temporary security credentials and permanent security credentials (such as username and password). Temporary security credentials are more secure because they have limited validity periods and a high refresh frequency. Your application can obtain temporary security credentials through an agency created for the ECS service on the IAM console to access the cloud service in a more secure way.

Figure 1 Application obtains temporary security credentials to access OBS

As shown in Figure 1, OBS requires the requester to provide access credentials, so the application needs to obtain the temporary AK/SK of the agency. The application sends a request to the ECS metadata service. The ECS metadata service obtains the temporary AK/SK of the agency from IAM and then returns the AK/SK to the application. The application then provides the AK/SK to OBS. OBS allows access only after verifying that the AK/SK is valid.

Solution

To enable applications running on ECS instances to obtain the temporary security credentials of agencies, create agencies for the ECS service on the IAM console and specify the permissions and scope for the agencies. Configure the agencies for the ECS instances. Then, the applications can apply for the temporary security credentials of the agencies to access Huawei Cloud resources based on the assigned permissions. The details are as follows:

  1. On the IAM console, create agencies with permissions delegated to the ECS service and select the permissions and scope for the agencies.
  2. Configure the created agencies for the ECS instances. Only one agency can be configured for an ECS.
  3. Obtain the temporary security credentials of agencies. After the agencies are configured for the ECS instances, the instances obtain the permissions assigned to the agencies. Then, the applications on the ECS instances can obtain the temporary security credentials of the agencies and communicate with OBS based on the assigned permissions in the authorized scope.
Figure 2 ECS agency

Operation Process

Figure 3 Obtaining the temporary AK/SK for an ECS instance

Procedure

As shown in Figure 3, obtaining the temporary security credentials for an ECS instance requires three steps: create an agency for the ECS service, configure the agency for the ECS instance, and obtain the temporary security credentials. The procedure is as follows:

Step 1: Create an Agency for the ECS Service

  1. Access the Create Agency page on the IAM console as an administrator.
  2. On the Create Agency page, enter an agency name.
  3. Select Cloud service for Agency Type and Elastic Cloud Server (ECS) and Bare Metal Server (BMS) for Cloud Service.

    Figure 4 Creating an agency

  4. Select a validity period.
  5. (Optional) Enter a description for the agency. This option is recommended.
  6. Click OK.
  7. Click Next. The page for authorizing the agency is displayed.
  8. In the displayed dialog box, click Authorize.
  9. Select the permissions to assign to the agency. In this example, select the OBS OperateAccess permission.
  10. Click Next and select the authorized scope. In this example, select All resources.
  11. Click OK. The agency is created.
  12. Wait for 10 to 15 minutes for the agency authorization to take effect.

Step 2: Configure the Agency for an ECS Instance

  1. On the ECS console, click the ECS name to go to the summary page.

    Figure 6 Clicking the ECS name

  2. In the Management Information area, click to select an agency for the ECS instance.

    Figure 7 Selecting an agency for the ECS instance

  3. Select the agency created in Step 1 based on the agency name.

    Figure 8 Selecting the agency

  4. Click to complete the configuration.

Step 3: Obtain the Temporary Credentials of the Agency and Use the OBS Java SDK to Access OBS

  1. Download and install the OBS Java SDK.
  2. Create and initialize an OBS SDK client and use EcsObsCredentialsProvider to automatically obtain temporary access keys from the ECS instance. The access credentials are periodically updated automatically. For more information about SDK initialization, see Initialization (SDK for Java).

    // Replace the following region with the one in use. CN-Hong Kong is used here as an example.
    String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com";
    // Create an ObsClient instance.
    ObsClient obsClient = new ObsClient(new EcsObsCredentialsProvider(), endPoint);
    
    // Use the instance to access OBS.
    
    // Close ObsClient.
    obsClient.close();

  3. Use the OBS client to call OBS SDK APIs. For details, see OBS Java SDK APIs.