Seven Qiniu storage Python SDK usage Tutorial-upload strategy detailed

Source: Internet
Author: User
Tags base64 processing instruction

textSeven Qiniu storage Python SDK usage Tutorial-upload strategy detailed
    • Seven Qiniu Storage
    • Python-sdk
    • Seven Qiniu Storage Tutorials
Jemygraw released January 04, 2015
    • Recommended 1 Recommendations
    • Collection 2 Favorites,2.7k browsing

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

Objective

We learned in the two sections above that when a client uploads a file, it needs to request an upload voucher (Upload Token) from the Business Server, and this upload voucher is generated by the business Server based on the upload policy (putpolicy). AccessKey and Secretkey must also be used in this build process.

We can see from the simple above:

    1. Uploading a client to a Business Server request an upload voucher (Upload Token)
    2. The Business Server returns an upload voucher to the upload client
    3. Upload client build upload request, upload file to seven Cow cloud storage server
    4. The seven Qiniu storage server returns the result of the client file upload.

Of course, in our previous example and in the following example, the upload client and the Business Server is one, but from the business model, the 1th and 2nd steps are still separate (for example, in the web-side upload or mobile app-side uploads).

Upload Policy

We have to study the seven-cow upload strategy and have to understand all of its supported parameters (and not many). Document: Poke here.
Many people may look at the first glance, feel a lot of parameters Ah, the head is big, in fact, to deal with more things, the best way is to group, the parameters according to the function of different groups. For example, we can divide the uploading strategy into the following groups:

Basic parameters

The basic parameters include the storage space after file upload, the upload mode of the file (whether overlay upload is supported), the expiration time of uploading the voucher, the name of the business Server to upload the file, etc.

Name of parameter Description
Scope Upload the space bucket , or Yes bucket:key (to support file overlay uploads).
Deadline Upload the time stamp of the expiry of the voucher, the General SDK will be set with parameters expires, that is, the deadline = now()+expires units are .
Insertonly The default is 0, mates scope=bucket:key can support overwrite uploads, and if 1, the Force upload semantics is new, and an error is returned when the file is present file exists .
Savekey The Business Server can use this parameter to specify the name of the uploaded file stored in the Seven Cow cloud storage (only if the upload client does not specify the request parameters key ).
Enduser Can be used to mark the file upload device information, such as phone type, system type and so on.

Custom upload Replies

By default, the information returned to the upload client by the seven Qiniu store includes only the file's hash and key , as we saw earlier:

{"hash":"FoaI_ZzokcGpZA48PVJPFNJvLoNc","key":"FoaI_ZzokcGpZA48PVJPFNJvLoNc"}

So is there a way to expand this flimsy return message?

The answer is of course: Yes!

Name of parameter Description
Returnbody You can use the reply content from the definition upload, support the magic variable and the custom variable (also known as the extension variable), about the variable see document: Poke here

Upload Complete redirect

Upload client after the file upload is complete, from the Seven Cow cloud storage server to obtain the results of the upload reply. In this process, no longer involved in the business Server, but if I want to upload the file, seven cattle server can tell the business Server a bit of information? Of course it is possible, one of the methods is page redirection, of course, page redirection must know the redirect address ( returnUrl ), and redirect to the new address to carry the parameters can be set by the content returnBody .

After the file upload is completed, seven cows return to 303 the upload status code, and then the upload can be redirected to the returnUrl specified address, such as redirection to http://.../service/return_upload_service?upload_ret=eyJmbmFtZSI6InVwbG9hZF9kcmFmdC5wbmciLCJldGFnIjoiRnNOM0g4eS15NGxzcV9jZ2p0NEQ3a2E5cjVmUSIsImtleSI6IkZzTjNIOHkteTRsc3FfY2dqdDREN2thOXI1ZlEiLCJleFBhcmFtMSI6ImhlbGxvIiwiZXhQYXJhbTIiOiJxaW5pdSIsImV4UGFyYW0zIjoicWluaXUifQ== such an address, where the returnUrl setting is http://.../service/return_upload_service , and the redirect address carries the contents of the parameters upload_ret are returnBodyto specify, it looks upload_ret like it's base64 coded, right! Yes URL安全的Base64编码 (in the seven-kn application, the code mentioned base64 is generally URL安全的Base64编码 ). upload_retyou can get the real content by doing a bit of decoding.

Name of parameter Description
ReturnUrl Upload the end multiplicity directed address.
Returnbody The content of the URL parameter that is used to organize when uploading end multiplicity orientation. upload_ret

Upload Complete callback
We saw a file upload after the completion of the seven cattle server and Business Server interaction is a way, is through the upload side redirection to achieve. Of course, this approach has a suitable scenario. But in most cases, we will use another way to interact with the seven-server and Business Server, which is called a callback (Callback). The callback occurs after the file has been uploaded to seven cows, and seven will send a POST request based on the address specified in the parameters of the upload policy, the content of the callbackUrl request is specified by the upload policy, and callbackBody there is a callbackHost host address that can be used to specify a callback. For example, the IP address, the other callbackBody content organization format is determined by callbackBodyType . The seven-ox server is callbackBodyType Content-Type currently supported application/x-www-form-urlencoded and application/json both formats based on the POST request sent to the business Server.

Name of parameter Description
Callbackurl Seven cows server callback address of the business Server, must be the address that the public network can access, can accept the POST request.
Callbackbody Defines the body content of the POST request when the seven Bull server callbacks the Business Server.
Callbackbodytype Specifies callbackBody the content format, support, url and json two formats.
Callbackhost Specifies the specific callback host address that can be set to an IP address to conserve DNS resolution time.

Trigger persistence after upload
We know that seven cattle support a lot of pictures and audio and video processing of the interface and instructions, if I want to upload a picture or video from the PC, and then upload completed immediately trigger seven of the file processing instructions to upload my files to support different platform files, then I can use the following three parameters.

Parameters Description
Persistentops Data processing instruction set, is called instruction set is to support multiple processing instructions to the original file, each command ; connected together. Here's the instructions for example, imageView2 avthumb and vframe so on.
Persistentnotifyurl This address is used to receive data processing results of seven cattle server, the content of its received and the use of interface prefop documents to query the content is identical format.
Persistentpipeline Specifies the name of the queue to use for data processing, which can be created in a seven bull backend management system. If not specified, indicates that a public queue is used, and data processing may be less efficient than the specified private processing queue.

File Upload Restrictions
We can specify the maximum size of file uploads and the allowed file types in the upload policy.

Parameters Description
Fsizelimit The maximum size of the file upload, in bytes (byte).
Mimelimit The type of file upload can be specified by allowing the semantics ( image/jpeg;image/png which means that only JPEG and PNG format images are allowed) or not allowed semantics ( !application/json;text/plain , which means that JSON and plain text files are not allowed).
Uploading Policies and SDKs

In the latest version of the Python SDK (v7.0), the parameters of the upload policy are defined auth.py inside, as follows:

set([    ‘callbackUrl‘,    ‘callbackBody‘,    ‘callbackHost‘,    ‘returnUrl‘, ‘returnBody‘, ‘endUser‘, ‘saveKey‘, ‘insertOnly‘, ‘detectMime‘, ‘mimeLimit‘, ‘fsizeLimit‘, ‘persistentOps‘, ‘persistentNotifyUrl‘, ‘persistentPipeline‘,])

Let's look at a small example to demonstrate how to use it policy .

   #有key上传, limit the size and mimetype of uploaded files def upload_with_key_and_ Fsizelimit_mimelimt (): bucket =  "IF-PBL" key = " qiniu.jpg "FilePath = "/users/jemy/documents/qiniu.jpg "auth = qiniu. Auth (AccessKey, secretkey) policy = { "Fsizelimit": 1000,  "Mimelimit":  "image/png"} Uptoken = Auth.upload_token (Bucket, Key=key , Policy=policy) retdata, Respinfo = Qiniu.put_file (Uptoken, Key, FilePath, progress_handler=progress) Parseret (retData , Respinfo)              

In this example, we used the policy two parameters fsizeLimit and indicated that mimeLimit we only allowed the upload size to be not greater than 1000字节 the type png of the picture.

More examples of policy other parameters are given in later chapters. Use similar, first understand.

Summary

From what is described above, we can generally draw the following conclusions:

    1. Upload policy determines the space for file storage
    2. Upload policy to set the validity period of the generated upload voucher
    3. Upload policy support file overwrite upload with same name
    4. Upload policy allows you to specify the upload end of the file after the redirect operation
    5. Upload policy You can specify the file upload after the seven cattle server and Business Server interactive operation
    6. Upload policy You can specify the file upload, seven cattle server to a set of data processing operations

Seven Qiniu storage Python SDK usage Tutorial-upload strategy detailed

Related Article

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.