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

Oracle Cloud:基于对象存储桶文件变更触发多实例执行命令

实现OCI对象存储桶触发计算实例执行命令的方案

核心思路

无需直接单独调用ComputeInstanceAgentClient,而是通过OCI的事件服务+函数服务+实例命令组合流程来实现需求:

具体步骤

  1. 配置对象存储事件规则

    • 在OCI控制台创建事件规则,触发条件设为对象存储的「对象创建」或「对象更新」事件(对应事件类型com.oraclecloud.objectstorage.object.create/com.oraclecloud.objectstorage.object.update)
    • 将事件目标设置为OCI函数服务
  2. 编写OCI函数调用实例命令

    • 在函数中使用OCI Python SDK调用ComputeInstanceAgentClient的create_instance_command接口,批量指定需要执行命令的计算实例OCID
    • 核心代码示例:
      import oci
      from oci.compute_instance_agent import ComputeInstanceAgentClient
      
      def handler(ctx, data):
          # 初始化客户端
          signer = oci.auth.signers.get_resource_principals_signer()
          client = ComputeInstanceAgentClient(config={}, signer=signer)
          
          # 定义命令内容与目标实例
          command_details = oci.compute_instance_agent.models.CreateInstanceAgentCommandDetails(
              compartment_id="你的 compartment OCID",
              target=oci.compute_instance_agent.models.InstanceAgentCommandTarget(
                  instance_ids=["实例1 OCID", "实例2 OCID"]
              ),
              display_name="存储桶触发命令",
              content=oci.compute_instance_agent.models.InstanceAgentCommandContent(
                  source=oci.compute_instance_agent.models.InstanceAgentCommandSourceViaText(
                      text="echo '文件已更新' >> /tmp/bucket_update.log"
                  ),
                  output_type="TEXT"
              )
          )
          
          # 发送命令请求
          response = client.create_instance_agent_command(command_details)
          return {"status": "success", "command_id": response.data.id}
      
  3. 配置权限策略

    • 给计算实例所属的动态组添加manage instance-agent-commands权限,确保实例能接收并执行命令
    • 给函数所属的动态组添加调用instance-agent-commands相关接口的权限
  4. 验证流程

    • 向目标存储桶上传或修改文件,触发事件规则
    • 查看函数执行日志,确认命令已发送到指定实例
    • 登录计算实例检查命令执行结果

关键注意事项

  • 计算实例必须安装并运行OCI实例代理(Oracle Cloud Agent),否则无法接收命令
  • 批量执行时注意实例的地域与compartment范围,避免跨地域调用报错
  • 可通过事件规则的过滤条件,仅触发特定存储桶或特定文件类型的事件

内容的提问来源于stack exchange,提问作者N. J

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 09:11:53