@gaoding/oss-uploader

upload file through ali-oss

Usage no npm install needed!

<script type="module">
  import gaodingOssUploader from 'https://cdn.skypack.dev/@gaoding/oss-uploader';
</script>

README

oss-upload

upload file by ali-oss

Installation

npm install -S @gaoding/oss-uploader

Node Usage

example:

const OSSUploader = require('@gaoding/oss-uploader');

const ossUpload = new OSSUploader({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>',
    bucket: '<bucket>',
    region: '<oss region>',
    internal: false
});

ossUploader.upload(file, key, {
    meta: {
        'Content-Type': 'font/woff'
    }
})
.then(result => {
    // result:
    // {
    //   name: 'test/index.e28ef59.js',
    //   url: 'https://test-cdn.oss-cn-hangzhou.aliyuncs.com/test/index.e28ef59.js',
    //   res: {
    //      status: 200,
    //      statusCode: 200,
    //      headers: {
    //          server: "Tengine",
    //          content - type: "application/json",
    //          content - length: "148",
    //          connection: "keep-alive",
    //          date: "Tue, 31 Mar 2015 11:06:32 GMT",
    //          "last-modified": "Mon, 30 Mar 2015 10:46:35 GMT"
    //      },
    //      size: 148,
    //      aborted: false,
    //      rt: 461,
    //      keepAliveSocket: false
    //   }
    // }
});

Browser Usage


import OSSUploader from '@gaoding/oss-uploader';

const ossUploader = new OSSUploader({
    key: '0/dianshang/20190214-111136-66fa.png',
    access_key: '<access_key>',
    action: '//st-gdx.dancf.com',
    oss_action: '//dancf-st-gdx.oss-cn-hangzhou.aliyuncs.com',
    policy: '<policy from server>'
    signature: '<signature from server>'
});
ossUploader.upload(file, options)
.then(result => {
    // result:
    // {
    //   key: '0/dianshang/20190214-111136-66fa.png'
    // }
});

Create An Instance In Node

options:

  • accessKeyId {String} access key you create on aliyun console website
  • accessKeySecret {String} access secret you create
  • bucket {String} the bucket name
  • region {String} the bucket data region location, default is oss-cn-hangzhou
  • internal {Boolean} access OSS with aliyun internal network or not, default is false

API

.upload(file, key, options)

Upload local files to cdn using secret key, return Promise.

parameters:

  • file {String|Buffer} object local path
  • key {String} object name store on OSS, if missing, it will be given a random value
  • options {Object} other information
    • bucket {String} the bucket name
    • meta {Object} including HTTP header and custom meta information

.getImageInfo(key)

Get target image's information. (format,size,width,height)

parameters:

  • key {String} object name store on OSS

.uploadStream(stream, key, bucket)

Upload stream file.

parameters:

  • stream {ReadStream} object ReadStream content instance
  • key {String} object name store on OSS
  • bucket {String} the bucket name

.uptoken(key)

Generate token.

parameters:

  • key {String} object name store on OSS

Create An Instance In Browser

options:

  • key {String} object name store on OSS
  • access_key {String} access key you create on aliyun console website
  • action {String} oss upload address
  • oss_action {String} the internal oss upload address, there may be an intranet bonus (East China region)
  • policy {String} upload certificate
  • signature {String} upload certificate

API

.upload(file, options)

Upload file,return Promise.

parameters:

  • file {String|Buffer|ReadStream|File|Blob} object local path, content buffer or ReadStream content instance use in Node, Blob and html5 File
  • options {Object}
    • filename {String}
    • action {String} upload url,
    • data {String} formData,
    • timeout {Number} upload timeout,
    • headers {Object},
    • withCredentials {Boolean},
    • onProgress {Function}
    • imageActionUrl {String} the url to compress image, default is //image-uploader.dancf.com/

.abort(file)

Abort upload request.

parameters:

  • file {String|Buffer|ReadStream|File|Blob} uploading file