Python's Django upload file to seven Cow cloud storage

Source: Internet
Author: User
Tags html form
A recently written Django small project needs to implement the user's ability to upload images, which is recorded by using the seven cow cloud storage. The seven kn python SDK version I'm using here is 7.0.3, which may be somewhat different from the old version.

The original file upload needs to upload the file to its own business Server, and then upload from the business Server to cloud storage. Now seven of cattle form upload can be directly uploaded files to seven cattle, no longer need to transit the Business Server, saving the traffic cost, reduce the pressure of the business Server. It also allows the client to automatically redirect to a successful upload results page after the file upload is complete. Here I am using a seven-ox form upload.

Form upload

The HTML form code for the user to upload the picture is as follows. Where key is used to specify the file name of the image stored in seven cows, token is the upload voucher, which is used to verify the legality and set the return information.

Upload.html

<form method= "POST" action= "http://upload.qiniu.com/" enctype= "Multipart/form-data" ><input name= "key" type = "hidden" value= "" ><input name= "token" type= "hidden" value= "" ><input name= "file" type= "file" >< Input type= "Submit" ></form>

The key code in the view function that jumps to the HTML page above is as follows. Where the Upload_token function is used to generate the token field in the form, 7200 of the Upload_token function represents the validity period of the upload voucher, ReturnUrl indicates the redirected address after the successful upload, and Returnbody indicates the information returned by the seven cows during redirection. It is a base64 encoded JSON data, need to decode to get JSON data, when the error message upload error is directly in the URL in the form of plaintext, and will not be in the returned JSON data. You can also limit the types of files you upload by setting mimelimit.

views.py

Import Qiniuimport Uuidaccess_key = ' Seven cow-assigned public key ' Secret_key = ' Seven cow-assigned private key ' Bucket_name = ' save file's warehouse name ' key = str (UUID.UUID1 ()). Replace ('-', ') # here uses the UUID as the name of the file saved in the seven cow. and remove the "-" q = Qiniu in the UUID. Auth (Access_key, secret_key) token = Q.upload_token (Bucket_name, KEY, 7200, {' ReturnUrl ': ' Http://127.0.0.1:8000/ Photos/uploadprocessor ', ' returnbody ': ' {' name ': $ (fname), ' key ': $ (key)} ', ' Mimelimit ': ' Image/jpeg;image/png '}) Return Render_to_response (' photos/upload.html ', {' token ': token, ' key ': Key}, Context_instance=requestcontext ( Request))
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.