NodeJs上传文件至七牛

2019-01-21 09:52:14

主要是直接输入内容并且保存到七牛


var qiniu = require('qiniu');
var fs = require('fs');

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

var key =  "test1.txt";

var putPolicy = new qiniu.rs.PutPolicy('diary:' + key);

var token = putPolicy.token();


fs.writeFile(key, "hello world!好的,你好", function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
    uploadFile(key);
});

// 上传至七牛
function uploadFile(key) {
    qiniu.io.putFile(token, key, key, null, function(err, ret) {
        if (!err) {
            console.log(ret.key, ret.hash, ret.persistentId);
        } else {
            console.log(err)
        }
    });
}

// 获取下载路径
function getDownload(key) {
    var policy = new qiniu.rs.GetPolicy();
    var url = "http://diary.website.com/" + key;
    var downloadUrl = policy.makeRequest(url);
    console.log(downloadUrl);
}

getDownload("123456.jpg");
--------------------- 
作者:白鼠闹东京 
来源:CSDN 
原文:https://blog.csdn.net/sbt0198/article/details/54572553 
版权声明:本文为博主原创文章,转载请附上博文链接!


  • 2019-09-28 08:00:30

    Java.io.tmpdir介绍

    System.getproperty(“java.io.tmpdir”)是获取操作系统缓存的临时目录,不同操作系统的缓存临时目录不一样,

  • 2019-09-28 08:36:43

    Ehcache配置持久化到硬盘,只存储到硬盘

    Ehcache默认配置的话 为了提高效率,所以有一部分缓存是在内存中,然后达到配置的内存对象总量,则才根据策略持久化到硬盘中,这里是有一个问题的,假如系统突然中断运行 那内存中的那些缓存,直接被释放掉了,不能持久化到硬盘;这种数据丢失,对于一般项目是不会有影响的,但是对于我们的爬虫系统,我们是用来判断重复Url的,所以数据不能丢失;

  • 2019-09-28 09:33:18

    put与putIfAbsent区别

    put在放入数据时,如果放入数据的key已经存在与Map中,最后放入的数据会覆盖之前存在的数据, 而putIfAbsent在放入数据时,如果存在重复的key,那么putIfAbsent不会放入值。

  • 2019-09-29 10:28:04

    程序员实用工具网站

    程序员开发需要具备良好的信息检索能力,为了备忘(收藏夹真是满了),将开发过程中常用的网站进行整理。