Skip to content

Commit

Permalink
Merge pull request #17 from NightRain233/master
Browse files Browse the repository at this point in the history
1. 修改readme 2. 添加example_demo
  • Loading branch information
joydchh authored Jan 21, 2021
2 parents 5619eb2 + 04553aa commit 86225cc
Show file tree
Hide file tree
Showing 20 changed files with 935 additions and 306 deletions.
781 changes: 552 additions & 229 deletions README.md

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions examples/common.py

This file was deleted.

Binary file removed examples/example.jpg
Binary file not shown.
27 changes: 27 additions & 0 deletions examples/example_bucketmanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

from ufile import bucketmanager

bucketmanager_handler = bucketmanager.BucketManager(public_key, private_key)

# 创建新的bucket
bucketname = '' #创建的空间名称,命名规范参见https://docs.ucloud.cn/api/ufile-api/create_bucket
region = 'cn-bj'#空间所在的地理区域,详细信息见https://docs.ucloud.cn/ufile/introduction/region
ret, resp = bucketmanager_handler.createbucket(bucketname, region,'public')
print(ret)

# 删除bucket
bucketname = '' #待删除的空间名称
ret, resp = bucketmanager_handler.deletebucket(bucketname)
print(ret)

# 获取bucket信息
bucketname = '' # 待查询的空间名称
ret, resp = bucketmanager_handler.describebucket(bucketname)
print(ret)

# 更改bucket属性
bucketname = '' # 待更改的私有空间名称
ret, resp = bucketmanager_handler.updatebucket(bucketname, 'public')
print(ret)
23 changes: 23 additions & 0 deletions examples/example_class_switch_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #空间名称
local_file = '' #本地文件名
put_key = '' #上传文件在空间中的名称
STANDARD = 'STANDARD' #标准文件类型
IA = 'IA' #低频文件类型

from ufile import filemanager

putufile_handler = filemanager.FileManager(public_key, private_key)
classswitch_handler = filemanager.FileManager(public_key, private_key)

# 普通上传文件至空间
header = dict()
header['X-Ufile-Storage-Class'] = STANDARD
ret, resp = putufile_handler.putfile(bucket, put_key, local_file, header=header)
assert resp.status_code == 200

# 标准文件类型转换为低频文件类型
ret, resp = classswitch_handler.class_switch_file(bucket, put_key, IA)
assert resp.status_code == 200
15 changes: 15 additions & 0 deletions examples/example_compare_file_etag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from ufile import filemanager

public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #添加空间名称
put_key = '' #添加远程文件key
local_file='' #添加本地文件路径

compare_handler = filemanager.FileManager(public_key, private_key)
result=compare_handler.compare_file_etag(bucket,put_key,local_file)
if result==True:
print('etag are the same!')
else:
print('etag are different!')
14 changes: 14 additions & 0 deletions examples/example_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public_key = '' #账户公钥
private_key = '' #账户私钥
bucket = '' #空间名称
key = '' #目的文件在空间中的名称
srcbucket = '' #源文件所在空间名称
srckey = '' #源文件名称

from ufile import filemanager

copyufile_handler = filemanager.FileManager(public_key, private_key)

# 拷贝文件
ret, resp = copyufile_handler.copy(bucket, key, srcbucket, srckey)
assert resp.status_code == 200
13 changes: 13 additions & 0 deletions examples/example_deletefile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #空间名称
delete_key = '' #文件在空间中的名称

from ufile import filemanager

deleteufile_handler = filemanager.FileManager(public_key, private_key)

# 删除空间的文件
ret, resp = deleteufile_handler.deletefile(bucket, delete_key)
assert resp.status_code == 204
36 changes: 36 additions & 0 deletions examples/example_downloadfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

public_bucket = '' #公共空间名称
private_bucket = '' #私有空间名称
public_savefile = '' #保存文件名
private_savefile = '' #保存文件名
range_savefile = '' #保存文件名
put_key = '' #文件在空间中的名称
stream_key = '' #文件在空间中的名称

from ufile import filemanager

downloadufile_handler = filemanager.FileManager(public_key, private_key)

# 从公共空间下载文件
ret, resp = downloadufile_handler.download_file(public_bucket, put_key, public_savefile, isprivate=False)
assert resp.status_code == 200

# 从私有空间下载文件
ret, resp = downloadufile_handler.download_file(private_bucket, put_key, private_savefile)
assert resp.status_code == 200

# 下载包含文件范围请求的文件
ret, resp = downloadufile_handler.download_file(public_bucket, put_key, range_savefile, isprivate=False, expires=300, content_range=(0, 15))
assert resp.status_code == 206

# 从所在region为上海二的私有空间下载文件
SH2_bucket = ''
SH2_put_key = ''
SH2_private_savefile = ''
SH2_DOWNLOAD_SUFFIX = '.cn-sh2.ufileos.com'

filemgr_sh = filemanager.FileManager(public_key, private_key, download_suffix=SH2_DOWNLOAD_SUFFIX )
ret, resp = filemgr_sh.download_file(SH2_bucket, SH2_put_key, SH2_private_savefile)
assert resp.status_code == 200
46 changes: 0 additions & 46 deletions examples/example_filemanager.py

This file was deleted.

28 changes: 28 additions & 0 deletions examples/example_getfilelist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #空间名称

from ufile import filemanager

getfilelist_hander = filemanager.FileManager(public_key, private_key)

prefix='' #文件前缀
limit=10 #文件列表数目
marker='' #返回以字母排序后,大于marker的文件列表
ret, resp = getfilelist_hander.getfilelist(bucket, prefix=prefix, limit=limit, marker=marker)
assert resp.status_code == 200
for object in ret["DataSet"]:
print(object)

# 根据返回值'NextMarker'循环遍历获得所有结果(若一次查询无法获得所有结果)
while True:
ret, resp = getfilelist_hander.getfilelist(bucket, prefix=prefix, limit=limit, marker=marker)
assert resp.status_code == 200

for object in ret["DataSet"]:#
print(object)

marker = ret['NextMarker']
if len(marker) <= 0 or len(ret['DataSet']) < limit:
break
14 changes: 14 additions & 0 deletions examples/example_headfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #空间名称
head_key = '' #文件在空间中的名称

from ufile import filemanager

headfile_handler = filemanager.FileManager(public_key, private_key)

# 查询文件基本信息
ret, resp = headfile_handler.head_file(bucket, head_key)
assert resp.status_code == 200
print(resp)
40 changes: 40 additions & 0 deletions examples/example_listobjects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #空间名称

from ufile import filemanager

listobjects_hander = filemanager.FileManager(public_key, private_key)

prefix='' #以prefix作为前缀的目录文件列表
maxkeys=100 #指定返回目录文件列表的最大数量,默认值为100,不超过1000
marker='' #返回以字母排序后,大于marker的目录文件列表
delimiter='/' #delimiter是目录分隔符,当前只支持"/"和"",当Delimiter设置为"/"且prefiex以"/"结尾时,返回prefix目录下的子文件,当delimiter设置为""时,返回以prefix作为前缀的文件

# 普通使用(一次查询即可得到所有结果)
def once_list():
ret, resp = listobjects_hander.listobjects(bucket, prefix=prefix, maxkeys=maxkeys, marker=marker, delimiter=delimiter)
assert resp.status_code == 200

for object in ret['Contents']:#子文件列表
print(object)

for object in ret['CommonPrefixes']:#子目录列表
print(object)

# 因为一次查询返回数量存在最大限制,所以若一次查询无法获得所有结果,则根据返回值'NextMarker'循环遍历获得所有结果
def loop_list():
while True:
ret, resp = listobjects_hander.listobjects(bucket, prefix=prefix, maxkeys=maxkeys, marker=marker, delimiter=delimiter)
assert resp.status_code == 200

for object in ret['Contents']:#子文件列表
print(object)

for object in ret['CommonPrefixes']:#子目录列表
print(object)

marker = ret['NextMarker']
if len(marker) <= 0 or maxkeys < len(ret['Contents']):
break
53 changes: 29 additions & 24 deletions examples/example_multipartuploadufile.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
from common import *
from ufile import config, multipartuploadufile
public_key = '' #账户公钥
private_key = '' #账户私钥

# 运行时, 请自行修改
BUCKET_BJ = 'lovecrazy-private'
BJ_UPLOAD_SUFFIX = '.cn-bj.ufileos.com'
bucket = '' #空间名称
sharding_key = '' #上传文件在空间中的名称
local_file = '' #本地文件名

BUCKET_SH2 = 'lovecrazy'
SH2_UPLOAD_SUFFIX = '.cn-sh2.ufileos.com'
from ufile import multipartuploadufile

# 待上传文件路径,最好填绝对路径
UPLOAD_FILE_PATH = './example.jpg'
multipartuploadufile_handler = multipartuploadufile.MultipartUploadUFile(public_key, private_key)

# 设置默认参数
config.set_default(uploadsuffix=BJ_UPLOAD_SUFFIX)
config.set_default(downloadsuffix=BJ_UPLOAD_SUFFIX)
config.set_default(connection_timeout=60)
config.set_default(expires=60)
config.set_default(md5=True)
# 分片上传一个全新的文件
ret, resp = multipartuploadufile_handler.uploadfile(bucket, sharding_key, local_file)
while True:
if resp.status_code == 200: # 分片上传成功
break
elif resp.status_code == -1: # 网络连接问题,续传
ret, resp = multipartuploadufile_handler.resumeuploadfile()
else: # 服务或者客户端错误
print(resp.error)
break

# 上传到北京的bucket
mup_bj = multipartuploadufile.MultipartUploadUFile(PUBLIC_KEY, PRIVATE_KEY)
_, resp = mup_bj.uploadfile(BUCKET_BJ, 'python-sdk/examples/multipartput-key', UPLOAD_FILE_PATH)
assert resp.status_code == 200

# 上传到上海的bucket
mup_sh = multipartuploadufile.MultipartUploadUFile(PUBLIC_KEY, PRIVATE_KEY, upload_suffix=SH2_UPLOAD_SUFFIX)
_, resp = mup_sh.uploadfile(BUCKET_SH2, 'python-sdk/examples/multipartput-key', UPLOAD_FILE_PATH)
assert resp.status_code == 200
# 分片上传一个全新的二进制数据流
from io import BytesIO
bio = BytesIO(u'你好'.encode('utf-8'))
ret, resp = multipartuploadufile_handler.uploadstream(bucket, sharding_key, bio)
while True:
if resp.status_code == 200: # 分片上传成功
break
elif resp.status_code == -1: # 网络连接问题,续传
ret, resp = multipartuploadufile_handler.resumeuploadstream()
else: # 服务器或者客户端错误
print(resp.error)
break
14 changes: 14 additions & 0 deletions examples/example_postfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #空间名称
local_file = '' #本地文件名
post_key = '' #上传文件在空间中的名称

from ufile import filemanager

postufile_handler = filemanager.FileManager(public_key, private_key)

# 表单上传文件至空间
ret, resp = postufile_handler.postfile(bucket, post_key, local_file)
assert resp.status_code == 200
29 changes: 29 additions & 0 deletions examples/example_putfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public_key = '' #账户公钥
private_key = '' #账户私钥

bucket = '' #空间名称
local_file = '' #本地文件名
put_key = '' #上传文件在空间中的名称

from ufile import filemanager

putufile_handler = filemanager.FileManager(public_key, private_key)

# 普通上传文件至空间
ret, resp = putufile_handler.putfile(bucket, put_key, local_file, header=None)
assert resp.status_code == 200

# 普通上传二进制数据流至空间
from io import BytesIO
bio = BytesIO(u'Do be a good man'.encode('utf-8')) #二进制数据流
stream_key = '' #上传数据流在空间中的名称
ret, resp = putufile_handler.putstream(bucket, stream_key, bio)
assert resp.status_code == 200

# 普通上传文件到所在region为上海二的空间
SH2_bucket = ''
SH2_UPLOAD_SUFFIX = '.cn-sh2.ufileos.com'

filemgr_sh = filemanager.FileManager(public_key, private_key, upload_suffix=SH2_UPLOAD_SUFFIX)
ret, resp = filemgr_sh.putfile(SH2_bucket , put_key, local_file, header=None)
assert resp.status_code == 200
Loading

0 comments on commit 86225cc

Please sign in to comment.