Skip to content

Instantly share code, notes, and snippets.

@kols
Created July 11, 2013 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kols/5976283 to your computer and use it in GitHub Desktop.
Save kols/5976283 to your computer and use it in GitHub Desktop.
upyun node.js lib concept
request = require "request"
url = require 'url'
fs = require 'fs'
path = require 'path'
crypto = require 'crypto'
UPYUN_API_HOSTS =
AUTO: "v0.api.upyun.com"
TELECOM: "v1.api.upyun.com"
NETCOM: "v2.api.upyun.com"
RAILCOM: "v3.api.upyun.com"
class UpYun
constructor: (@bucket, @auth, @ssl, @api_host) ->
@protocol = if @ssl then 'https:' else 'http:'
digest = (data) ->
md5sum = crypto.createHash('md5')
md5sum.update(data)
md5sum.digest('hex')
url: (rpath) ->
fullPath = path.join('/', @bucket,
if rpath.charAt(0) == '/' then rpath.substr(1) else rpath)
url.format(
protocol: @protocol
hostname: @api_host || UPYUN_API_HOSTS.AUTO
pathname: fullPath
)
put: (path, file, verify, mimetype, secret, headers, mkdir = true) ->
headers = headers || {}
if mimetype?
headers['content-type'] = mimetype
if secret?
headers['content-secret'] = secret
if mkdir
headers['mkdir'] = 'true'
fs.readFile(file, (err, data) =>
body = if err then file else data
if verify? and verify
headers['content-md5'] = digest(body)
request.put(
uri: @url(path)
headers: headers
body: body
auth: @auth,
(err, resp, body) ->
if !err and resp.statusCode == 200
console.log 'success'
else
console.log 'error: ' + resp.statusCode + ' ' + body
)
)
upyun = ->
upyun.createClient = (bucket, auth, ssl, api_host) ->
new UpYun(bucket, auth, ssl, api_host)
module.exports = upyun
{
"name": "upyun-node",
"version": "0.0.0",
"description": "Feature complete UpYun REST API client",
"main": "index.js",
"dependencies": {
"request": "~2.21.0"
},
"devDependencies": {
"coffee-script": "~1.6.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"keywords": [
"rest",
"api",
"client",
"upyun",
"storage"
],
"author": "Kane Dou",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment