OSS

描述

OSS(对象存储服务)模块提供与云存储服务交互的功能。

方法

说明

环境

ComputerUseLinux

ComputerUseWindows

BrowserUse

MobileUse

CodeSpace

oss_env_init

初始化OSS环境变量,设置访问密钥、安全令牌和区域配置。

支持

支持

不支持

支持

支持

oss_upload

上传本地文件或目录到指定OSS存储桶(需要先初始化环境)。

支持

支持

不支持

支持

支持

oss_download

OSS存储桶下载对象到本地路径(需要先初始化环境)。

支持

支持

不支持

支持

支持

oss_upload_annon

通过HTTP PUT方式匿名上传文件到指定URL。

支持

支持

不支持

支持

支持

oss_download_annon

从指定URL匿名下载文件到本地。

支持

支持

不支持

支持

支持

Oss Class

OSS类提供与云存储服务交互的方法。

EnvInit - 初始化 OSS 环境

Golang

// 初始化并创建 OSS 环境变量
func (o *Oss) EnvInit(accessKeyId, accessKeySecret, securityToken, endpoint, region string) (*EnvInitResult, error)

参数:

  • accessKeyId:OSS认证的访问密钥ID。

  • accessKeySecret:OSS认证的访问密钥。

  • securityToken:OSS认证的安全令牌。

  • endpoint:OSS服务端点。若未指定,则使用默认值。

  • region:OSS区域。若未指定,则使用默认值。

返回值:

  • *EnvInitResult:包含初始化结果和请求 ID 的对象。

  • error:若操作失败,返回错误信息。

EnvInitResult 结构体:

type EnvInitResult struct {
    RequestID string // 调试用的唯一请求标识符
    Result    string // 环境初始化操作的结果
}

示例:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)

func main() {
	// 初始化 SDK
	client, err := agentbay.NewAgentBay("your_api_key", nil)
	if err != nil {
		fmt.Printf("Error initializing AgentBay client: %v\n", err)
		os.Exit(1)
	}

	// 初始化 OSS 环境
	result, err := client.Oss.EnvInit(
		"your_access_key_id",
		"your_access_key_secret",
		"your_security_token",
		"oss-cn-hangzhou.aliyuncs.com",
		"cn-hangzhou",
	)
	if err != nil {
		fmt.Printf("Error initializing OSS environment: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("OSS environment initialized successfully, request ID: %s\n", result.RequestID)
}

Python

def env_init(self, access_key_id: str, access_key_secret: str, securityToken: Optional[str] = None,
             endpoint: Optional[str] = None, region: Optional[str] = None) -> OSSClientResult

参数:

  • access_key_id:OSS认证的访问密钥ID。

  • access_key_secret:OSS认证的访问密钥。

  • securityToken:OSS认证的安全令牌(可选)。

  • endpoint:OSS服务端点。若未指定,则使用默认值。

  • region:OSS区域。若未指定,则使用默认值。

返回值:

  • OSSClientResult:包含客户端配置、请求ID、成功状态和错误信息的结果对象。

OSSClientResult 结构体:

class OSSClientResult(ApiResponse):
    def __init__(self, request_id: str = "", success: bool = False,
                 client_config: Optional[Dict[str, Any]] = None, error_message: str = "")

示例:

from agentbay import AgentBay

# 初始化 SDK
agent_bay = AgentBay(api_key="your_api_key")

# 初始化 OSS 环境
result = agent_bay.oss.env_init(
    access_key_id="your_access_key_id",
    access_key_secret="your_access_key_secret",
    securityToken="your_security_token",
    endpoint="oss-cn-hangzhou.aliyuncs.com",
    region="cn-hangzhou"
)

if result.success:
    print(f"OSS environment initialized successfully, request ID: {result.request_id}")
else:
    print(f"Failed to initialize OSS environment: {result.error_message}")

TypeScript

async envInit(
  accessKeyId: string,
  accessKeySecret: string,
  securityToken: string,
  endpoint?: string,
  region?: string
): Promise<string>

参数:

  • accessKeyId:OSS认证的访问密钥ID。

  • accessKeySecret:OSS认证的访问密钥密钥。

  • securityToken:OSS认证的安全令牌。

  • endpoint:OSS服务端点。若未指定,则使用默认值。

  • region:OSS区域。若未指定,则使用默认值。

返回值:

  • Promise<string>:环境初始化操作的结果。

异常:

  • APIError:若环境初始化失败,抛出错误。

示例:

import { AgentBay } from 'wuying-agentbay-sdk';

// 初始化 SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });

// 初始化 OSS 环境
async function initializeOSS() {
  try {
    const result = await agentBay.oss.envInit(
      'your_access_key_id',
      'your_access_key_secret',
      'your_security_token',
      'oss-cn-hangzhou.aliyuncs.com',
      'cn-hangzhou'
    );
    console.log('OSS environment initialized successfully:', result);
  } catch (error) {
    console.error('Error initializing OSS environment:', error);
  }
}

initializeOSS();

createClient - 创建 OSS 客户端

说明

调用此API前,必须先通过env_init初始化OSS环境。

Golang

// 上传本地文件或目录到 OSS
func (o *Oss) Upload(bucket, object, path string) (*UploadResult, error)

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件或目录路径。

返回值:

  • *UploadResult:包含上传URL和请求ID的对象。

  • error:若操作失败,返回错误信息。

UploadResult 结构体:

type UploadResult struct {
    RequestID string // 调试用的唯一请求标识符
    URL       string // 已上传文件的 URL
}

示例:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)

func main() {
	// 初始化 SDK
	client, err := agentbay.NewAgentBay("your_api_key", nil)
	if err != nil {
		fmt.Printf("Error initializing AgentBay client: %v\n", err)
		os.Exit(1)
	}

	// 步骤 1: 初始化 OSS 环境
	_, err = client.Oss.EnvInit(
		"your_access_key_id",
		"your_access_key_secret",
		"your_security_token",
		"oss-cn-hangzhou.aliyuncs.com",
		"cn-hangzhou",
	)
	if err != nil {
		fmt.Printf("Error initializing OSS environment: %v\n", err)
		os.Exit(1)
	}

	// 步骤 2: 上传文件到 OSS
	result, err := client.Oss.Upload("my-bucket", "my-object", "/path/to/local/file")
	if err != nil {
		fmt.Printf("Error uploading file: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("File uploaded successfully, URL: %s, request ID: %s\n", result.URL, result.RequestID)
}

Python

def upload(self, bucket: str, object: str, path: str) -> OSSUploadResult

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件或目录路径。

返回值:

  • OSSUploadResult:包含上传结果、请求ID、成功状态和错误信息的结果对象。

OSSUploadResult 结构体:

class OSSUploadResult(ApiResponse):
    def __init__(self, request_id: str = "", success: bool = False,
                 content: str = "", error_message: str = "")

示例:

from wuying_agentbay_sdk import AgentBay

# 初始化 SDK
agent_bay = AgentBay(api_key="your_api_key")

# 第一步:初始化 OSS 环境
agent_bay.oss.env_init(
    access_key_id="your_access_key_id",
    access_key_secret="your_access_key_secret",
    security_token="your_security_token",
    endpoint="oss-cn-hangzhou.aliyuncs.com",
    region="cn-hangzhou"
)

# 第二步:上传文件
result = agent_bay.oss.upload("my-bucket", "my-object", "/path/to/local/file")
print("文件上传成功:", result)

TypeScript

async createClient(
  accessKeyId: string,
  accessKeySecret: string,
  endpoint?: string,
  region?: string
): Promise<string>

参数:

  • accessKeyId:OSS认证的访问密钥ID。

  • accessKeySecret:OSS认证的访问密钥密钥。

  • endpoint:OSS服务端点。若未指定,则使用默认值。

  • region:OSS区域。若未指定,则使用默认值。

返回值:

  • Promise<string>:客户端创建操作的结果。

异常:

  • APIError:若客户端创建失败,抛出错误。

示例:

import { AgentBay } from 'wuying-agentbay-sdk';

// 初始化 SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });

// 创建 OSS 客户端
async function createOSSClient() {
  try {
    const result = await agentBay.oss.createClient(
      'your_access_key_id',
      'your_access_key_secret',
      'oss-cn-hangzhou.aliyuncs.com',
      'cn-hangzhou'
    );
    console.log('OSS client created successfully:', result);
  } catch (error) {
    console.error('Error creating OSS client:', error);
  }
}

createOSSClient();

Upload  - 上传文件到 OSS

说明

调用此 API 前,必须先通过 env_init 初始化 OSS 环境。

Golang

// 上传本地文件或目录到 OSS
func (o *Oss) Upload(bucket, object, path string) (*UploadResult, error)

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件或目录路径。

返回值:

  • *UploadResult:包含上传URL和请求ID的对象。

  • error:若操作失败,返回错误信息。

UploadResult 结构体:

type UploadResult struct {
    RequestID string // 调试用的唯一请求标识符
    URL       string // 已上传文件的 URL
}

示例:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)

func main() {
	// 初始化 SDK
	client, err := agentbay.NewAgentBay("your_api_key", nil)
	if err != nil {
		fmt.Printf("Error initializing AgentBay client: %v\n", err)
		os.Exit(1)
	}

	// 步骤 1: 初始化 OSS 环境
	_, err = client.Oss.EnvInit(
		"your_access_key_id",
		"your_access_key_secret",
		"your_security_token",
		"oss-cn-hangzhou.aliyuncs.com",
		"cn-hangzhou",
	)
	if err != nil {
		fmt.Printf("Error initializing OSS environment: %v\n", err)
		os.Exit(1)
	}

	// 步骤 2: 上传文件到 OSS
	result, err := client.Oss.Upload("my-bucket", "my-object", "/path/to/local/file")
	if err != nil {
		fmt.Printf("Error uploading file: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("File uploaded successfully, URL: %s, request ID: %s\n", result.URL, result.RequestID)
}

Python

def upload(self, bucket: str, object: str, path: str) -> OSSUploadResult

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件或目录路径。

返回值:

  • OSSUploadResult:包含上传结果、请求ID、成功状态和错误信息的结果对象。

OSSUploadResult 结构体:

class OSSUploadResult(ApiResponse):
    def __init__(self, request_id: str = "", success: bool = False,
                 content: str = "", error_message: str = "")

示例:

from wuying_agentbay_sdk import AgentBay

# 初始化 SDK
agent_bay = AgentBay(api_key="your_api_key")

# 第一步:初始化 OSS 环境
agent_bay.oss.env_init(
    access_key_id="your_access_key_id",
    access_key_secret="your_access_key_secret",
    security_token="your_security_token",
    endpoint="oss-cn-hangzhou.aliyuncs.com",
    region="cn-hangzhou"
)

# 第二步:上传文件
result = agent_bay.oss.upload("my-bucket", "my-object", "/path/to/local/file")
print("文件上传成功:", result)

TypeScript

async upload(bucket: string, object: string, path: string): Promise<string>

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件或目录路径。

返回值:

  • Promise<string>:上传操作的结果。

异常:

  • APIError:若上传失败,抛出错误。

示例:

import { AgentBay } from 'wuying-agentbay-sdk';

// 初始化 SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });

async function uploadFile() {
  try {
    // 步骤 1: 初始化 OSS 环境
    await agentBay.oss.envInit(
      'your_access_key_id',
      'your_access_key_secret',
      'your_security_token',
      'oss-cn-hangzhou.aliyuncs.com',
      'cn-hangzhou'
    );
    // 步骤 2: 上传文件到 OSS
    const result = await agentBay.oss.upload('my-bucket', 'my-object', '/path/to/local/file');
    console.log('File uploaded successfully:', result);
  } catch (error) {
    console.error('Error uploading file:', error);
  }
}

uploadFile();

UploadAnonymous - 匿名上传文件

说明

调用此API前,必须先通过env_init初始化OSS环境。

Golang

// 将本地文件或目录匿名上传到指定 URL
func (o *Oss) UploadAnonymous(url, path string) (*UploadResult, error)

参数:

  • url:HTTP/HTTPS URL。

  • path:本地文件或目录路径。

返回值:

  • *UploadResult:包含上传URL和请求ID的对象。

  • error:若操作失败,返回错误信息。

示例:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)

func main() {
	// 初始化 SDK
	client, err := agentbay.NewAgentBay("your_api_key", nil)
	if err != nil {
		fmt.Printf("Error initializing AgentBay client: %v\n", err)
		os.Exit(1)
	}

	// 步骤 1: 初始化 OSS 环境
	_, err = client.Oss.EnvInit(
		"your_access_key_id",
		"your_access_key_secret",
		"your_security_token",
		"oss-cn-hangzhou.aliyuncs.com",
		"cn-hangzhou",
	)
	if err != nil {
		fmt.Printf("Error initializing OSS environment: %v\n", err)
		os.Exit(1)
	}

	// 步骤 2: 匿名上传文件
	result, err := client.Oss.UploadAnonymous("https://example.com/upload", "/path/to/local/file")
	if err != nil {
		fmt.Printf("Error uploading file anonymously: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("File uploaded anonymously successfully, URL: %s, request ID: %s\n", result.URL, result.RequestID)
}

Python

def upload_anonymous(self, url: str, path: str) -> OSSUploadResult

参数:

  • url:HTTP/HTTPS URL。

  • path:本地文件或目录路径。

返回值:

  • OSSUploadResult:包含上传结果、请求ID、成功状态和错误信息的结果对象。

示例:

from agentbay import AgentBay

# 初始化 SDK
agent_bay = AgentBay(api_key="your_api_key")

# 匿名上传文件
result = agent_bay.oss.upload_anonymous("https://example.com/upload", "/path/to/local/file")

if result.success:
    print(f"File uploaded anonymously successfully, content: {result.content}")
    print(f"Request ID: {result.request_id}")
else:
    print(f"Failed to upload file anonymously: {result.error_message}")

TypeScript

async uploadAnonymous(url: string, path: string): Promise<string>

参数:

  • url:HTTP/HTTPS URL。

  • path:本地文件或目录路径。

返回值:

  • Promise<string>:上传操作的结果。

异常:

  • APIError:若上传失败,抛出错误。

示例:

import { AgentBay } from 'wuying-agentbay-sdk';

// 初始化 SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });

async function uploadFileAnonymously() {
  try {
    // 步骤 1: 初始化 OSS 环境
    await agentBay.oss.envInit(
      'your_access_key_id',
      'your_access_key_secret',
      'your_security_token',
      'oss-cn-hangzhou.aliyuncs.com',
      'cn-hangzhou'
    );
    // 步骤 2: 匿名上传文件
    const result = await agentBay.oss.uploadAnonymous('https://example.com/upload', '/path/to/local/file');
    console.log('File uploaded anonymously successfully:', result);
  } catch (error) {
    console.error('Error uploading file anonymously:', error);
  }
}

uploadFileAnonymously();

Download - 从 OSS 下载文件

说明

调用此API前,必须先通过env_init初始化OSS环境。

Golang

// 从 OSS 下载对象到本地文件
func (o *Oss) Download(bucket, object, path string) (*DownloadResult, error)

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件保存路径。

返回值:

  • *DownloadResult:包含本地文件路径和请求ID的对象。

  • error:若操作失败,返回错误信息。

DownloadResult 结构体:

type DownloadResult struct {
    RequestID string // 调试用的唯一请求标识符
    LocalPath string // 文件下载到的本地路径
}

示例:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)

func main() {
	// 初始化 SDK
	client, err := agentbay.NewAgentBay("your_api_key", nil)
	if err != nil {
		fmt.Printf("Error initializing AgentBay client: %v\n", err)
		os.Exit(1)
	}

	// 步骤 1: 初始化 OSS 环境
	_, err = client.Oss.EnvInit(
		"your_access_key_id",
		"your_access_key_secret",
		"your_security_token",
		"oss-cn-hangzhou.aliyuncs.com",
		"cn-hangzhou",
	)
	if err != nil {
		fmt.Printf("Error initializing OSS environment: %v\n", err)
		os.Exit(1)
	}

	// 步骤 2: 从 OSS 下载文件
	result, err := client.Oss.Download("my-bucket", "my-object", "/path/to/local/file")
	if err != nil {
		fmt.Printf("Error downloading file: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("File downloaded successfully to: %s, request ID: %s\n", result.LocalPath, result.RequestID)
}

Python

def download(self, bucket: str, object: str, path: str) -> OSSDownloadResult

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件保存路径。

返回值:

  • OSSDownloadResult:包含下载结果、请求ID、成功状态和错误信息的结果对象。

OSSDownloadResult 结构体:

class OSSDownloadResult(ApiResponse):
    def __init__(self, request_id: str = "", success: bool = False,
                 content: str = "", error_message: str = "")

示例:

from wuying_agentbay_sdk import AgentBay

# 初始化 SDK
agent_bay = AgentBay(api_key="your_api_key")

# 第一步:初始化 OSS 环境
agent_bay.oss.env_init(
    access_key_id="your_access_key_id",
    access_key_secret="your_access_key_secret",
    security_token="your_security_token",
    endpoint="oss-cn-hangzhou.aliyuncs.com",
    region="cn-hangzhou"
)

# 第二步:下载文件
result = agent_bay.oss.download("my-bucket", "my-object", "/path/to/local/file")
print("文件下载成功:", result)

TypeScript

async download(bucket: string, object: string, path: string): Promise<string>

参数:

  • bucket:OSS存储桶名称。

  • object:OSS中的对象键。

  • path:本地文件保存路径。

返回值:

  • Promise<string>:下载操作的结果。

异常:

  • APIError:若下载失败,抛出错误。

示例:

import { AgentBay } from 'wuying-agentbay-sdk';

// 初始化 SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });

async function downloadFile() {
  try {
    // 步骤 1: 初始化 OSS 环境
    await agentBay.oss.envInit(
      'your_access_key_id',
      'your_access_key_secret',
      'your_security_token',
      'oss-cn-hangzhou.aliyuncs.com',
      'cn-hangzhou'
    );
    // 步骤 2: 从 OSS 下载文件
    const result = await agentBay.oss.download('my-bucket', 'my-object', '/path/to/local/file');
    console.log('File downloaded successfully:', result);
  } catch (error) {
    console.error('Error downloading file:', error);
  }
}

downloadFile();

DownloadAnonymous - 匿名下载文件

说明

调用此 API 前,必须先通过 env_init 初始化 OSS 环境。

Golang

// 从指定 URL 匿名下载文件到本地
func (o *Oss) DownloadAnonymous(url, path string) (*DownloadResult, error)

参数:

  • url:HTTP/HTTPS URL。

  • path:本地文件完整保存路径。

返回值:

  • *DownloadResult:包含本地文件路径和请求ID的对象。

  • error:若操作失败,返回错误信息。

示例:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)

func main() {
	// 初始化 SDK
	client, err := agentbay.NewAgentBay("your_api_key", nil)
	if err != nil {
		fmt.Printf("Error initializing AgentBay client: %v\n", err)
		os.Exit(1)
	}

	// 步骤 1: 初始化 OSS 环境
	_, err = client.Oss.EnvInit(
		"your_access_key_id",
		"your_access_key_secret",
		"your_security_token",
		"oss-cn-hangzhou.aliyuncs.com",
		"cn-hangzhou",
	)
	if err != nil {
		fmt.Printf("Error initializing OSS environment: %v\n", err)
		os.Exit(1)
	}

	// 步骤 2: 匿名下载文件
	result, err := client.Oss.DownloadAnonymous("https://example.com/file.txt", "/path/to/local/file.txt")
	if err != nil {
		fmt.Printf("Error downloading file anonymously: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("File downloaded anonymously successfully to: %s, request ID: %s\n", result.LocalPath, result.RequestID)
}

Python

def download_anonymous(self, url: str, path: str) -> OSSDownloadResult

参数:

  • url:HTTP/HTTPS URL。

  • path:本地文件或目录保存路径。

返回值:

  • OSSDownloadResult:包含下载内容、请求ID、成功状态和错误信息的结果对象。

示例:

from agentbay import AgentBay

# 初始化 SDK
agent_bay = AgentBay(api_key="your_api_key")

# 匿名下载文件
result = agent_bay.oss.download_anonymous("https://example.com/file.txt", "/path/to/local/file.txt")

if result.success:
    print(f"File downloaded anonymously successfully, content: {result.content}")
    print(f"Request ID: {result.request_id}")
else:
    print(f"Failed to download file anonymously: {result.error_message}")

TypeScript

async downloadAnonymous(url: string, path: string): Promise<string>

参数:

  • url:HTTP/HTTPS URL。

  • path:本地文件完整保存路径。

返回值:

  • Promise<string>:下载操作的结果。

异常:

  • APIError:若下载失败,抛出错误。

示例:

import { AgentBay } from 'wuying-agentbay-sdk';

// 初始化 SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });

async function downloadFileAnonymously() {
  try {
    // 步骤 1: 初始化 OSS 环境
    await agentBay.oss.envInit(
      'your_access_key_id',
      'your_access_key_secret',
      'your_security_token',
      'oss-cn-hangzhou.aliyuncs.com',
      'cn-hangzhou'
    );
    // 步骤 2: 匿名下载文件
    const result = await agentBay.oss.downloadAnonymous('https://example.com/file.txt', '/path/to/local/file.txt');
    console.log('File downloaded anonymously successfully:', result);
  } catch (error) {
    console.error('Error downloading file anonymously:', error);
  }
}

downloadFileAnonymously();