Upload the js generated by vue to qiniu instance, vuejs

Source: Internet
Author: User

Upload the js generated by vue to qiniu instance, vuejs

Generally, for vue projects, we directly upload the final css js and other files to the server without cdn services.

This generally has two drawbacks,

  1. Increase the server bandwidth pressure. Once the access traffic goes up, the server may be suspended due to bandwidth pressure.
  2. Access speed may slow down in some regions

Although I know it, I am tired of manually uploading the js css file to qiniu every time.
It is a waste of time to upload qiniu cdn each time, especially when changes are made frequently.

So I went to the documents of qiniu and decided to use the scripts to solve this problem. I usedpython Sdk implementation.

python Install the environment on your own

First download the qiniu package

pip install qiniu

Then install the third-party dependency package glob2

pip install glob2

Upqiniu. py

#-*-Coding: UTF-8-*-from qiniu import Auth, put_file, import qiniu. configimport glob2import OS # Enter your Access Key and Secret Keyaccess_key = '*********************** 'secret _ key = *********************** '# Build an authentication object q = Auth (access_key, secret_key) # The bucket to be uploaded bucket_name = '*****' resources = glob2.glob ('dist/static/** ') def upload (path): file_name = path. replace ('dist/', '') key = file_name token = q. upload_token (bucket_name, key, 3600) localfile = '. /'+ path put_file (token, key, localfile) for r in resources: if OS. path. isfile (r): upload (r)

You need to put this script (upqiniu. py) in the directory of the same level as dist, for example:


Image.png

Access_key secret_key in the script. You can find it in your profile after logging on to qiniu:


You also need to change the publicPath of webpack to the address of qiniu.


In the vue project, you only need to change index. js under the config directory.


Although it is a little difficult to change, once and for all,

Firstnpm run build

Then python upqiniu.py

Finally index.htmlPull to the online server.

Ps: This script will dist/static/ Upload All files to qiniu, including font files and images.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.