


This article mainly introduces the method of connecting small programs to Qiniu Cloud Storage. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
Foreword:
I have been doing small programs for a while. Let me summarize the technical points I have made. , hereby contribute to my friends! In the project, there are three types of image storage, video storage, and recording storage that require cloud docking storage.
1. Upload pictures/videos/recordings for Qiniu docking
Preparation work:
a. You need to have a Qiniu account. After real-name authentication, there is a secret key management in Qiniu's personal center - there is a pair of secret keys in it, which are necessary for uploading. This pair of secret keys is configured in the backend. During configuration, you can set the allowed upload format, or you can set it to upload in any format. Let our backend brother do it slowly. In addition, you also need to create a new storage space in Qiniu's object storage. The files to be uploaded will be stored in the space you created. If you want to facilitate management, you can also create a storage space for pictures/videos/recordings/others. This storage space name must also be configured on the backend.
b. An upload token is required. One file upload corresponds to one token, which is necessary. Uploading tokens is also time-sensitive. The backend configuration takes 1 hour, which is enough for you to complete the upload operation. This token is generated by our own backend, and the frontend adjusts the interface to obtain the token, or like me, just throw the interface directly behind [uptokenURL], and Qiniu will get it by itself. We can also get the token ourselves first and then throw it to Qiniu.
uptokenURL:'https://get.qiniutoken.com/minibx/geo_f/gain_qn_toke', uploadURL:'https://up.qbox.me',//华东
uptoken: token,uploadURL:'https://up.qbox.me',//华东
c. Qiniu’s js file qiniuUploader.js can be downloaded from the Qiniu address given below. There is an SDK for small programs inside. Unzip it and find qiniuUploader.js inside. Import this js on the page you need to upload. https://developer.qiniu.com/sdk#community-sdk
1. Image upload Qiniu
provides users with the ability to add local Picture, or take a photo, and then you will get the temporary path of the picture returned by the method. We can maintain the images in an array, so that when uploading Qiniu, they will be uploaded in the form of a queue.
constqiniuUploader = require("../../libs/qiniuUploader.js"); var sourceType = [['camera'], ['album'], ['camera','album']]; var sizeType = [['compressed'], ['original'], ['compressed','original']]; var imageArray = [];// 点击事件,从本地相册选择图片或使用相机拍照。 chooseImage: function (e) { var that =this; wx.chooseImage({ sourceType: sourceType[this.data.sourceTypeIndex], sizeType: sizeType[this.data.sizeTypeIndex], count:this.data.count[this.data.countIndex],//这个count可以用来删除当前图片 success: function (res) { // 返回照片的本地文件路径,tempFilePath可以作为img标签的src属性显示图片vartempFilePaths = res.tempFilePaths; imageArray.push(tempFilePaths); that.setData({ imageList: tempFilePaths }) that.pictureUploadqiniuMethod(imageArray,"tupian_"); }, }) }, //得到图片路径数组后,准备上传七牛 pictureUploadqiniuMethod: function (imageArray, fileHead){ var that =this; for(vari =0; i < imageArray.length; i++) { var filePath = imageArray[i]; var imgName = filePath.substr(30,50); qiniuUploader.upload(filePath, (res) => { //上传成功,上传成功一张图片,进入一次这个方法,也就是返回一次 console.log(res) }, (error) => { //图片上传失败,可以在七牛的js里面自己加了一个err错误的返回值console.log('error: '+ error) }, { domain:'oqxfq54dn.bkt.clouddn.com', uptokenURL:'https://get.qiniutoken.com/minibx/geo_f/gain_qn_token', uploadURL:'https://up.qbox.me',//华东key: fileHead + imgName,// 自定义文件 keyregion:'ECN', }); } },
The code in the red line box is in the Qiniu qiniuUploader.js file. I added an if judgment to prevent data from being returned even though the upload was successful. In this way, the return is obtained in line 112 of the code. data, an error will not be reported directly.
Explanation:
imageArray: Temporary address array of images to be uploaded.
fileHead: Customize the header of uploaded Qiniu file name. In order to distinguish uploaded files, such as pictures/videos/recordings/others,
imgName: Customize the uploaded Qiniu file name, front-end processing Well, I simply intercepted the 30-50 characters of the temporary path (filePath) as the file name stored in Qiniu. Even if you add two identical pictures, the temporary path given to you by the mini program will be different. So there will be no duplication of names.
domain: used when downloading resources. If set, the res.ImageURL field returned in the success after uploading is a directly accessible file address with http, otherwise you need to splice it yourself.
key: The file name finally stored in Qiniu. The image file name here = file header (fileHead) + pseudo file name (imgName)
uploadURL: The one uploaded to Qiniu The storage area, upload area and upload area code must correspond.
region: Upload region code.
shouldUseQiniuFileName: Indicates whether Qiniu defines the uploaded file name. If it is true, the file key is assigned by the qiniu server (global deduplication). The default is false, which is defined by ourselves. If the key is set, the priority is the highest.
Qiniu storage area table:
In this way, you can upload by calling QiniuUploader.js on the page that needs to be uploaded .
Problems you will encounter:
Possible reasons for failure to upload a certain picture/video/recording:
①The uploaded file is not supported by the backend Allowed size, pictures are generally smaller than 3M. My video/recording limit is less than 1M
②The uploaded file format is not allowed in the backend.
③Token acquisition failed. For example, what I encountered was that Qiniu’s qiniuUploader.js file obtains the token through the interface. The default is [var token = res.data.token;], and our backend interface returns The token is like this
#So I need to change it in Qiniu’s js file to [var token = res.data.extra;], or let the backend change it.
2. Video Upload Qiniu
Video uploading and picture uploading are the same routine, but the file formats are different. Videos are generally just one file, unlike pictures with multiple , you need to create a queue to upload. So upload the video like this:
//事件绑定,添加视频 chooseVideo: function (res) { var that =this; wx.chooseVideo({ sourceType: sourceType[this.data.sourceTypeIndex], camera: camera[this.data.cameraIndex], maxDuration: duration[this.data.durationIndex], success: function (res) { var shipinFile= res.tempFilePath; that.setData({ src: shipinFile }); //用户寻选择好图片后,调用上传方法 that.shipinUploadqiniuMethod(shipinFile,"shipin_"); } }) }, //视频上传七牛 shipinUploadqiniuMethod: function (shipinFile, fileHead){ var that =this; var shipinName = shipinFile.substr(30,50); qiniuUploader.upload(shipinFile, (res) => { //视频上传成功console.log(res) }, (error) => { //视频上传失败,可以在七牛的js里面自己加了一个err错误的返回值 console.log('error: '+ error) }, { domain:'oqxfq54dn.bkt.clouddn.com', uptokenURL:'https://get.qiniutoken.com/minibx/geo_f/gain_qn_token', uploadURL:'https://up.qbox.me',//华东 key: fileHead + shipinName ,// 自定义文件 keyregion:'ECN',//华东区域代码}); } },
3、录音文件上传七牛
小程序的录音格式为silk,录音上传七牛,可以和视频共用一个方法。但虽然上传成功了,状态码为403,七牛没有返回data,像这样:
正常上传时,能正常返回data,并且状态码是200
后端配置silk格式允许,这样应该是没问题的。
上传成功七牛却没有返回data,这个data里有文件传七牛那边在线地址,不返回我们怎么访问了。现在的处理是:把音频文件传到自己服务器。目前就只能这么办了。
The above is the detailed content of An example of how WeChat applet can be connected to Qiniu Cloud Storage. For more information, please follow other related articles on the PHP Chinese website!

随着移动互联网技术和智能手机的普及,微信成为了人们生活中不可或缺的一个应用。而微信小程序则让人们可以在不需要下载安装应用的情况下,直接使用小程序来解决一些简单的需求。本文将介绍如何使用Python来开发微信小程序。一、准备工作在使用Python开发微信小程序之前,需要安装相关的Python库。这里推荐使用wxpy和itchat这两个库。wxpy是一个微信机器

小程序能用react,其使用方法:1、基于“react-reconciler”实现一个渲染器,生成一个DSL;2、创建一个小程序组件,去解析和渲染DSL;3、安装npm,并执行开发者工具中的构建npm;4、在自己的页面中引入包,再利用api即可完成开发。

实现思路x01服务端的建立首先,在服务端,使用socket进行消息的接受,每接受一个socket的请求,就开启一个新的线程来管理消息的分发与接受,同时,又存在一个handler来管理所有的线程,从而实现对聊天室的各种功能的处理x02客户端的建立客户端的建立就要比服务端简单多了,客户端的作用只是对消息的发送以及接受,以及按照特定的规则去输入特定的字符从而实现不同的功能的使用,因此,在客户端这里,只需要去使用两个线程,一个是专门用于接受消息,一个是专门用于发送消息的至于为什么不用一个呢,那是因为,只

微信小程序是一种轻量级的应用程序,可以在微信平台上运行,不需要下载安装,方便快捷。Java语言作为一种广泛应用于企业级应用开发的语言,也可以用于微信小程序的开发。在Java语言中,可以使用SpringBoot框架和第三方工具包来开发微信小程序。下面是一个简单的微信小程序开发过程。创建微信小程序首先,需要在微信公众平台上注册一个小程序。注册成功后,可以获取到

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了如何在小程序中用公众号模板消息,下面一起来看一下,希望对大家有帮助。

PHP与小程序的地理位置定位与地图显示地理位置定位与地图显示在现代科技中已经成为了必备的功能之一。随着移动设备的普及,人们对于定位和地图显示的需求也越来越高。在开发过程中,PHP和小程序是常见的两种技术选择。本文将为大家介绍PHP与小程序中的地理位置定位与地图显示的实现方法,并附上相应的代码示例。一、PHP中的地理位置定位在PHP中,我们可以使用第三方地理位

随着小程序的广泛应用,越来越多的开发者需要将其与后台服务器进行数据交互,其中最常见的业务场景之一就是上传文件。本文将介绍在小程序中实现文件上传的PHP后台实现方法。一、小程序中的文件上传在小程序中实现文件上传,主要依赖于小程序APIwx.uploadFile()。该API接受一个options对象作为参数,其中包含了要上传的文件路径、需要传递的其他数据以及

PHP与小程序的第三方登录与绑定功能实现随着互联网的发展和智能手机的普及,小程序成为了移动应用程序开发的热门选择。小程序不仅提供了优秀的用户体验,还具备各种强大的功能。其中,第三方登录与绑定是小程序中常见的功能之一。本文将介绍如何使用PHP与小程序实现第三方登录与绑定的功能,并为读者提供代码示例。第三方登录是指用户可以使用其他平台的账号信息登录到目标平台,而


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.