This article mainly introduces the use of the interface provided by seven cattle to the seven qiniu as a graph bed, with Python to achieve image upload and file upload script implementation, can support bulk upload by folder.
Build the site of the students, must be related to the picture upload and file upload problems, recently in order to get a picture of the resources to contact the cloud storage, but also very useful.
Before if we put the picture resources to the local server, that every time you write the article need to prepare a bunch of pictures, and to upload to the corresponding relative directory up, if the use of cloud storage (not limited to seven cattle), the advantage is that the resources needed to upload directly, and then generate a link to use on the line, Markdown Syntax support for links is also good, yes, you can do it.
However, there is a new problem, is to use cloud storage, you have to go through the login, open object storage, open content management, upload, and then fill in the relative directory after uploading, and manually select local files, is not very troublesome?
So this is a one-click script to upload resources (only for seven qiniu), the effect is to enter the project directory and file local address, automatically upload all files to the cloud, and generate a URL to use.
Come on, directly on the code:
#-*-Coding:utf-8-*-import osimport sysfrom qiniu import Auth, Put_fileif 3! = Len (SYS.ARGV): Print (' [Usage]%s [dir _SET] [filepath] '% os.path.basename (sys.argv[0]) sys.exit (0) Else: # Dir_set in the format image/upload-qiniu/, note the end with a backslash/ Dir_set = sys.argv[1] File_path = sys.argv[2]# Personal Center, Key Management->akaccess_key = ' Your accesskey ' # Personal center, Key Management->sksec Ret_key = ' Your secretkey ' # Seven cow space name bucket_name = ' Your storage space name ' Qiniu_auth = Auth (Access_key, Secret_key) def upload_qiniu (input_ Path): #upload single file to Qiniu filename = os.path.basename (input_path) key = '%s%s '% (dir_set, filename) token = Qiniu_auth.upload_token (Bucket_name, key) ret, info = put_file (token, key, Input_path, check_crc=true) if R ET and ret[' key '] = = Key:print ('%s done '% (' http://www.sylan215.com/' + dir_set + filename) else:prin T ('%s error '% (' http://www.sylan215.com/' + dir_set + filename) def upload_all_files (Input_path): If Os.path.isfile (in Put_path): Upload_qiniU (input_path) elif Os.path.isdir (input_path): Dirlist = Os.walk (input_path) for root, dirs, files in Dirl ist:for filename in Files:upload_qiniu (os.path.join (root, filename)) else:print (' Pl Ease input the exists file path! ') if __name__ = = "__main__": Upload_all_files (File_path)
Instructions for use 1. Please install Qiniu's dependent library first:
python -m pip install qiniu
2. Modify the bound domain name
Modify the http://www.sylan215.com in the code to bind the domain name for yourself in the Seven cow cloud storage
3. Execute a script with two parameters
The first parameter is the post-upload directory, and the second parameter is the file's local address, which supports the folder.
The directory format for the first parameter is: image/test/.
For example, the following command:
upload_to_qiniu.py image/test/ d:\test.png
After running, the uploaded file address is similar: http://www.sylan215.com/image/test/test.png
PS: Has been used Python2.7 and Python3.4 Pro test effective, other Python version if there is an exception, please feedback.
For more detailed instructions, please go to the project address: Https://github.com/sylan215/upload-to-qiniu, Welcome to star, and together enrich the functionality of this script.
Use seven cow interface to achieve picture and file upload with one click