A detailed explanation of how Python can be used to store seven cow cloud

Source: Internet
Author: User
This tutorial is intended to show you how to use the seven kn Python sdk to quickly file upload, download, process, manage, and more.
installation

First, the SDK to use Python must be installed first. The seven-Ox Python SDK is open source, hosted on GitHub and has a project address of HTTPS://GITHUB.COM/QINIU/PYTHON-SDK.

The installation can be done using PIP install Qiniu, as stated in the project description. Of course, you can directly clone a copy of the source code down the direct use. I generally like the direct clone source code, so if you want to make some changes to the SDK is very easy.

The latest version of the Python SDK relies on the requests library, so it needs to be installed in advance. The installation method can also use PIP install requests.
Development environment

Python's development environment has a lot of choices, if you like the way text, such as vim,emacs,sublime text is a good choice, if you like the IDE, then the most popular is pycharm. The latest version of Pycharm to download here.
Access Key and secret key

We know the seven Qiniu. The permissions check mechanism is based on a pair of keys, called access key and secret key, respectively. Where access key is the public key, Secret key is the private key. This pair of keys can be obtained from the background of seven cows.

Small trial Sledgehammer

Well, doing the above preparatory work, we will go to upload a simple file, practice practiced hand.

#coding =utf-8__author__ = ' Jemy ' "This example demonstrates a simple file upload. In this example, the SDK chooses whether to upload a form or a shard based on the size of the file. ' Import Qiniuaccesskey = '
 
  
   "Secretkey =" 
   
    "#解析结果def Parseret (Retdata, Respinfo): if retdata! = None:print (" Upload file success! ") Print ("hash:" + retdata["hash"]) print ("Key:" + retdata["key"]) #检查扩展参数 for K, V in Retdata.items (): I  F K[:2] = = "x:": print (k + ":" + V) #检查其他参数 for K, V in Retdata.items (): if k[:2] = = "x:" or k = = "Hash"    or k = = "key": Continue Else:print (k + ":" + str (v)) else:print ("Upload file failed!") Print ("Error:" + respinfo.text_body) #无key上传, the key parameter is not specified in the HTTP request def upload_without_key (bucket, FilePath): #生成上传凭证 auth = Qi Niu. Auth (AccessKey, secretkey) Uptoken = Auth.upload_token (bucket, key=none) #上传文件 retdata, respinfo = Qiniu.put_file (uptok En, None, FilePath) #解析结果 Parseret (Retdata, Respinfo) def main (): bucket = "IF-PBL" FilePath = "/users/jemy/documents/j Emy.png "Upload_without_key (bucket, filePath) if __name__ = =" __main__ ": Main ()
    

  
 

The result of the operation is:

Upload file success! Hash:Fp0XR6tM4yZmeiKXw7eZzmeyYsq8Key:Fp0XR6tM4yZmeiKXw7eZzmeyYsq8

As we can see from the above, the most basic steps to uploading a file using the seven KN Python SDK are:

    • Generate Upload Voucher
    • Uploading files
    • Parse the reply result

Summary

To sum up, in fact, the use of seven Ox SDK to upload files is very simple, the next tutorial, we will be on the basis of this example to learn more about file upload knowledge.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.