How to use qiniu - 10 common examples

To help you get started, we’ve selected a few qiniu examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github qiniu / js-sdk / test / server.js View on Github external
var mac = new qiniu.auth.digest.Mac(config.AccessKey, config.SecretKey);
var config2 = new qiniu.conf.Config();
// 这里主要是为了用 node sdk 的 form 直传,结合 demo 中 form 方式来实现无刷新上传
config2.zone = qiniu.zone.Zone_z2;
var formUploader = new qiniu.form_up.FormUploader(config2);
var putExtra = new qiniu.form_up.PutExtra();
var options = {
  scope: config.Bucket,
  deleteAfterDays: 1,
  returnBody:
    '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}'
};

var putPolicy = new qiniu.rs.PutPolicy(options);
var bucketManager = new qiniu.rs.BucketManager(mac, null);

app.get("/api/uptoken", function(req, res, next) {
  var token = putPolicy.uploadToken(mac);
  res.header("Cache-Control", "max-age=0, private, must-revalidate");
  res.header("Pragma", "no-cache");
  res.header("Expires", 0);
  if (token) {
    res.json({
      uptoken: token,
      domain: config.Domain
    });
  }
});

app.post("/api/transfer", function(req, res) {
  var form = new multiparty.Form();
github qiniu / nodejs-sdk / examples / rs_batch_change_type.js View on Github external
const qiniu = require('qiniu');
const proc = require('process');

var accessKey = proc.env.QINIU_ACCESS_KEY;
var secretKey = proc.env.QINIU_SECRET_KEY;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
var config = new qiniu.conf.Config();
var bucketManager = new qiniu.rs.BucketManager(mac, config);

var srcBucket = proc.env.QINIU_TEST_BUCKET;

// 每个operations的数量不可以超过1000个,如果总数量超过1000,需要分批发送
var changeTypeOperations = [
    qiniu.rs.changeTypeOp(srcBucket, 'qiniu1.mp4', 1),
    qiniu.rs.changeTypeOp(srcBucket, 'qiniu2.mp4', 1),
    qiniu.rs.changeTypeOp(srcBucket, 'qiniu3.mp4', 1),
    qiniu.rs.changeTypeOp(srcBucket, 'qiniu4.mp4', 1)
];

bucketManager.batch(changeTypeOperations, function (err, respBody, respInfo) {
    if (err) {
        console.log(err);
    // throw err;
    } else {
github qiniu / nodejs-sdk / examples / rs_batch_delete_after_days.js View on Github external
const qiniu = require('qiniu');
const proc = require('process');

var accessKey = proc.env.QINIU_ACCESS_KEY;
var secretKey = proc.env.QINIU_SECRET_KEY;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
var config = new qiniu.conf.Config();
var bucketManager = new qiniu.rs.BucketManager(mac, config);

var srcBucket = proc.env.QINIU_TEST_BUCKET;

// 每个operations的数量不可以超过1000个,如果总数量超过1000,需要分批发送
var deleteAfterDaysOperations = [
    qiniu.rs.deleteAfterDaysOp(srcBucket, 'qiniu1.mp4', 10),
    qiniu.rs.deleteAfterDaysOp(srcBucket, 'qiniu2.mp4', 10),
    qiniu.rs.deleteAfterDaysOp(srcBucket, 'qiniu3.mp4', 10),
    qiniu.rs.deleteAfterDaysOp(srcBucket, 'qiniu4.mp4', 10)
];

bucketManager.batch(deleteAfterDaysOperations, function (err, respBody, respInfo) {
    if (err) {
        console.log(err);
    // throw err;
    } else {
github arterli / CmsWing / src / controller / ext / attachment / service / qiniu.js View on Github external
async pfop() {
    qiniu.conf.ACCESS_KEY = this.accessKey;
    qiniu.conf.SECRET_KEY = this.secretKey;

    // 要转码的文件所在的空间和文件名
    const bucket = this.bucket;
    const key = 'thinkjs-create-project.mp4';

    // 转码所使用的队列名称。
    const pipeline = 'abc';

    // 要进行转码的转码操作。
    let fops = 'avthumb/mp4/s/640x360/vb/1.25m';

    // 可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定文件会默认命名并保存在当前空间
    const saveas_key = qiniu.util.urlsafeBase64Encode(saved_bucket + ':' + saved_key);
    fops = fops + '|saveas/' + saveas_key;
    // console.log(saveas_key);
    const opts = {
github cag2050 / qiniu_upload / create_uptoken.js View on Github external
var qiniu = require("qiniu");
//需要填写你的 Access Key 和 Secret Key
qiniu.conf.ACCESS_KEY = 'iUTbwTRLotclpFa8kHoeSUvgxgvH1WL2-ROdbY7B';
qiniu.conf.SECRET_KEY = 'pArurQYI4YcMJK1hA6HAvArS0xNMmRVH70627SkQ';
//要上传的空间
var bucket_name = 'testvideo2';
//上传到七牛后保存的文件名
// key = 'my-nodejs-logo.png';
//构建上传策略函数
function create_uptoken(bucket_name) {
    var putPolicy = new qiniu.rs.PutPolicy(bucket_name);
    return putPolicy.token();
}
//生成上传 Token
var uptoken = create_uptoken(bucket_name);

console.log('uptoken =')
console.log(uptoken)
github qiniu / nodejs-sdk / examples / rsDemo.js View on Github external
var qiniu = require('qiniu');
var path = require('path');

// 初始化ak,sk
qiniu.conf.ACCESS_KEY = 'ACCESS_KEY';
qiniu.conf.SECRET_KEY = 'SECRET_KEY';

var EntryPath = qiniu.rs.EntryPath;
var EntryPathPair = qiniu.rs.EntryPathPair;

//bucket 空间名
//key 文件名
var client = new qiniu.rs.Client();
client.stat(bucket, key,function(err, ret){
 if (!err) {
      // 上传成功, 处理返回值
      console.log(ret.hash);
      console.log(ret);
      //ret.should.have.keys('hash', 'fsize', 'putTime', 'mimeType');
      // ret.key & ret.hash
    } else {
github bimohxh / webon / lib / qiniu.js View on Github external
var qiniu = require('qiniu')
var Helper = require('./helper')
var request = require('request')
var querystring = require('querystring')

var localEnv = Helper.readLocalEnv()

qiniu.conf.ACCESS_KEY = localEnv.qiniu.accessKey
qiniu.conf.SECRET_KEY = localEnv.qiniu.secretKey

let bucket = localEnv.qiniu.bucket

var qn = {

  // 构建上传策略函数
  uptoken: (bucket, key) => {
    var putPolicy = new qiniu.rs.PutPolicy(bucket + ':' + key)
    return putPolicy.token()
  },

  // 普通上传
  upload: (file, removePrefix) => {
    let fullname = Helper.uploadFileName(file, removePrefix)
    console.log('===', removePrefix, fullname)
github qiniu / nodejs-sdk / examples / cdn_get_bandwidth_data.js View on Github external
const qiniu = require('qiniu');
const proc = require('process');

// 域名列表
var domains = [
    'if-pbl.qiniudn.com',
    'qdisk.qiniudn.com'
];

// 指定日期
var startDate = '2017-06-20';
var endDate = '2017-06-22';
var granularity = 'day';
var accessKey = proc.env.QINIU_ACCESS_KEY;
var secretKey = proc.env.QINIU_SECRET_KEY;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

var cdnManager = new qiniu.cdn.CdnManager(mac);
// 获取域名带宽
cdnManager.getBandwidthData(startDate, endDate, granularity, domains, function (
    err, respBody, respInfo) {
    if (err) {
        console.log(err);
        throw err;
    }

    console.log(respInfo.statusCode);
    if (respInfo.statusCode == 200) {
        var jsonBody = JSON.parse(respBody);
        var code = jsonBody.code;
        console.log(code);
github qiniu / nodejs-sdk / examples / cdn_prefetch_urls.js View on Github external
const qiniu = require('qiniu');
const proc = require('process');

// URL 列表
var urlsToPrefetch = [
    'http://if-pbl.qiniudn.com/nodejs.png',
    'http://if-pbl.qiniudn.com/qiniu.jpg'
];
var accessKey = proc.env.QINIU_ACCESS_KEY;
var secretKey = proc.env.QINIU_SECRET_KEY;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
var cdnManager = new qiniu.cdn.CdnManager(mac);
// 预取链接
cdnManager.prefetchUrls(urlsToPrefetch, function (err, respBody, respInfo) {
    if (err) {
        throw err;
    }

    console.log(respInfo.statusCode);
    if (respInfo.statusCode == 200) {
        var jsonBody = JSON.parse(respBody);
        console.log(jsonBody.code);
        console.log(jsonBody.error);
        console.log(jsonBody.requestId);
        console.log(jsonBody.invalidUrls);
        console.log(jsonBody.invalidDirs);
        console.log(jsonBody.urlQuotaDay);
github qiniu / nodejs-sdk / examples / rs_change_type.js View on Github external
const qiniu = require('qiniu');
const proc = require('process');

var accessKey = proc.env.QINIU_ACCESS_KEY;
var secretKey = proc.env.QINIU_SECRET_KEY;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
var config = new qiniu.conf.Config();
// config.useHttpsDomain = true;
config.zone = qiniu.zone.Zone_z0;
var bucketManager = new qiniu.rs.BucketManager(mac, config);
var bucket = proc.env.QINIU_TEST_BUCKET;
var key = 'qiniu.mp4';
var newType = 1; // 低频存储

bucketManager.changeType(bucket, key, newType, function (err, respBody, respInfo) {
    if (err) {
        console.log(err);
    // throw err;
    } else {
    // 200 is success
        console.log(respInfo.statusCode);
        console.log(respBody);