alibabacloud-oss-python-sdk-v2 是OSS在Python编译语言下的第二版SDK, 处于开发预览版状态
- 此Python SDK基于阿里云对象存储服务官方API构建。
- 阿里云对象存储(Object Storage Service,简称OSS),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。
- OSS适合存放任意文件类型,适合各种网站、开发企业及开发者使用。
- 使用此SDK,用户可以方便地在任何应用、任何时间、任何地点上传,下载和管理数据。
- Python 3.8 及以上。
$ pip install alibabacloud-oss-v2
$ sudo python setup.py install
import alibabacloud_oss_v2 as oss
def main():
region = "cn-hangzhou"
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = region
client = oss.Client(cfg)
# Create the Paginator for the ListBuckets operation
paginator = client.list_buckets_paginator()
# Iterate through the bucket pages
for page in paginator.iter_page(oss.ListBucketsRequest(
)
):
for o in page.buckets:
print(f'Bucket: {o.name}, {o.location}, {o.creation_date} {o.resource_group_id}')
if __name__ == "__main__":
main()
import alibabacloud_oss_v2 as oss
def main():
region = "cn-hangzhou"
bucket_name = "your bucket name"
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = region
client = oss.Client(cfg)
# Create the Paginator for the ListObjectsV2 operation
paginator = client.list_objects_v2_paginator()
# Iterate through the object pages
for page in paginator.iter_page(oss.ListObjectsV2Request(
bucket=bucket_name
)
):
for o in page.contents:
print(f'Object: {o.key}, {o.size}, {o.last_modified}')
if __name__ == "__main__":
main()
import alibabacloud_oss_v2 as oss
def main():
region = "cn-hangzhou"
bucket_name = "your bucket name"
object_name = "your object name"
local_file = "your local file path"
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = region
client = oss.Client(cfg)
with open(local_file, 'rb') as f:
result = client.put_object(oss.PutObjectRequest(
bucket=bucket_name,
key=object_name,
body=f,
))
print(f'put object sucessfully, ETag {result.etag}')
if __name__ == "__main__":
main()
请参看sample
目录
- 进入示例程序目录
sample
。- 通过环境变量,配置访问凭证,
export OSS_ACCESS_KEY_ID="your access key id"
,export OSS_ACCESS_KEY_SECRET="your access key secrect"
。- 以 list_buckets.go 为例,执行
python list_buckets.python --region cn-hangzhou
。
开发者指南 - 参阅该指南,来帮助您安装、配置和使用该开发套件。
- Apache-2.0, 请参阅 许可文件