描述
OSS(Object Storage Service服務)模組提供與雲端儲存體服務互動的功能。
方法 | 說明 | 環境 | ||||
ComputerUseLinux | ComputerUseWindows | BrowserUse | MobileUse | CodeSpace | ||
| 初始化OSS環境變數,設定存取金鑰、安全性權杖和地區配置。 | 支援 | 支援 | 不支援 | 支援 | 支援 |
| 上傳本地檔案或目錄到指定OSS儲存桶(需要先初始化環境)。 | 支援 | 支援 | 不支援 | 支援 | 支援 |
| 從OSS儲存桶下載對象到本地路徑(需要先初始化環境)。 | 支援 | 支援 | 不支援 | 支援 | 支援 |
| 通過HTTP PUT方式匿名上傳檔案到指定URL。 | 支援 | 支援 | 不支援 | 支援 | 支援 |
| 從指定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();