Seven cattle cloud with python interactive tutorial

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

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

Jump to activate

Here Insert Picture Description

Enter the account number and password, login

Activation Successful

Here Insert Picture Description

Try new memory space pictures

Management background, object storage is the key

Here Insert Picture Description

Here Insert Picture Description

New Space

403 Error

Here Insert Picture Description

The reason may not be Verified

Here Insert Picture Description

Need: real-name authentication at once

Verified

https://portal.qiniu.com/identity/choice

Here Insert Picture Description

Personal authentication and commit identity

Here Insert Picture Description

Once submitted, Alipay verification

Here Insert Picture Description

Services Licensing

Here Insert Picture Description

Application has been submitted, treatment

Here Insert Picture Description

Create a space again

Here Insert Picture Description

Here Insert Picture Description

View space

Click on the name space

Here Insert Picture Description

python operate seven cattle cloud

  • Looking documents
  • Find the function code
  • Edit the desired function

"Find Documents
Here Insert Picture Description

"View document

Here Insert Picture Description

use

"Need to install

pip install qiniu

Find the very data

Here Insert Picture Description

Find key

Here Insert Picture Description

Here Insert Picture Description

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

Here Insert Picture Description

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)


Knowledge Review

Here Insert Picture Description

Published 847 original articles · won praise 43 · views 130 000 +

Guess you like

Origin blog.csdn.net/ifubing/article/details/104181073