You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于cloud-init标识筛选镜像列表及SoftLayer Java SDK公共镜像筛选指引

Hey there, let's walk through how to tackle both of these tasks with clear, actionable steps:

1. 基于cloud-init标记筛选镜像列表

The approach varies slightly depending on the tool you're using, but the core logic is targeting images tagged with cloud-init. Here are common methods:

  • SoftLayer CLI Method
    If you're using the SoftLayer command-line tool, you can directly filter images by tag with the --tag flag:

    # 筛选公共镜像中带有cloud-init标记的
    slcli image list --public --tag cloud-init
    
    # 筛选私有镜像中带有cloud-init标记的,去掉--public参数即可
    slcli image list --tag cloud-init
    
  • REST API Method
    When calling the SoftLayer REST API, construct an objectFilter to target the tag. Send a GET request to https://api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getAllObjects with this filter in the request parameters:

    {
      "objectFilter": {
        "tagReferences": {
          "tag": {
            "name": {
              "operation": "cloud-init"
            }
          }
        }
      }
    }
    

    Note: Add "isPublic": {"operation": true} to the filter if you only want to see public images.

2. 通过SoftLayer Java SDK筛选带有cloud-init标记且按创建时间过滤的公共镜像

First, add the SoftLayer Java SDK to your project. For Maven, include this dependency in your pom.xml:

<dependency>
    <groupId>com.softlayer.api</groupId>
    <artifactId>softlayer-api-client</artifactId>
    <version>0.3.4</version>
</dependency>

Then use this code snippet to filter images by both tag and creation date:

import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.virtual.guest.block.device.template.group.VirtualGuestBlockDeviceTemplateGroup;
import com.softlayer.api.filter.FilterOperator;
import com.softlayer.api.filter.ObjectFilter;
import com.softlayer.api.filter.ObjectSort;

import java.util.List;

public class CloudInitImageFilter {
    public static void main(String[] args) {
        // 初始化API客户端,替换为你的SoftLayer用户名和API密钥
        ApiClient client = new RestApiClient().withCredentials("YOUR_USERNAME", "YOUR_API_KEY");
        VirtualGuestBlockDeviceTemplateGroup.Service imageService = VirtualGuestBlockDeviceTemplateGroup.service(client);

        // 构建过滤条件
        ObjectFilter filter = new ObjectFilter();
        // 筛选公共镜像
        filter.append("isPublic", FilterOperator.EQUALS, true);
        // 筛选带有cloud-init标记的镜像
        filter.append("tagReferences.tag.name", FilterOperator.EQUALS, "cloud-init");
        // 筛选创建时间在2024年1月1日之后的镜像,可根据需求调整日期
        filter.append("createDate", FilterOperator.GREATER_THAN_OR_EQUAL, "2024-01-01T00:00:00Z");

        // 按创建时间降序排序,最新镜像优先
        imageService.setObjectSort(new ObjectSort().append("createDate", ObjectSort.Order.DESC));
        imageService.setObjectFilter(filter);

        try {
            // 获取符合条件的镜像列表
            List<VirtualGuestBlockDeviceTemplateGroup> images = imageService.getAllObjects();

            // 遍历输出结果
            for (VirtualGuestBlockDeviceTemplateGroup image : images) {
                System.out.println("镜像名称: " + image.getName());
                System.out.println("创建时间: " + image.getCreateDate());
                System.out.println("镜像ID: " + image.getId());
                System.out.println("---------------------------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Key Notes:

  • Double-check the tag name: Some images might use variants like cloud_init instead of cloud-init—run a basic list request first to confirm the exact tag if you get no results.
  • Adjust the date filter: Use LESS_THAN_OR_EQUAL to target older images, or add a second date condition to create a range.
  • For private images: Remove the isPublic filter line from the code.

内容的提问来源于stack exchange,提问作者Malyadri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:24:09