官方网址:又拍云官网

1.点击注册或登录

2.点击云存储
又拍云1

3.创建服务
又拍云2

4.查看又拍云图片
又拍云3
4.1分配的网址/图片名
http://md-upyun.test.upcdn.net/图片名

4.2进入文件管理查看

5.添加操作员
添加操作员

6.添加操作员
创建操作员

下载依赖
1
pip install upyun

使用又拍云下载大文件

1
2
3
4
5
6
7
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
#文件流操作(节省内存)
with open('./bg.png','rb') as f:
#上传文件
res = up.put('/bg.png',f,checksum=True)

使用又拍云进行写入文件

1
2
3
4
5
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
value = 'hello python,恩呢恩'
up.put('/yun.txt',value)

使用又拍云进行目录操作,创建文件夹

1
2
3
4
5
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
#目录操作,创建文件夹
up.mkdir('/ypyun_test/')

使用又拍云移动文件

1
2
3
4
5
6
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
#移动文件
#src移动谁,dest移动到哪
up.move('bg.png','/ypyun_test/bg.png')

使用又拍云复制文件

1
2
3
4
5
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
#复制文件
up.copy('9c1d0bd8-8f51-11ea-a567-005056c00008.jpg','/ypyun_test/9c1d0bd8-8f51-11ea-a567-005056c00008.jpg')

使用又拍云断点续传

1
2
3
4
5
6
7
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
#断点续传(第二次传的时候是从第一次剩下的开始继续上传)
with open('D:\学习资料\视频\猫和老鼠.mp4','rb') as f:
# need_resume=True断点续传
res = up.put('/ypyun_test/猫和老鼠.mp4',f,checksum=True,need_resume=True)

使用又拍云进行下载

1
2
3
4
5
6
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
#下载图片
res = up.get('/ypyun_test/9c1d0bd8-8f51-11ea-a567-005056c00008.jpg')
print(res)

使用又拍云进行删除

1
2
3
4
5
import upyun
#新建又拍云实例
up = upyun.UpYun('服务名称', username='操作员', password='生成密码')
#删除
up.delete('/ypyun_test/9c1d0bd8-8f51-11ea-a567-005056c00008.jpg')

拖拽上传代码示例

vue代码:

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
65
66
67
68
69
70
<template>	
<div>
<input type="file" @change="upload_upyun" />

<div class="upload">
拖拽上传
</div>

</div>
</template>

<script>
export default{
data(){
return{

}
},
mounted () {
let upload = document.querySelector('.upload');
upload.addEventListener('dragenter', this.onDrag, false);
upload.addEventListener('dragover', this.onDrag, false);
upload.addEventListener('drop', this.onDrop, false);
},
//自定义方法
methods:{
onDrag (e) {
e.stopPropagation();
e.preventDefault();
},
onDrop (e) {
e.stopPropagation();
e.preventDefault();
this.upload_upyun(e.dataTransfer.files);
},
//上传又拍云
upload_upyun:function(files){
//获取文件对象
// let file = e.target.files[0];
let file = files[0];

//声明参数
let param = new FormData();
param.append('file',file);

const config = {
headers: { 'Content-Type': 'multipart/form-data' }
}
this.axios({
url:'http://localhost:8000/ypy/',
method:'POST',
data:param,
config:config
}).then(resp=>{
console.log(resp)
})
}
}
}

</script>

<style>
.upload {
margin: 100px auto;
width: 300px;
height: 150px;
border: 2px dashed #f00;
}
</style>

django代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
urls:    path('ypy/',UpyunLoad.as_view()),

import upyun

# 定义文件上传类
class UpyunLoad(APIView):
def post(self, request):
#前端获取文件
img = request.FILES.get('file')
#创建文件名
file_name = str(uuid.uuid1())+'.jpg'
print(file_name)
up = upyun.UpYun('你的空间服务名称', username='操作员', password='生成密码')
#x-gmkerl-rotate 旋转角度,目前只允许设置:auto,90,180,270
headers = {'x-gmkerl-rotate': 'auto'}
for chunk in img.chunks():
res = up.put('/%s'%file_name, chunk, checksum=True, headers=headers)
# 返回结果
return Response({'filename': file_name})

评论





载入天数...载入时分秒...

Blog content follows the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License

Use WZH as theme, total visits times