什么是阿里云盘(阿里云对象存储服务(Object Storage Service,简称OSS))?
阿里云对象存储服务(Object Storage Service,简称OSS),是阿里云对外提供的海量、安全、低成本、高可靠的云存储服务。您可以通过本文档提供的简单的REST接口,在任何时间、任何地点、任何互联网设备上进行上传和下载数据。基于OSS,您可以搭建出各种多媒体分享网站、网盘、个人和企业数据备份等基于大规模数据的服务。 阿里云对象存储服务(Object Storage Service,简称OSS),是阿里云对外提供的海量、安全、低成本、高可靠的云存储服务。您可以通过本文档提供的简单的REST接口,在任何时间、任何地点、任何互联网设备上进行上传和下载数据。基于OSS,您可以搭建出各种多媒体分享网站、网盘、个人和企业数据备份等基于大规模数据的服务。
在阿里云官网,购买相关的服务,即可使用。 在阿里云官网,购买相关的服务,即可使用。
如下,先贴出python操作oos代码:
```python
//定义oss类
import requests
import oos2
class UPLOAD_FILE():
def __init__(self,subfilename,key="your_key",password="your_password"):
auth = oss2.Auth(key,password) //初始化
self.bucket = oss2.Bucket(auth, "http://oss-cn-beijing.aliyuncs.com", "your_name")
self.subfilename = subfilename# oss 路径# oss 路径
print(self.subfilename)
def upload_file(self,path_list=None,path=None,type='content',content_file=None,content_name=None):
if path_list:
list_file = os.listdir(path_list)
for file in list_file:
local_file = path_list + "\\" + "{}".format(file)
osspath = self.subfilename + '/' + file
print(osspath)
self.up_file(osspath, local_file)
elif path:
osspath = self.subfilename + '/' +path.split('/')[-1]
local_file = path
self.up_file(osspath,local_file)
return osspath
elif type=='content':
osspath = self.subfilename + '/' + content_name
exist = self.bucket.object_exists(osspath)
if exist:
print("oss have files with the same name, ignore oss upload")
return osspath
else:
self.bucket.put_object(osspath, content_file)
print(" {} 上传成功".format(osspath))
return osspath
else:
print("未指定路径")
def up_file(self,osspath,local_file):
# 先检测oss上是否有该文件
exist = self.bucket.object_exists(osspath)
if exist:
print("oss have files with the same name, ignore oss upload")
else:
# 上传文件
with open(local_file, "rb") as fileobj:
result1 = self.bucket.put_object(osspath, fileobj)
print("{} 上传成功".format(osspath))
if int(result1.status) != 200:
print("oss upload faild %s" % osspath)
//下载数据,操作oss类
def oss2_data(oss_class,datasurl,name):
if datasurl:
try:
print(datasurl)
res = requests.get(datasurl, timeout=5,verify=False)
content_name = name
content_file = res.content
osspath = oss_class.upload_file(type='content', content_name=content_name, content_file=content_file)
return osspath
except Exception as e:
print(e)
return None
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
实例:
#初始化oss:
upload_class = UPLOAD_FILE(subfilename="mypic/exam")#初始化oss,创建mypic/exam目录
oss2_data(upload_class,'www.exam.jpg','exam.jpg')#下载www.eaam.jpg ,上次oss
- 1
- 2
- 3
- 4