Seven cattle introduced cloud
- The initial project, a small amount of data, the general will use a third-party storage solutions, the cost is relatively low, but also safe and reliable
- When the data reaches a certain size, the cost of third-party storage of more than build their own service file, the file will choose to build their own service system
- Object storage service OSS
- Details
- Requirements Verified
- Create storage space
- The default domain name to generate test is valid for one month is required for long-term use to bind your own domain name
Interactive process
API package
-installationpip install qiniu
from qiniu import Auth, put_data
def upload_file(file_data):
"""
七牛云上传
:param file_data 上传的二进制数据
:return: 文件名
"""
#需要填写你的 Access Key 和 Secret Key
access_key = 'kJ8wVO7lmFGsdvtI5M7eQDEJ1eT3Vrygb4SmR00E'
secret_key = 'rGwHyAvnlLK7rU4htRpNYzpuz0OHJKzX2O1LWTNl'
#构建鉴权对象
q = Auth(access_key, secret_key)
# 要上传的空间
bucket_name = 'info28'
# 上传后保存的文件名 如果为None 自动生成文件名(hash值)
key = None
#生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
ret, info = put_data(token, key, file_data)
# 判断请求结果
if info.status_code == 200:
return ret.get('key')
else:
raise Exception(info.error)
if __name__ == '__main__':
with open('123.jpg', 'rb') as f:
img_bytes = f.read()
file_name = upload_file(img_bytes)
print(file_name)
- Determine whether the file type for the picture
import imghdr
if __name__ == '__main__':
with open('123.jpg', 'rb') as f:
# 判断文件类型的本质是 对比文件的前几个字节
content = f.read()
# print(content)
# # 方式1 判断图片类型
# img_type = imghdr.what(f)
# if img_type:
# print('是图片类型: %s' % img_type)
# else:
# print('不是图片类型')
# 方式2 判断图片类型
img_type = imghdr.what(None, content)
if img_type:
print('是图片类型: %s' % img_type)
else:
print('不是图片类型')
Interface ''
# 上传头像
/v1_0/user/photo
# 请求方式
PATCH form-data
# 请求参数
photo 上传的头像文件
# 响应数据 json
{
"photo_url": "www.xx.com/123.jpg"
}
Expires tune longer
- There is a large deviation of the virtual machine time and real time