当前位置:网站首页>七牛云图片上传
七牛云图片上传
2022-08-04 22:57:00 【Csdn__F】
- Step 1:创建空间
- Step 2:上传文件
- Step 3:下载文件
- Step 4:删除文件
- Step 5:删除空间
步骤1:创建空间
首先,您需要创建一个 空间(Bucket)。
1. 登录 七牛云官网。
2 .存储空间 管理界面
3. 新建存储空间
注意:
如果您是体验用户,新建存储空间时,会遇到 “新建存储空间失败,请先实名认证”
的提示信息。
4. 创建成功
5. 在 内容管理 栏目中可以上传、下载、访问、修改资源等操作。
安装七牛云
pip install qiniu
实现
# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth, put_file
#需要填写你的 Access Key 和 Secret Key
access_key = 'Access_Key'
secret_key = 'Secret_Key'
#构建鉴权对象
q = Auth(access_key, secret_key)
#要上传的空间
bucket_name = 'Bucket_Name'
# 生成上传后要保存的文件名
# 获取要上传文件的后缀名
end_name = img.filename.split(".")[-1]
# 生成一个随机数,防止用户上传的文件名带有敏感操作
name = str(random.randint(100000, 999999))
# 随机数拼接文件后缀生成一个新的图片路径
localname=name+"."+end_name
#上传后保存的文件名
key = 'static/%s' % localname
#生成上传 Token
# bucket_name:要上传的空间,key:上传的路径,过期时间:3600s
token = q.upload_token(bucket_name, key, 3600)
#要上传文件的本地路径
localfile = './static/%s' % localname
info = put_file(token, key, localfile, version='v2')
print(info)
边栏推荐
猜你喜欢
[Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)
How to make a video gif?Try this video making gif artifact
Pytest learning - fixtures
2022七夕程序员必备的表白黑科技(七夕限定款)
一点点读懂thermal(一)
3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊
直接插入排序
得不到你的心,就用“分布式锁”锁住你的人
Redis understanding
地面高度检测/平面提取与检测(Fast Plane Extraction in Organized Point Clouds Using Agglomerative Hierarchical Clu)
随机推荐
使用代理对象执行实现类目标方法异常
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
Shell expect 实战案例
MySQL的JSON 数据类型2
The Controller layer code is written like this, concise and elegant!
App测试和Web测试的区别
中国的顶级黑客在国际上是一个什么样的水平?
JVM memory configuration parameter GC log
【项目实战】仿照Room实现简单管理系统
PID Controller Improvement Notes No. 7: Improve the anti-overshoot setting of the PID controller
[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
If you can't get your heart, use "distributed lock" to lock your people
Shell编程之循环语句与函数的使用
【字符串函数内功修炼】strncpy + strncat + strncmp(二)
线性DP(下)
kernel hung_task死锁检测机制原理实现
Service Mesh落地路径
一点点读懂regulator(二)
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
Nacos配置中心之客户端长轮询