A Clojure library for qiniu.com storage that wraps qiniu java-sdk.
Leiningen dependency:
[clj.qiniu "0.1.1"]
require it in your namespace:
(require '[clj.qiniu :as qiniu])
(qiniu/set-config! :access-key "your qiniu access key" :secret-key "your qiniu secret key")
其他选项:
user-agent
: 请求的HTTP user agent值,默认Clojure/qiniu sdk
。throw-exception?
: 错误的时候是否抛出异常,默认 false,替代地返回{:ok false}
加上错误信息。
(qiniu/uptoken bucket)
(qiniu/uptoken bucket
:expires 3600
:scope scope
:callbackUrl "http://exmaple.com/callback"
:insertOnly 1
:detectMime 1)
更多选项直接看源码吧。
file
参数可以是任何可以通过clojure.java.io/input-stream
转成输入流的对象,比如 File 对象、URL、Java Resource等:
(qiniu/upload-bucket bucket key file)
结果:
{:ok true :status 200 :hash "xxxxx" :key "yyyyy" :response …… }
(qiniu/public-download-url domain key)
(qiniu/private-download-url domain key)
(qiniu/private-download-url domain key :expires (* 24 3600))
(qiniu/stat bucket key)
返回:
{:hash "xxxx" :putTime 14048865740735254 :mimeType "image/jpeg"
:status 200
:fsize 16098}
(qiniu/copy src-bucket src-key dest-bucket dest-key)
(qiniu/move src-bucket src-key dest-bucket dest-key)
返回:
{:ok true ……}
(qiniu/delete bucket key)
返回:
{:ok true ……}
使用with-batch
配合exec
搞定:
(use '[clj.qiniu :only [with-batch stat]])
(with-batch
(stat bucket key1)
(stat bucket key2)
(stat bucket key3)
(exec))
返回:
{:ok true :status 200
:results ({:fsize 123 :hash "xxxx" ……}
{:fsize 234 :hash "yyy" ……}
{:fsize 345 :hash "zzz" ……}
)}
批量拷贝、移动和删除文件也是类似:
(with-batch
(delete bucket key1)
(delete bucket key2)
(delete bucket key3)
(exec))
返回:
{:ok true :status 200
:results ({:ok true} {:ok true} {:ok true})}
但是请注意,with-batch
里的操作必须是同一种类型,不能混合。
获取图片信息和 EXIF 信息:
(image-info url)
(image-exif url)
获取图片缩略图:
(image-view url :width 100 :height 100 :mode 1 :format "png")
返回结果的:response
值就是缩略图的二进制数据,可以存储为文件或者输出到网页。
根据前缀prefix
获取 Bucket 内匹配的文件列表,bucket-file-seq
会返回一个LazySeq
:
(bucket-file-seq bucket "<prefix>" :limit 32)
limit设定批量查询大小,默认 32。
查询单月统计:
(bucket-monthly-stats bucket "201407")
查询某个时间范围内的空间、流量或者 API 调用统计:
(bucket-stats bucket "space" "20140701" "20140710")
(bucket-stats bucket "transfer" "20140701" "20140710")
(bucket-stats bucket "apicall" "20140701" "20140710")
创建 Bucket:
创建和删除 bucket:
(mk-bucket bucket)
(remove-bucket bucket)
发布到开放域名:
(publish-bucket bucket "http://example.qiniudn.com")
如果需要对已保存在空间中的资源进行云处理并将结果持久化,可以使用pfop
方法:
(pfop "clj-qiniu" (java.net.URLEncoder/encode "viva la vida.mp3")
"avthumb/m3u8/segtime/10/preset/audio_32k"
"http://example.com/persistentNotify"
:pipeline "dennis")
;; {:ok true, :results "544df4f97823de406816e673"}
返回的results
就是persistentId
,可以用来查询持久化处理状态:
(prefop-status "544df4f97823de406816e673")
Copyright © 2014 killme2008
Distributed under the Eclipse Public License version 1.0