全部產品
Search
文件中心

Object Storage Service:擷取儲存空間資訊(Python SDK V1)

更新時間:Aug 08, 2025

儲存空間(Bucket)是儲存物件(Object)的容器。本文介紹如何擷取儲存空間的資訊。

注意事項

範例程式碼

以下代碼用於擷取儲存空間的資訊(Info),包括儲存空間所在地區、建立日期等。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請先配置環境變數。
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# 填寫Endpoint對應的Region資訊,例如cn-hangzhou。注意,v4簽名下,必須填寫該參數
region = "cn-hangzhou"

# yourBucketName填寫儲存空間名稱。
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# 擷取Bucket相關資訊。
bucket_info = bucket.get_bucket_info()
# 擷取Bucket名稱。
print("name: " + bucket_info.name)
# 擷取Bucket儲存類型。
print("storage class: " + bucket_info.storage_class)
# 擷取Bucket建立時間。
print("creation date: " + bucket_info.creation_date)
# 擷取Bucket內網Endpoint。
print("intranet_endpoint: " + bucket_info.intranet_endpoint)
# 擷取Bucket外網Endpoint。
print("extranet_endpoint " + bucket_info.extranet_endpoint)
# 擷取擁有者資訊。
print("owner: " + bucket_info.owner.id)
# 擷取Bucket讀寫權限ACL。
print("grant: " + bucket_info.acl.grant)
# 擷取容災類型。
print("data_redundancy_type:" + bucket_info.data_redundancy_type)
# 擷取Bucket的訪問跟蹤狀態。僅Python SDK 2.16.1及以上版本支援擷取訪問跟蹤狀態。
print("access_monitor:" + bucket_info.access_monitor)

相關文檔

  • 關於擷取儲存空間資訊的完整範例程式碼,請參見GitHub樣本

  • 關於擷取儲存空間資訊的API介面說明,請參見GetBucketInfo