阿里云OSS操作

一、阿里云OSS API常用操作

1.上传文件

1.1 小文件上传(小于5GB)

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import oss2
import os
import sys
oss_file_name=sys.argv[1]
local_file_name=sys.argv[2]
auth = oss2.Auth('LTAI5tKDLc6S2pkrcqyZHxxx', 'OJybPiQpyeS22tlKpmgaex1xxxxxx')
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'mqops-backup')

bucket.put_object_from_file(filename=local_file_name,key=oss_file_name)

1.2 大文件上传(断点续传)

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import oss2
import os
import sys
oss_file_name=sys.argv[1]
local_file_name=sys.argv[2]
auth = oss2.Auth('LTAI5tKDLc6S2pkrcqyZHxxx', 'OJybPiQpyeS2xxxxxx')
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'mqops-backup')

oss2.resumable_upload(bucket, oss_file_name, local_file_name, multipart_threshold=100 * 1024)  # 设置分片大小,单位为字节,取值范围为100 KB~5 GB。默认值为100 KB
 

2.删除文件

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import oss2
file_list = []
retain_file_num = 3   #保留的文件个数
prefix = 'TestDB_bak/' # 文件前缀(类似目录名称)prefix参数不能以正斜线(/)开头。如果prefix参数置空,则默认列举Bucket内的所有Object

auth = oss2.Auth('LTAI5tKDLc6S2pkrcqyZHxxx', 'OJybPiQpyeS2xxxxxx')
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'mqops-backup')
# 获取文件名称保存到列表,默认按照字母序排列
for obj in oss2.ObjectIterator(bucket,prefix=prefix):
    file_list.append(obj.key)
# 删除需要保留的文件 file_list=file_list[:- retain_file_num] if file_list:
# 删除文件,传入对象为列表 result = bucket.batch_delete_objects(file_list) # 打印成功删除的文件名。 print('\n'.join(result.deleted_keys))

3.列举文件

简单列举

import oss2
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# 列举fun文件夹下的文件与子文件夹名称,不列举子文件夹下的文件。如果不需要返回owner信息可以不设置fetch_owner参数。
# prefix 列举以prefix开头的文件,即文件夹下所有文件和子文件夹,包含子文件下文件及文件夹名称
# delimiter 如果为 '/' 则只列举该文件夹下的文件和子文件夹名称,子文件下的文件和文件夹不显示
# start_after 列举文件的起始位置
# fetch_owner 是否返回包含owenr的信息 for obj in oss2.ObjectIteratorV2(bucket, prefix = 'fun/', delimiter = '/', start_after='fun/', fetch_owner=True): # 通过is_prefix方法判断obj是否为文件夹。 if obj.is_prefix(): # 判断obj为文件夹。 print('directory: ' + obj.key) else: # 判断obj为文件。 print('file: ' + obj.key) print('file owner display name: ' + obj.owner.display_name) print('file owner id: ' + obj.owner.id) 参考:https://help.aliyun.com/document_detail/88458.html 

根据字段排序列举(需要OSS开启数据索引)

import oss2

from oss2.models import MetaQuery, AggregationsRequest

auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'mqops-backup')

#do_meta_query_request = MetaQuery(max_results=10, query='{"Field": "Size","Value": "1048576","Operation": "lt"}', sort='Size', order='asc')  #根据文件大小排序
do_meta_query_request = MetaQuery(query='{"Field": "Filename","Value": "TestDB_bak/","Operation": "prefix"}',sort='FileModifiedTime', order='asc') #指定路径,按时间进行排序
result = bucket.do_bucket_meta_query(do_meta_query_request)
print(type(result.files))
print(result.files[0].file_name)


参考:
字段和操作符的支持列表:https://help.aliyun.com/document_detail/419236.htm?spm=a2c4g.11186623.0.0.61ff4c940OkJTg#concept-2084036
DoMetaQuery API:https://help.aliyun.com/document_detail/419228.html
数据索引:https://help.aliyun.com/document_detail/416775.html

  

  

 

 

参考:https://help.aliyun.com/document_detail/32025.html

 

 

 

 

  

posted @ 2023-01-28 17:04  百衲本  阅读(327)  评论(0编辑  收藏  举报
cnblogs_post_body { color: black; font: 0.875em/1.5em "微软雅黑" , "PTSans" , "Arial" ,sans-serif; font-size: 15px; } cnblogs_post_body h1 { text-align:center; background: #333366; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 23px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h2 { text-align:center; background: #006699; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 20px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h3 { background: #2B6695; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 18px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } 回到顶部 博客侧边栏 回到顶部 页首代码 回到顶部 页脚代码