title: 对象存储服务oss初探subtitle: 对象存储服务oss初探
date: 2021-07-14
author: NSX
catalog: true
tags:
- oss
oss 简介
对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。
简单使用示例
import oss2
class oss(object):
"""对象存储类,将模型传至阿里云端"""
def __init__(self, access_key_id, access_key_secret, endpoint, bucket_name):
self.auth = oss2.Auth(access_key_id, access_key_secret)
self.bucket = oss2.Bucket(self.auth, endpoint, bucket_name) # 连接OSS
def put_file(self, file_path, oss_path):
with open("{}".format(file_path), "rb") as f:
put_result = self.bucket.put_object(oss_path, f)
if put_result.status == 200:
# 若此时的status状态为200,则说明上传成功;
print("put success")
def get_file(self, file_path, oss_path):
# param1:oss上bucket中的文件名
# param2:保存在当地的文件路径+文件名
get_result = self.bucket.get_object_to_file(oss_path, file_path)
if get_result.status == 200:
print("get success")
else:
print("get failed")
oss_server = oss(
access_key_id="AccessKey"),
access_key_secret="AccessKeySecret"),
endpoint="EndPoint",
bucket_name="Bucket",
)
def download_longfor_bert(pretrain_file, oss_get_path):
"""获取OSS指定目录下的文件
"""
for obj in oss2.ObjectIterator(oss_server.bucket, prefix = oss_get_path, delimiter = '/'):
# 通过is_prefix方法判断obj是否为文件夹。
if obj.is_prefix(): # 判断obj为文件夹。
print('directory: ' + obj.key)
else: # 判断obj为文件。
print('file: ' + obj.key)
file_name = str(obj.key).split('/')[-1]
if file_name:
oss_server.get_file(pretrain_file+file_name, obj.key)
参考
https://help.aliyun.com/product/31815.html?spm=a2c4g.11174283.6.540.47ce4c07QFiKcz