Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

qiniu

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qiniu

Node wrapper for Qiniu Resource (Cloud) Storage API

  • 2.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.1K
Maintainers
1
Weekly downloads
 
Created
Source

Node wrapper for Qiniu Resource (Cloud) Storage API

logo

该 SDK 适用于 NodeJS 0.4.7 及其以上版本,基于 七牛云存储官方API 构建。若您的服务端是一个基于 NodeJS 编写的网络程序,使用此 SDK ,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。以便让您应用的终端用户进行高速上传和下载,同时也使得您的服务端更加轻盈。

jscoverage: 85%

安装

npm install qiniu

获取 ACCESS_KEY 和 SECRET_KEY

要对接七牛云存储服务,您需要七牛云存储服务端颁发给您的 ACCESS_KEYSECRET_KEYACCESS_KEY 用于标识客户方的身份,在网络请求中会以某种形式进行传输。SECRET_KEY 作为私钥形式存放于客户方本地并不在网络中传递,SECRET_KEY 的作用是对于客户方发起的具体请求进行数字签名,用以保证该请求是来自指定的客户方并且请求本身是合法有效的。使用 ACCESS_KEY 进行身份识别,加上 SECRET_KEY 进行数字签名,即可完成应用接入与认证授权。

您可以通过如下步骤获得 ACCESS_KEYSECRET_KEY

  1. 开通七牛开发者帐号
  2. 登录七牛开发者自助平台,查看 ACCESS_KEY 和 SECRET_KEY

获取到 ACCESS_KEYSECRET_KEY 之后,您就可以参考下面的示例代码进行接入使用了。

使用

SDK 使用文档参考:http://docs.qiniutek.com/v3/sdk/nodejs/

示例程序

var qiniu = require('qiniu');

// 配置密钥
qiniu.conf.ACCESS_KEY = '<Please apply your access key>';
qiniu.conf.SECRET_KEY = '<Dont send your secret key to anyone>';

// 实例化带授权的 HTTP Client 对象
var conn = new qiniu.digestauth.Client();

// 创建空间,也可以在开发者自助网站创建
var bucket = 'yet_another_bucket';
qiniu.rs.mkbucket(conn, bucket, function(resp) {
    console.log("\n===> Make bucket result: ", resp);
    if (resp.code != 200) {
        return;
    }
});

// 实例化 Bucket 操作对象
var rs = new qiniu.rs.Service(conn, bucket);

// 上传文件第1步
// 生成上传授权凭证(uploadToken)
var opts = {
    scope: "yet_another_bucket",
    expires: 3600,
    callbackUrl: "http://www.example.com/notifications/qiniurs", // 可选
    callbackBodyType: "application/x-www-form-urlencoded", // 可选
    customer: "username@example.com" // 可选
};
var token = new qiniu.auth.UploadToken(opts);
var uploadToken = token.generateToken();

// 上传文件第2步
// 组装上传文件所需要的参数
var key = __filename;
var localFile = key,
    customMeta = "",
    callbackParams = {"bucket": bucket, "key": key},
    enableCrc32Check = false,
    mimeType = mime.lookup(key);

// 上传文件第3步
// 上传文件
rs.uploadFileWithToken(uploadToken, localFile, key, mimeType, customMeta, callbackParams, enableCrc32Check, function(resp){
    console.log("\n===> Upload File with Token result: ", resp);
    if (resp.code != 200) {
        // ...
        return;
    }

    // 查看已上传文件属性信息
    rs.stat(key, function(resp) {
        console.log("\n===> Stat result: ", resp);
        if (resp.code != 200) {
            // ...
            return;
        }
    });
});


// 获取文件下载链接(含文件属性信息)
var saveAsFriendlyName = key;
rs.get(key, saveAsFriendlyName, function(resp) {
    console.log("\n===> Get result: ", resp);
    if (resp.code != 200) {
        // ...
        return;
    }
});

// 删除已上传文件
rs.remove(key, function(resp) {
    console.log("\n===> Delete result: ", resp);
});

// 将bucket的内容作为静态内容发布
var DEMO_DOMAIN = bucket + '.dn.qbox.me';
rs.publish(DEMO_DOMAIN, function(resp){
    console.log("\n===> Publish result: ", resp);
    if (resp.code != 200){
        clear(rs);
        return;
    }
});

// 删除bucket,慎用!
rs.drop(function(resp){
    console.log("\n===> Drop result: ", resp);
});

贡献代码

  1. Fork
  2. 创建您的特性分支 (git checkout -b my-new-feature)
  3. 提交您的改动 (git commit -am 'Added some feature')
  4. 将您的修改记录提交到远程 git 仓库 (git push origin my-new-feature)
  5. 然后到 github 网站的该 git 远程仓库的 my-new-feature 分支下发起 Pull Request

许可证

Copyright (c) 2012 qiniutek.com

基于 MIT 协议发布:

Keywords

FAQs

Package last updated on 28 Dec 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc