What is seven cattle cloud
Provide cloud storage service
official website
Seven cattle cloud - a leading provider of enterprise-class cloud service
https://www.qiniu.com/
Registration and Activation
Seven cattle cloud - Registration and polite https://portal.qiniu.com/signup
Jump to activate
Enter the account number and password, login
Activation Successful
Try new memory space pictures
Management background, object storage is the key
New Space
403 Error
The reason may not be Verified
Need: real-name authentication at once
Verified
https://portal.qiniu.com/identity/choice
Personal authentication and commit identity
Once submitted, Alipay verification
Services Licensing
Application has been submitted, treatment
Create a space again
View space
Click on the name space
python operate seven cattle cloud
- Looking documents
- Find the function code
- Edit the desired function
"Find Documents
"View document
use
"Need to install
pip install qiniu
Find the very data
Find key
Code
"""
Python SDK_SDK_对象存储 - 七牛开发者中心
https://developer.qiniu.com/kodo/sdk/1242/python#3
"""
from qiniu import Auth, put_file, etag
import qiniu.config
#需要填写你的 Access Key 和 Secret Key
access_key = 'gMQ_x2DD6xcBsHf7Bwn4iRGFLwLilsmiW5DG3RsI'
secret_key = 'CAvmXjwUEZm8d8h_gStjOLKqy9ssx6mSHtlcFsdf'
#构建鉴权对象
q = Auth(access_key, secret_key)
#要上传的空间
bucket_name = 'test-two'
#上传后保存的文件名
key = 'testupimg.png'
#生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
#要上传文件的本地路径
localfile = './bd.jpg'
ret, info = put_file(token, key, localfile)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
URL splicing technology
urljoin
Function Package
"""
Python SDK_SDK_对象存储 - 七牛开发者中心
https://developer.qiniu.com/kodo/sdk/1242/python#3
"""
# 需要填写你的 Access Key 和 Secret Key
access_key = 'gMQ_x2DD6xcBsHf7Bwn4iRGFLwLilsmiW5DG3RsI'
secret_key = 'CAvmXjwUEZm8d8h_gStjOLKqy9ssx6mSHtlcFsdf'
from urllib.parse import urljoin
# 目标
# 写一个函数,功能是上传本地图片
# 返回值是上传后的网络图片的地址
# 空间名称与空间网址的对应字典
space2url ={
"test-two":"http://q57wyk04l.bkt.clouddn.com",
"x":"urlx"
}
def up2qiniu(local_img,space_name,img_name):
"""
本图图片的上传
:param local_img: 本地图片路径
:param space_name: 云服务器的空间名称
:param img_name: 上传后的网络上保存的图片名称
:return img_url: 远程图片的路径(绝对路径)
"""
from qiniu import Auth, put_file, etag
import qiniu.config
# 构建鉴权对象
q = Auth(access_key, secret_key)
# 要上传的空间
bucket_name = space_name
# 上传后保存的文件名
key = img_name
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
# 要上传文件的本地路径
localfile = local_img
ret, info = put_file(token, key, localfile)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
# img_url = 空间名称 拼接 远程图片名称
# img_url = urljoin("http://q57wyk04l.bkt.clouddn.com", img_name)
img_url = urljoin(space2url[space_name], img_name)
return img_url
res = up2qiniu("./fy.jpeg", "test-two","far_fy.jpeg")
print(res)