前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >swiftclient操作

swiftclient操作

作者头像
周小董
发布2019-03-25 10:26:01
6510
发布2019-03-25 10:26:01
举报
文章被收录于专栏:python前行者
代码语言:javascript
复制
# encoding=utf-8
from keystoneauth1 import session
from keystoneauth1.identity import v3
from swiftclient import client,ClientException


OS_USERNAME = 'admin'
OS_USER_DOMAIN_NAME = 'Default'
OS_PASSWORD = '123456'
OS_PROJECT_NAME = 'admin'
OS_PROJECT_DOMAIN_NAME = 'Default'
OS_AUTH_URL = 'http://192.169.113.11:35357/v3'

# Create a password auth plugin
auth = v3.Password(auth_url=OS_AUTH_URL,
                   username=OS_USERNAME,
                   password=OS_PASSWORD,
                   user_domain_name=OS_USER_DOMAIN_NAME,
                   project_name=OS_PROJECT_NAME,
                   project_domain_name=OS_PROJECT_DOMAIN_NAME)
# Create session
keystone_session = session.Session(auth=auth)
# Create swiftclient Connection
swift_conn = client.Connection(session=keystone_session)

# List the available containers (列出可用的容器)------------------------------
resp_headers, containers = swift_conn.get_account()
print("Response headers: %s" % resp_headers)
for container in containers:
    print(container)

# # List the available objects(列出可用对象)------------------------------
container = 'container-lsy'
resp_headers, objects = swift_conn.get_container(container)
print("Response headers: %s" % resp_headers)
for object in objects:
    print(object)

# # Create a new container(创建一个新容器)------------------------------
container = 'container-lsy2'
swift_conn.put_container(container)
resp_headers, containers = swift_conn.get_account()
if container in containers:
    print("The container was created")
for container in containers:
    print(container)

# # Create a new object with the contents of a local text file(创建具有本地文本文件内容的新对象)------------------------------
container = 'container-lsy'
with open('data/123.pdf', 'rb') as local:
    swift_conn.put_object(
        container,
        '123.pdf',
        contents=local,
        content_type='text/plain'
    )

## list object(列表对象)----------------------------------------
container = 'container-lsy'
resp_headers,body = swift_conn.get_object(container, '123.pdf')
print(resp_headers)

## Confirm presence of the object(确认物体的存在)------------------------------
obj = '123.pdf'
container = 'container-lsy'
try:
    resp_headers = swift_conn.head_object(container, obj)
    print('The object was successfully created')
except ClientException as e:
    if e.http_status == '404':
        print('The object was not found')
    else:
        print('An error occurred checking for the existence of the object')

# # # Download the created object(下载创建的对象)------------------------------
resp_headers, obj_contents = swift_conn.get_object(container, obj)
with open('local_copy.pdf', 'wb') as local:
    local.write(obj_contents)

# # Delete the created object(删除创建的对象) -------------------------------------
obj = '123.pdf'
container = 'container-lsy'
try:
    swift_conn.delete_object(container, obj)
    print("Successfully deleted the object")
except ClientException as e:
    print("Failed to delete the object with error: %s" % e)

文档:https://ecloud.10086.cn/op-help-center/develop/Swift Python SDK.pdf

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年01月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档