搞了一天終於搞定了 用node上傳圖片到七牛 代碼如下: const qiniu_sdk = require('qiniu') qiniu_sdk.conf.ACCESS_KEY = '***********************'; qiniu_sdk.conf.SECRET_KEY = '** ...
搞了一天終於搞定了 用node上傳圖片到七牛
代碼如下:
const qiniu_sdk = require('qiniu') qiniu_sdk.conf.ACCESS_KEY = '***********************'; qiniu_sdk.conf.SECRET_KEY = '***********************'; // 要上傳的空間 const bucket = "me-jd" // 文件首碼 const prefix = 'image/me/' // 生成上傳文件的 token // isPrefixalScope: 1,{ scope: bucket + ':' + key } const token = (bucket, key) => { const policy = new qiniu_sdk.rs.PutPolicy({isPrefixalScope: 1, scope: bucket + ':' + key })//scope: "me-jd" + ':' + 'image/activity/nianhuo' return policy.uploadToken() } const config = new qiniu_sdk.conf.Config() ; //設置空間位置為華南 config.zone=qiniu_sdk.zone.Zone_z2; const upload_file = (file_name, file_path) => { // 保存到七牛的地址 const file_save_path = prefix + file_name // 七牛上傳的token const up_token = token(bucket, file_save_path) //把'me-jd','image/activity/nianhuo' 傳入 const extra = new qiniu_sdk.form_up.PutExtra() const formUploader = new qiniu_sdk.form_up.FormUploader(config) // 上傳文件 formUploader.putFile(up_token, file_save_path, file_path, extra, (err, ret) => {
//參數為 1:上傳憑證,2:目標文件名 ,3:本機文件路徑 ,4:額外選項 if (!err) { // 上傳成功, 處理返回值 console.log(ret); } else { // 上傳失敗, 處理返回代碼 console.error(err); } }); } // console.log(qiniu_sdk) upload_file("nianhuo","../dist/static/images/5ad87bf0N66c5db7c.webp")