Resource groups (Java SDK V2)
Manage the resource group of a bucket by calling the PutBucketResourceGroup and GetBucketResourceGroup operations with OSS Java SDK V2.
Prerequisites
-
OSS Java SDK V2 is installed. If you use Maven, add the
com.aliyun:alibabacloud-oss-v2dependency to pom.xml. -
Access credentials are configured by using environment variables. The sample code in this topic uses
EnvironmentVariableCredentialsProviderto read credentials from theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables.
Permissions
Alibaba Cloud accounts have all permissions by default. RAM users and RAM roles under an Alibaba Cloud account have no permissions by default. Permissions must be granted by the Alibaba Cloud account or an account administrator.
|
API |
Action |
Description |
|
PutBucketResourceGroup |
|
Set or query the resource group of a bucket. |
Sample code
import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
public class BucketResourceGroupSample {
public static void main(String[] args) throws Exception {
String region = "cn-hangzhou";
String bucket = "examplebucket";
String resourceGroupId = "rg-aek27tc****";
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
OSSClientBuilder clientBuilder = OSSClient.newBuilder()
.credentialsProvider(provider)
.region(region);
try (OSSClient client = clientBuilder.build()) {
// Set the resource group
BucketResourceGroupConfiguration config = BucketResourceGroupConfiguration.newBuilder()
.resourceGroupId(resourceGroupId)
.build();
PutBucketResourceGroupResult putResult = client.putBucketResourceGroup(
PutBucketResourceGroupRequest.newBuilder()
.bucket(bucket)
.bucketResourceGroupConfiguration(config)
.build());
System.out.printf("PutBucketResourceGroup status code:%d%n", putResult.statusCode());
// Get the resource group
GetBucketResourceGroupResult getResult = client.getBucketResourceGroup(
GetBucketResourceGroupRequest.newBuilder()
.bucket(bucket)
.build());
System.out.printf("Resource group ID: %s%n",
getResult.bucketResourceGroupConfiguration().resourceGroupId());
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
-
For the synchronous PutBucketResourceGroup sample code, see PutBucketResourceGroup.java.
-
For the asynchronous PutBucketResourceGroup sample code, see PutBucketResourceGroupAsync.java.
-
For the synchronous GetBucketResourceGroup sample code, see GetBucketResourceGroup.java.
-
For the asynchronous GetBucketResourceGroup sample code, see GetBucketResourceGroupAsync.java.