本教程详细介绍如何使用Alibaba Cloud SDK for Java对资源包进行查询、创建、续费、升级等操作。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见阿里云开发工具包(SDK)
    <dependencies>
         <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <version>4.4.3</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-bssopenapi -->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-bssopenapi</artifactId>
             <version>1.4.2</version>
         </dependency>
    </dependencies>

示例代码

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.bssopenapi.model.v20171214.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 对资源包进行查询、创建、续费、升级等操作
 */
public class ResourcePackageRelatedDemo {

    /**
     * 查询某个资源包服务详情
     */
    private DescribeResourcePackageProductResponse describeResourcePackageProduct(DescribeResourcePackageProductRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 资源包产品询价
     */
    private GetResourcePackagePriceResponse getResourcePackagePrice(GetResourcePackagePriceRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 创建资源包
     */
    private CreateResourcePackageResponse createResourcePackage(CreateResourcePackageRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 查询拥有的资源包列表
     */
    private QueryResourcePackageInstancesResponse queryResourcePackageInstances(QueryResourcePackageInstancesRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 资源包续费
     */
    private RenewResourcePackageResponse renewResourcePackage(RenewResourcePackageRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 资源包升级
     */
    private UpgradeResourcePackageResponse upgradeResourcePackage(UpgradeResourcePackageRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }

    public static void main(String[] args) {
        // 设置鉴权参数,初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-qingdao",// 地域ID
                "your-access-key-id",// 您的AccessKey ID
                "your-access-key-secret");// 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        ResourcePackageRelatedDemo resourcePackageRelatedDemo = new ResourcePackageRelatedDemo();

        // 查询某个资源包服务详情
        DescribeResourcePackageProductRequest describeResourcePackageProductRequest = new DescribeResourcePackageProductRequest();
        // 资源包产品code
        describeResourcePackageProductRequest.setProductCode("ossbag");
        DescribeResourcePackageProductResponse describeResourcePackageProductResponse = resourcePackageRelatedDemo.describeResourcePackageProduct(describeResourcePackageProductRequest, client);
        log_print("describeResourcePackageProduct", describeResourcePackageProductResponse);

        // 资源包产品询价
        GetResourcePackagePriceRequest getResourcePackagePriceRequest = new GetResourcePackagePriceRequest();
        // 资源包产品code
        getResourcePackagePriceRequest.setProductCode("ossbag");
        // 资源包类型,标准存储包(华北2)
        getResourcePackagePriceRequest.setPackageType("FPT_ossbag_absolute_Storage_bj");
        // 资源包规格
        getResourcePackagePriceRequest.setSpecification("500");
        // 购买时长
        getResourcePackagePriceRequest.setDuration(1);
        GetResourcePackagePriceResponse resourcePackagePriceResponse = resourcePackageRelatedDemo.getResourcePackagePrice(getResourcePackagePriceRequest, client);
        log_print("getResourcePackagePrice",resourcePackagePriceResponse);

        // 创建资源包
        CreateResourcePackageRequest createResourcePackageRequest = new CreateResourcePackageRequest();
        createResourcePackageRequest.setProductCode("ossbag");
        createResourcePackageRequest.setPackageType("FPT_ossbag_absolute_Storage_bj");
        createResourcePackageRequest.setSpecification("500");
        createResourcePackageRequest.setDuration(1);
        CreateResourcePackageResponse createResourcePackageResponse = resourcePackageRelatedDemo.createResourcePackage(createResourcePackageRequest, client);
        log_print("createResourcePackage",createResourcePackageResponse);

        // 查询拥有的资源包列表
        QueryResourcePackageInstancesRequest queryResourcePackageInstancesRequest = new QueryResourcePackageInstancesRequest();
        queryResourcePackageInstancesRequest.setProductCode("ossbag");
        QueryResourcePackageInstancesResponse queryResourcePackageInstancesResponse = resourcePackageRelatedDemo.queryResourcePackageInstances(queryResourcePackageInstancesRequest, client);
        log_print("queryResourcePackageInstances",queryResourcePackageInstancesResponse);

        // 资源包续费
        RenewResourcePackageRequest renewResourcePackageRequest = new RenewResourcePackageRequest();
        renewResourcePackageRequest.setInstanceId("OSSBAG-cn-v641c8jbxxxx");
        renewResourcePackageRequest.setDuration(2);
        renewResourcePackageRequest.setPricingCycle("Month");
        RenewResourcePackageResponse renewResourcePackageResponse = resourcePackageRelatedDemo.renewResourcePackage(renewResourcePackageRequest, client);
        log_print("renewResourcePackage",renewResourcePackageResponse);

        // 资源包升级
        UpgradeResourcePackageRequest upgradeResourcePackageRequest = new UpgradeResourcePackageRequest();
        upgradeResourcePackageRequest.setInstanceId("OSSBAG-cn-v641c8jbxxxx");
        upgradeResourcePackageRequest.setSpecification("5000");
        UpgradeResourcePackageResponse upgradeResourcePackageResponse = resourcePackageRelatedDemo.upgradeResourcePackage(upgradeResourcePackageRequest, client);
        log_print("upgradeResourcePackage",upgradeResourcePackageResponse);
    }
}

运行结果

正确运行结果类似如下:
-------------------------------describeResourcePackageProduct-------------------------------
{
    "requestId": "5FF49396-ECDE-464E-90C0-10B4D7FFBBFC",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "resourcePackages": [
            {
                "productCode": "ossbag",
                "productType": "ossbag",
                "name": "对象存储OSS资源包(包月)",
                "packageTypes": [
                    {
                        "name": "回源流量包(中国大陆)",
                        "code": "FPT_ossbag_deadlineAcc_CdnOut_china_common",
                        "properties": [
                            {
                                "name": "ossbag_type",
                                "value": "cdn2oss_flow_out"
                            },
                            {
                                "name": "region",
                                "value": "china-common"
                            }
                        ],
                        "specifications": [
                            {
                                "name": "1TB",
                                "value": "1024",
                                "availableDurations": [
                                    {
                                        "name": "6 Month",
                                        "value": 6,
                                        "unit": "Month"
                                    }
                                ]
                            },
                            {
                                "name": "5TB",
                                "value": "5120",
                                "availableDurations": [
                                    {
                                        "name": "6 Month",
                                        "value": 6,
                                        "unit": "Month"
                                    }
                                ]
                            },
                            {
                                "name": "10TB",
                                "value": "10240",
                                "availableDurations": [
                                    {
                                        "name": "6 Month",
                                        "value": 6,
                                        "unit": "Month"
                                    }
                                ]
                            },
                            {
                                "name": "50TB",
                                "value": "51200",
                                "availableDurations": [
                                    {
                                        "name": "6 Month",
                                        "value": 6,
                                        "unit": "Month"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
-------------------------------getResourcePackagePrice-------------------------------
{
    "requestId": "BF0CE628-6C15-4256-8C0F-43E40996FAC2",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "currency": "CNY",
        "originalPrice": 54.0,
        "tradePrice": 54.0,
        "discountPrice": 0.0,
        "promotions": []
    }
}
-------------------------------createResourcePackage-------------------------------
{
    "requestId": "382A2A7E-C34E-4F04-9830-D20542BC29C2",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "orderId": 204596149xxxxx,
        "instanceId": "OSSBAG-cn-v641c8jxxxxx"
    }
}
-------------------------------queryResourcePackageInstances-------------------------------
{
    "requestId": "5BF19B3A-D6FD-442E-B322-B6CC06F347D3",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "hostId": "cn",
        "pageNum": "1",
        "pageSize": "20",
        "totalCount": "1",
        "instances": [
            {
                "instanceId": "OSSBAG-cn-v641c8jxxxxx",
                "region": "cn-beijing",
                "totalAmount": "500.000",
                "totalAmountUnit": "GB",
                "remainingAmount": "500.000",
                "remainingAmountUnit": "GB",
                "effectiveTime": "2019-10-08T09:31:07Z",
                "expiryTime": "2019-11-08T16:00:00Z",
                "remark": "标准存储包(华北2)",
                "packageType": "FPT_ossbag_absolute_Storage_bj",
                "status": "Available",
                "deductType": "Absolute",
                "applicableProducts": [
                    "oss_cainiao",
                    "oss",
                    "snapshot"
                ]
            }
        ]
    }
}
-------------------------------renewResourcePackage-------------------------------
{
    "requestId": "AF0CC2C5-E665-4DCA-8BA1-1744EADC268F",
    "code": "Success",
    "message": "Successful!",
    "data": {
        "InstanceId": "OSSBAG-cn-0xl0njjxxxxx",
        "OrderId": 20432230111xxxxx
    }
}
-------------------------------upgradeResourcePackage-------------------------------
{
    "requestId": "43D61680-1666-415D-B516-3D2DB19B9278",
    "code": "Success",
    "message": "Successful!",
    "data": {
        "InstanceId": "OSSBAG-cn-0xl0njjxxxxx",
        "OrderId": 20432230111xxxxx
    }
}