Skip to content

Clojure SDK for qiniu. Clojure 七牛云存储 SDK。

License

Notifications You must be signed in to change notification settings

leancloud/clj.qiniu

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clj.qiniu Build Status

A Clojure library for qiniu.com storage that wraps qiniu java-sdk.

alt test coverage

Usage

Leiningen dependency:

[cn.leancloud/clj.qiniu "0.2.2"]

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}加上错误信息。
  • num-http-threads: clj-http 使用的连接池线程数,默认10。

生成uptoken

(qiniu/uptoken bucket)
(qiniu/uptoken bucket
    :expires 3600
    :key "photo/my.jpg" ;; limit scope to key or a prefix
    :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值就是缩略图的二进制数据,可以存储为文件或者输出到网页。

批量获取 Bucket 下的文件

根据前缀prefix获取 Bucket 内匹配的文件列表,bucket-file-seq会返回一个LazySeq

(bucket-file-seq  bucket "<prefix>" :limit 32)

limit设定批量查询大小,默认 32。

Bucket 统计

查询单月统计:

(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:

创建和删除 bucket:

(mk-bucket bucket)
(remove-bucket bucket)

发布到开放域名:

(publish-bucket bucket "http://example.qiniudn.com")

将 bucket 私有或者设置为公开:

(private-bucket bucket true)
(private-bucket bucket false)

持久化处理(音频视频)

如果需要对已保存在空间中的资源进行云处理并将结果持久化,可以使用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")

获取 CDN 域名

(domain-list bucket)

刷新 CDN 缓存(可能需要申请相关权限):

(refresh-bucket-cdn urls dirs)

绑定自定义域名(可能要申请相关权限):

(set-custom-domain! domain bucket cret key platform geo protocol)
;;; geo "chain" or "global"
;;; protocol "http" or "https"

上下线自定义域名:

(online-custom-domain domain)
(offline-custom-domain domain)

删除自定义域名:

(delete-custom-domain domain)

贡献者

License

Copyright © 2014 killme2008

Distributed under the Eclipse Public License version 1.0

About

Clojure SDK for qiniu. Clojure 七牛云存储 SDK。

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Clojure 100.0%