How to use Qiniu Cloud storage using Python

Source: Internet
Author: User
This article mainly introduces how to use Qiniu Cloud storage through Python. Qiniu Cloud storage is a leading solution provider for server data backup in China, for more information about how to use the Python SDK of Qiniu to quickly upload, download, process, and manage files.
Install

First, you must install Python SDK first. The Python SDK of Qiniu is open-source and hosted on Github. the Project address is https://github.com/qiniu/python-sdk.

You can use pip install qiniu as described in the project description. Of course, you can directly clone the source code and use it directly. I generally like to clone the source code directly. in this way, it is very easy to make some changes to the SDK.

The latest version of Python SDK depends on the requests library, so you must install it in advance. You can also use pip install requests for installation.
Development Environment

Python has many development environments. if you like text, such as vim, emacs, and sublime text, it is a good choice. if you like IDE, PyCharm is the most popular. The latest version of PyCharm is downloaded here.
Access Key and Secret Key

We know that the permission verification mechanism of Qiniu Cloud storage is based on a pair of keys called Access Key and Secret Key. The Access Key is the public Key and the Secret Key is the private Key. This pair of keys can be obtained from the backend of Qiniu.

Test Tool

Well, after completing the above preparations, we will upload a simple file and train our hands.

# Coding = UTF-8 _ author _ = 'jemy ''' This example demonstrates a simple file upload. In this example, the sdk selects Form upload or multipart Upload based on the file size. '''Import qiniuaccessKey ="
 
  
"SecretKey ="
  
   
"# Parsing result def parseRet (retData, respInfo): if retData! = None: print ("Upload file success! ") Print (" Hash: "+ retData [" hash "]) print (" Key: "+ retData [" key "]) # Check the extension parameter for k, v in retData. items (): if k [: 2] = "x:": print (k + ":" + v) # check other parameters 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) # upload without a key. The key parameter def upload_without_key (bucket, filePath) is not specified in the http request: # Generate the upload credential auth = qiniu. auth (accessKey, secretKey) upToken = auth. upload_token (bucket, key = None) # upload the file retData, respInfo = qiniu. put_file (upToken, None, filePath) # parse the result parseRet (retData, respInfo) def main (): bucket = "if-pbl" filePath = "/Users/jemy/Documents/jemy.png" upload_without_key (bucket, filePath) if _ name _ = "_ main _": main ()
  
 

The running result is:

Upload file success!Hash: Fp0XR6tM4yZmeiKXw7eZzmeyYsq8Key: Fp0XR6tM4yZmeiKXw7eZzmeyYsq8

From the above we can see that the most basic steps for uploading files using the Python SDK of Qiniu are:

  • Generate an upload credential
  • Upload files
  • Parse response results

Summary

To sum up, it is very easy to use the SDK of Qiniu to upload files. in the next tutorial, we will gradually learn about it based on this example.

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.