diff --git a/README.md b/README.md index 673b6503f..dfa746d05 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Qiniu Logo](http://qiniutek.com/images/logo-2.png)](http://qiniu.com/) -此 Java SDK 适用于 Java 6 及以上版本,基于 [七牛云存储官方API](http://docs.qiniutek.com/v2/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 +此 Java SDK 适用于 Java 6 及以上版本,基于 [七牛云存储官方API](http://developer.qiniu.com/docs/v6/sdk/java-sdk.html) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 ## 安装 @@ -21,6 +21,7 @@ 其中最新版本的版本号可以到 https://github.com/qiniu/java-sdk/tags 这里查看。 +如果想直接下载依赖的jar,可以到 这个地址进行下载 [http://javasdk.qiniudn.com/dependencies.zip](http://javasdk.qiniudn.com/dependencies.zip) ## 使用 @@ -38,7 +39,7 @@ ## 许可证 -Copyright (c) 2012 qiniutek.com +Copyright (c) 2012-2014 qiniu.com 基于 MIT 协议发布: diff --git a/docs/README.md b/docs/README.md deleted file mode 100644 index 6fb56dbf9..000000000 --- a/docs/README.md +++ /dev/null @@ -1,730 +0,0 @@ ---- -title: Java SDK | 七牛云存储 ---- - -# Java SDK 使用指南 - -此SDK适用于Java 6及以上版本。基于 [七牛云存储官方API](http://docs.qiniu.com) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 - -SDK下载地址:[https://github.com/qiniu/java-sdk/tags](https://github.com/qiniu/java-sdk/tags) - -目录 ----- -- [环境准备](#env_preparation) -- [初始化](#setup) - - [配置密钥](#setup-key) -- [上传下载接口](#get-and-put-api) - - [上传流程](#io-put-flow) - - [生成上传授权uptoken](#make-uptoken) - - [上传代码](#upload-code) - - [断点续上传、分块并行上传](#resumable-io-put) - - [上传策略](#io-put-policy) - - [公有资源下载](#public-download) - - [私有资源下载](#private-download) -- [资源管理接口](#rs-api) - - [查看单个文件属性信息](#rs-stat) - - [复制单个文件](#rs-copy) - - [移动单个文件](#rs-move) - - [删除单个文件](#rs-delete) - - [批量操作](#batch) - - [批量获取文件属性信息](#batch-stat) - - [批量复制文件](#batch-copy) - - [批量移动文件](#batch-move) - - [批量删除文件](#batch-delete) -- [数据处理接口](#fop-api) - - [图像](#fop-image) - - [查看图像属性](#fop-image-info) - - [查看图片EXIF信息](#fop-exif) - - [生成图片预览](#fop-image-view) -- [高级资源管理接口](#rsf-api) - - [批量获得文件列表](#rsf-listPrefix) -- [贡献代码](#contribution) -- [许可证](#license) - ----- - - - -## 1. 环境准备 - -安装 Maven 的插件:[The Maven Integration for Eclipse](http://www.eclipse.org/m2e/) - -添加依赖 - - - com.qiniu - sdk - 6.0.0 - - - - -## 2.初始化 - - -### 2.1 配置密钥 - -要接入七牛云存储,您需要拥有一对有效的 Access Key 和 Secret Key 用来进行签名认证。可以通过如下步骤获得: - -1. [开通七牛开发者帐号](https://portal.qiniu.com/signup) -2. [登录七牛开发者自助平台,查看 Access Key 和 Secret Key](https://portal.qiniu.com/setting/key) 。 - -在获取到 Access Key 和 Secret Key 之后,您可以按照如下方式进行密钥配置: - -```{java} -import com.qiniu.api.config.Config; - -public class Init { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - } -} -``` - - - -## 3. 上传下载接口 - -为了尽可能地改善终端用户的上传体验,七牛云存储首创了客户端直传功能。 - -一般云存储的上传流程是: - - 客户端(终端用户) => 业务服务器 => 云存储服务 - -这样通过用户自己的业务服务器中转上传至云存储服务端。这种方式存在一些不足: - -1. 多了一次中转的上传过程,同数据存放在用户的业务服务器中相比,会相对慢一些; -1. 增加了用户业务服务器的负载,消耗了带宽,占用了磁盘,降低了服务能力; -1. 增加了用户的流量消耗,来自终端用户的上传数据进入业务服务器,然后再次上传至云存储服务,净增一倍流量。 - -因此,七牛云存储引入了客户端直传的模式,将整个上传过程调整为: - - 客户端(终端用户) => 七牛 => 业务服务器 - -客户端(终端用户)直接上传到七牛的服务器。通过DNS智能解析,七牛会选择到离终端用户最近的ISP服务商节点,速度会相比数据存放在用户自己的业务服务器上的方式更快。而且,七牛云存储可以在用户文件上传成功以后,替用户的客户端向用户的业务服务器发送反馈信息,减少用户的客户端同业务服务器之间的交互。详情请参考[上传策略](#io-put-policy) - -**注意**:如果您只是想要将您电脑上,或者是服务器上的文件上传到七牛云存储,可以直接使用七牛提供的 [qrsync](http://docs.qiniu.com/tools/qrsync.html) 上传工具,而无需额外开发。 - -文件上传有两种方式:普通方式,即一次性上传整个文件;断点续上传,即将文件分割成若干小块,分别上传,然后在七牛云存储服务端重新合并成一个文件。一般情况下,用户可以采用普通上传。如果文件较大,或者网络条件不佳,那么可以使用断点续上传,提高上传的速度和成功率。 - - - - -### 3.1 上传流程 - -在七牛云存储中,整个上传流程大体分为这样几步: - -1. 业务服务器颁发 [uptoken(上传授权凭证)](#make-uptoken)给客户端(终端用户) -1. 客户端凭借 [uptoken](#make-uptoken) 上传文件到七牛 -1. 在七牛获得完整数据后,根据用户请求的设定执行以下操作: - - a. 如果用户设定了[returnUrl](#io-put-policy),七牛云存储将反馈一个指向returnUrl的HTTP 301,驱动客户端执行跳转; - - b. 如果用户设定了[callbackUrl](#io-put-policy),七牛云存储将向callbackUrl指定的地址发起一个HTTP 请求回调业务服务器,同时向业务服务器发送数据。发送的数据内容由[callbackBody](#io-put-policy)指定。业务服务器完成回调的处理后,可以在HTTP Response中放入数据,七牛云存储会响应客户端,并将业务服务器反馈的数据发送给客户端; - - c. 如果两者都没有设置,七牛云存储根据[returnBody](#io-put-policy)的设定向客户端发送反馈信息。 - -需要注意的是,回调到业务服务器的过程是可选的,它取决于业务服务器颁发的 [uptoken](#make-uptoken)。如果没有回调,七牛会返回一些标准的信息(比如文件的 hash)给客户端。如果上传发生在业务服务器,以上流程可以自然简化为: - -1. 业务服务器生成 uptoken(不设置回调,自己回调到自己这里没有意义) -1. 凭借 [uptoken](#make-uptoken) 上传文件到七牛 -1. 善后工作,比如保存相关的一些信息 - - - - -### 3.2 生成上传授权uptoken - -uptoken是一个字符串,作为http协议Header的一部分(Authorization字段)发送到我们七牛的服务端,表示这个http请求是经过用户授权的。 - -```{java} -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.PutPolicy; - -public class Uptoken { - - public static void main(String[] args) throws Exception { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - // 请确保该bucket已经存在 - String bucketName = "Your bucket name"; - PutPolicy putPolicy = new PutPolicy(bucketName); - String uptoken = putPolicy.token(mac); - } -} - -``` - - - -### 3.3 上传代码 - -上传本地文件。如果用户从自己的计算机或服务器上传文件,可以直接使用七牛云存储提供的[qrsync](http://docs.qiniu.com/tools/qrsync.html)工具。用户也可以自行编写上传程序。 - -上传程序大体步骤如下: - -1. 设置AccessKey和SecretKey; -1. 创建Mac对象; -1. 创建PutPolicy对象; -1. 生成UploadToken; -1. 创建PutExtra对象; -1. 调用put或putFile方法上传文件; - -具体代码如下: - -```{java} -import java.io.File; - -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.io.IoApi; -import com.qiniu.api.io.PutExtra; -import com.qiniu.api.io.PutRet; -import com.qiniu.api.rs.PutPolicy; - -public class UploadFile { - - public static void main(String[] args) throws Exception { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - // 请确保该bucket已经存在 - String bucketName = ""; - PutPolicy putPolicy = new PutPolicy(bucketName); - String uptoken = putPolicy.token(mac); - PutExtra extra = new PutExtra(); - String key = ""; - String localFile = ""; - PutRet ret = IoApi.putFile(uptoken, key, localFile, extra); - } -} - -``` - - - -### 3.4 断点续上传、分块并行上传 - -与普通上传类似: -```{java} - private void uploadFile() throws AuthException, JSONException{ - PutPolicy p = new PutPolicy(bucketName); - p.returnBody = "{\"key\": $(key), \"hash\": $(etag),\"mimeType\": $(mimeType)}"; - String upToken = p.token(mac); - PutRet ret = ResumeableIoApi.put(file, upToken, key, mimeType); - } - - private void uploadStream() throws AuthException, JSONException, FileNotFoundException{ - PutPolicy p = new PutPolicy(bucketName); - String upToken = p.token(mac); - FileInputStream fis = new FileInputStream(file); - PutRet ret = ResumeableIoApi.put(fis, upToken, key, mimeType); - } - -``` -key,mimeType 可为null。 - - - -### 3.5 上传策略 - -[uptoken](http://docs.qiniu.com/api/put.html#uploadToken) 实际上是用 AccessKey/SecretKey 进行数字签名的上传策略(`rs.PutPolicy`),它控制则整个上传流程的行为。让我们快速过一遍你都能够决策啥: - -* `expires` 指定 [uptoken](http://docs.qiniu.com/api/put.html#uploadToken) 有效时长。单位:秒(s),默认1小时,3600秒。deadline = System.currentTimeMillis() / 1000 + this.expires,不直接指定deadline。一个 [uptoken](http://docs.qiniu.com/api/put.html#uploadToken) 可以被用于多次上传(只要它还没有过期)。 - -关于上传策略更完整的说明,请参考 [uptoken](http://docs.qiniu.com/api/put.html#uploadToken)。 - -### 3.6 文件下载 - -七牛云存储上的资源下载分为 公有资源下载 和 私有资源下载 。 - -私有(private)是 Bucket(空间)的一个属性,一个私有 Bucket 中的资源为私有资源,私有资源不可匿名下载。 - -新创建的空间(Bucket)缺省为私有,也可以将某个 Bucket 设为公有,公有 Bucket 中的资源为公有资源,公有资源可以匿名下载。 - - - -### 3.7 公有资源下载 - -如果在给bucket绑定了域名的话,可以通过以下地址访问。 - - [GET] http:/// - -其中\是bucket所对应的域名。七牛云存储为每一个bucket提供一个默认域名。默认域名可以到[七牛云存储开发者平台](https://portal.qiniu.com/)中,空间设置的域名设置一节查询。用户也可以将自有的域名绑定到bucket上,通过自有域名访问七牛云存储。 - -**注意: key必须采用utf8编码,如使用非utf8编码访问七牛云存储将反馈错误** - - - -### 3.8 私有资源下载 - -私有资源必须通过临时下载授权凭证(downloadToken)下载,如下: - - [GET] http:///?token= - -注意,尖括号不是必需,代表替换项。 - -`downloadToken` 可以使用 SDK 提供的如下方法生成: - -```{java} -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.GetPolicy; -import com.qiniu.api.rs.URLUtils; - -public class DownloadFile { - - public static void main(String[] args) throws Exception { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - String baseUrl = URLUtils.makeBaseUrl("", ""); - GetPolicy getPolicy = new GetPolicy(); - String downloadUrl = getPolicy.makeRequest(baseUrl, mac); - } -} -``` - - - -## 4. 资源管理接口 - -文件管理包括对存储在七牛云存储上的文件进行查看、复制、移动和删除处理。 - - - -### 4.1 查看单个文件属性信息 - -```{java} -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.Entry; -import com.qiniu.api.rs.RSClient; - -public class Stat { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - RSClient client = new RSClient(mac); - Entry statRet = client.stat("", ""); - } -} -``` - - - - -### 4.2 复制单个文件 - -```{java} -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.RSClient; - -public class Copy { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - RSClient client = new RSClient(mac); - client.copy("", "", "", ""); - } -} -``` - - - -### 4.3 移动单个文件 - -```{java} -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.RSClient; - -public class Move { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - RSClient client = new RSClient(mac); - client.move("", "", "", ""); - } -} - -``` - - - -### 4.4 删除单个文件 - -```{java} -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.RSClient; - -public class Delete { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - RSClient client = new RSClient(mac); - client.delete("", ""); - } -} - -``` - - - - -### 4.5 批量操作 - -当您需要一次性进行多个操作时, 可以使用批量操作. - - - -#### 4.5.1 批量获取文件属性信息 - -```{java} - -import java.util.ArrayList; -import java.util.List; - -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.BatchStatRet; -import com.qiniu.api.rs.EntryPath; -import com.qiniu.api.rs.RSClient; - -public class BatchStat { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - - RSClient rs = new RSClient(mac); - List entries = new ArrayList(); - - EntryPath e1 = new EntryPath(); - e1.bucket = ""; - e1.key = ""; - entries.add(e1); - - EntryPath e2 = new EntryPath(); - e2.bucket = ""; - e2.key = ""; - entries.add(e2); - - BatchStatRet bsRet = rs.batchStat(entries); - } -} -``` - - - - -#### 4.5.2 批量复制文件 - -```{java} - -import java.util.ArrayList; -import java.util.List; - -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.BatchCallRet; -import com.qiniu.api.rs.EntryPath; -import com.qiniu.api.rs.EntryPathPair; -import com.qiniu.api.rs.RSClient; - -public class BatchCopy { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - RSClient rs = new RSClient(mac); - List entries = new ArrayList(); - - EntryPathPair pair1 = new EntryPathPair(); - - EntryPath src = new EntryPath(); - src.bucket = ""; - src.key = ""; - - EntryPath dest = new EntryPath(); - dest.bucket = ""; - dest.key = ""; - - pair1.src = src; - pair1.dest = dest; - - EntryPathPair pair2 = new EntryPathPair(); - - EntryPath src2 = new EntryPath(); - src2.bucket = ""; - src2.key = ""; - - EntryPath dest2 = new EntryPath(); - dest2.bucket = ""; - dest2.key = ""; - - pair2.src = src2; - pair2.dest = dest2; - - entries.add(pair1); - entries.add(pair2); - - BatchCallRet ret = rs.batchCopy(entries); - } -} -``` - - - -#### 4.5.3 批量移动文件 - -```{java} - -import java.util.ArrayList; -import java.util.List; - -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.BatchCallRet; -import com.qiniu.api.rs.EntryPath; -import com.qiniu.api.rs.EntryPathPair; -import com.qiniu.api.rs.RSClient; - -public class BatchMove { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - RSClient rs = new RSClient(mac); - List entries = new ArrayList(); - - EntryPathPair pair1 = new EntryPathPair(); - - EntryPath src = new EntryPath(); - src.bucket = ""; - src.key = ""; - - EntryPath dest = new EntryPath(); - dest.bucket = ""; - dest.key = ""; - - pair1.src = src; - pair1.dest = dest; - - EntryPathPair pair2 = new EntryPathPair(); - - EntryPath src2 = new EntryPath(); - src2.bucket = ""; - src2.key = ""; - - EntryPath dest2 = new EntryPath(); - dest2.bucket = ""; - dest2.key = ""; - - pair2.src = src2; - pair2.dest = dest2; - - entries.add(pair1); - entries.add(pair2); - - BatchCallRet ret = rs.batchMove(entries); - } -} -``` - - - -#### 4.5.4 批量删除文件 - -```{java} -import java.util.ArrayList; -import java.util.List; - -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rs.BatchCallRet; -import com.qiniu.api.rs.EntryPath; -import com.qiniu.api.rs.RSClient; - -public class BatchDelete { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - RSClient rs = new RSClient(mac); - List entries = new ArrayList(); - - EntryPath e1 = new EntryPath(); - e1.bucket = ""; - e1.key = ""; - entries.add(e1); - - EntryPath e2 = new EntryPath(); - e2.bucket = ""; - e2.key = ""; - entries.add(e2); - - BatchCallRet bret = rs.batchDelete(entries); - } -} - -``` - -参阅: `rs.EntryPath`, `rs.Client.BatchDelete` - - - -#### 4.5.5 高级批量操作 - -批量操作不仅仅支持同时进行多个相同类型的操作, 同时也支持不同的操作. - -```{java} -to do! -``` - - - -## 5. 数据处理接口 - -七牛支持在云端对图像, 视频, 音频等富媒体进行个性化处理 - - - -### 5.1 图像 - - - -### 5.1.1 查看图像属性 - -```{java} -import com.qiniu.api.fop.ImageInfo; -import com.qiniu.api.fop.ImageInfoRet; - -public class FopImageInfo { - - public static void main(String[] args) { - String url = "" + "/" + ""; - ImageInfoRet ret = ImageInfo.call(url); - } -} -``` - -参阅: `fop.ImageInfoRet`, `fop.ImageInfo` - - - -### 5.1.2 查看图片EXIF信息 - -```{java} -import com.qiniu.api.fop.ExifRet; -import com.qiniu.api.fop.ImageExif; - -public class FopImageExif { - - public static void main(String[] args) { - String url = "" + "/" + ""; - ExifRet ret = ImageExif.call(url); - } -} -``` - - - -### 5.1.3 生成图片预览 - -```{java} -import com.qiniu.api.fop.ImageView; -import com.qiniu.api.net.CallRet; - -public class FopImageView { - - public static void main(String[] args) { - String url = "http://domain/key"; - ImageView iv = new ImageView(); - iv.mode = 1 ; - iv.width = 100 ; - iv.height = 200 ; - iv.quality = 1 ; - iv.format = "jpg" ; - CallRet ret = iv.call(url); - } -} -``` - - - -## 6. 高级资源管理接口(rsf) - - - -批量获取文件列表 - -```{java} -import com.qiniu.api.auth.digest.Mac; -import com.qiniu.api.config.Config; -import com.qiniu.api.rsf.RSFClient; - -public class ListPrefix { - - public static void main(String[] args) { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); - - RSFClient client = new RSFClient(mac); - String marker = ""; - - List all = new ArrayList(); - ListPrefixRet ret = null; - while (true) { - ret = client.listPrifix(bucketName, "", marker, 10); - marker = ret.marker; - all.addAll(ret.results); - if (!ret.ok()) { - // no more items or error occurs - break; - } - } - if (ret.exception.getClass() != RSFEofException.class) { - // error handler - } - } -} -``` - - -## 7. 贡献代码 - -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 - - -## 8. 许可证 - -Copyright (c) 2013 qiniu.com - -基于 MIT 协议发布: - -* [www.opensource.org/licenses/MIT](http://www.opensource.org/licenses/MIT) - diff --git a/src/main/java/com/qiniu/api/config/Config.java b/src/main/java/com/qiniu/api/config/Config.java index 6567fc186..ce4f52142 100644 --- a/src/main/java/com/qiniu/api/config/Config.java +++ b/src/main/java/com/qiniu/api/config/Config.java @@ -9,18 +9,10 @@ public class Config { /**本地检测不通过、程序抛异常,设置 CallRet 的 statusCode 为此错误码*/ public static final int ERROR_CODE = 0; - public static final String VERSION = "6.1.5"; + public static final String VERSION = "6.1.8"; - /** - * You can get your accesskey from https://dev.qiniutek.com - */ public static String ACCESS_KEY = ""; - /** - * You can get your accesskey from https://dev.qiniutek.com - */ public static String SECRET_KEY = ""; public static String RS_HOST = "http://rs.qbox.me"; diff --git a/src/main/java/com/qiniu/api/io/IoApi.java b/src/main/java/com/qiniu/api/io/IoApi.java index 31273450d..1939f2025 100644 --- a/src/main/java/com/qiniu/api/io/IoApi.java +++ b/src/main/java/com/qiniu/api/io/IoApi.java @@ -10,6 +10,7 @@ import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; +import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.AbstractContentBody; import org.apache.http.entity.mime.content.FileBody; @@ -35,7 +36,7 @@ private static PutRet put(String uptoken, String key, File file, "File does not exist or not readable."))); } extra = extra == null ? new PutExtra() : extra; - MultipartEntity requestEntity = new MultipartEntity(); + MultipartEntity requestEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(Config.CHARSET)); try { requestEntity.addPart("token", new StringBody(uptoken)); AbstractContentBody fileBody = buildFileBody(file, extra); @@ -90,7 +91,7 @@ private static void setParam(MultipartEntity requestEntity, Map private static PutRet putStream(String uptoken, String key, InputStream reader, PutExtra extra, long length) { extra = extra == null ? new PutExtra() : extra; - MultipartEntity requestEntity = new MultipartEntity(); + MultipartEntity requestEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(Config.CHARSET)); try { requestEntity.addPart("token", new StringBody(uptoken)); String fileName = key != null ? key : "null"; diff --git a/src/main/java/com/qiniu/api/rs/PutPolicy.java b/src/main/java/com/qiniu/api/rs/PutPolicy.java index 1ae64c192..9a1147d19 100644 --- a/src/main/java/com/qiniu/api/rs/PutPolicy.java +++ b/src/main/java/com/qiniu/api/rs/PutPolicy.java @@ -28,8 +28,6 @@ public class PutPolicy { /** 可选 */ public String returnBody; /** 可选 */ - public String asyncOps; - /** 可选 */ public String endUser; /** 可选 */ public long expires; @@ -51,7 +49,7 @@ public class PutPolicy { public String persistentOps; private long deadline; - + /** * 转码队列名,须预先开通 * 资源上传成功后,触发转码时指定独立的队列进行转码 @@ -79,9 +77,6 @@ public String marshal() throws JSONException { if (this.returnBody != null && this.returnBody.length() > 0) { stringer.key("returnBody").value(this.returnBody); } - if (this.asyncOps != null && this.asyncOps.length() > 0) { - stringer.key("asyncOps").value(this.asyncOps); - } if (this.saveKey != null && this.saveKey.length() > 0) { stringer.key("saveKey").value(this.saveKey); } @@ -109,7 +104,7 @@ public String marshal() throws JSONException { if(persistentPipeline != null && persistentPipeline.trim().length() > 0){ stringer.key("persistentPipeline").value(this.persistentPipeline); } - + stringer.key("deadline").value(this.deadline); stringer.endObject(); diff --git a/src/test/java/README.md b/src/test/java/README.md index bca485f46..60e310173 100644 --- a/src/test/java/README.md +++ b/src/test/java/README.md @@ -1,8 +1,8 @@ -# Qiniu Resource (Cloud) Storage SDK demo for Java +# Qiniu Resource (Cloud) Storage SDK demo for Java # 关于 这些demo都是基于 Qiniu Resource (Cloud) Storage SDK for Java 开发的。其中RSDemo.java演示了服务端进行资源上传,下载,删除,发布,取消发布等功能的演示。 -其中Notifier.java 和 UpDemo.java 演示了客户端进行断点续上传。 +其中Notifier.java 和 UpDemo.java 演示了客户端进行断点续上传。 在ResumableNotifier.java和ResumablePutDemo.java中对客户端在断点续上传过程中的状态进行了序列化,使客户在上传过程中即使程序崩溃或者退出在下次重新启动时还可以接着上次继续上次。 ResumableGUINotifier.java 和 ResumableGUIputDemo.java 则添加了客户在断点续上传时的UI,使上传更加直观。 @@ -13,15 +13,15 @@ ## 使用 要接入七牛云存储,您需要拥有一对有效的 Access Key 和 Secret Key 用来进行签名认证。可以通过如下步骤获得: -[开通七牛开发者账号](https://dev.qiniutek.com/signup) -[登录七牛开发者自助平台,查看Access Key 和 Secret Key](https://dev.qiniutek.com/account/keys) +[开通七牛开发者账号](https://portal.qiniu.com/signup) +[登录七牛开发者自助平台,查看Access Key 和 Secret Key](https://portal.qiniu.com/setting/key) 然后把得到的Access Key 和 Secret Key 写入每个demo中后。 1.RSDemo可以直接运行。 2.ResumablePutDemo 和 ResumableGUIPutDemo 则需要传入 要上传文件的具体路径加文件名 作为参数。 在eclipse中您可以这样添加:选中您要运行的demo-->右键-->选中 Build Path-->Run/Debug Settings-->双击你要运行的demo-->选中 ‘(x)=Arguments’ 选项卡-->在Program arguments中加入要传入的参数。 在命令窗口下:”java 您要运行的demo 参数” 把参数改成你的实际参数。 (你也可以在demo中把 String inputFile = args[0];中的args[0]换成你的参数) -参考文档:[七牛云存储 Java SDK 使用指南](http://docs.qiniutek.com/v2/sdk/java/) +参考文档:[七牛云存储 Java SDK 使用指南](http://developer.qiniu.com/docs/v6/sdk/java-sdk.html) ## 贡献代码 @@ -33,7 +33,7 @@ ## 许可证 -Copyright (c) 2012 qiniutek.com +Copyright (c) 2012-2014 qiniu.com 基于 MIT 协议发布: diff --git a/src/test/java/com/qiniu/testing/IOTest.java b/src/test/java/com/qiniu/testing/IOTest.java index 007f9087f..5c2c1a37f 100644 --- a/src/test/java/com/qiniu/testing/IOTest.java +++ b/src/test/java/com/qiniu/testing/IOTest.java @@ -106,53 +106,53 @@ private String getJsonValue(JSONObject jsonObject, String name){ return null; } } - + public void testNoLengthStream() throws Exception { PutPolicy p = new PutPolicy(bucketName); p.returnBody = "{\"key\": $(key), \"hash\": $(etag),\"mimeType\": $(mimeType)}"; String upToken = p.token(mac); - - HttpEntity en = getHttpEntity("http://qiniuphotos.qiniudn.com/gogopher.jpg"); - + + HttpEntity en = getHttpEntity("http://testres.qiniudn.com/gogopher.jpg"); + class MyInputStream extends InputStream{ InputStream in; MyInputStream(InputStream is){ this.in = is; } - + @Override public int read() throws IOException { // TODO Auto-generated method stub return in.read(); } - + public int available() throws IOException { throw new IOException(); } - + public void close() throws IOException { in.close(); } - + } - - + + PutRet ret = IoApi.Put(upToken, key, new MyInputStream(en.getContent()), null); - + assertTrue(ret.ok()); } - + public void testSetLengthStream() throws Exception { PutPolicy p = new PutPolicy(bucketName); p.returnBody = "{\"key\": $(key), \"hash\": $(etag),\"mimeType\": $(mimeType)}"; String upToken = p.token(mac); - - HttpEntity en = getHttpEntity("http://qiniuphotos.qiniudn.com/gogopher.jpg"); + + HttpEntity en = getHttpEntity("http://testres.qiniudn.com/gogopher.jpg"); PutExtra extra = new PutExtra(); extra.mimeType = en.getContentType().getValue(); PutRet ret = IoApi.Put(upToken, key, en.getContent(), extra, en.getContentLength()); - + assertTrue(ret.ok()); } diff --git a/src/test/java/com/qiniu/testing/RSFTest.java b/src/test/java/com/qiniu/testing/RSFTest.java index 872b5717d..dda6e56b1 100644 --- a/src/test/java/com/qiniu/testing/RSFTest.java +++ b/src/test/java/com/qiniu/testing/RSFTest.java @@ -25,7 +25,7 @@ public class RSFTest extends TestCase { public final String expectedHash = "FmDZwqadA4-ib_15hYfQpb7UXUYR"; public String bucketName; - + public Mac mac; @Override public void setUp() { @@ -41,7 +41,6 @@ public void setUp() { mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY); } - // just upload an image in testdata. public void testRSF() throws Exception { { String uptoken = new PutPolicy(bucketName).token(mac); @@ -56,14 +55,14 @@ public void testRSF() throws Exception { assertTrue(ret.ok()); assertTrue(expectedHash.equals(ret.getHash())); } - + } // we don't checkout the result of how may items are in the buckets. // not very convient, it's better, although. { RSFClient client = new RSFClient(mac); String marker = ""; - + List all = new ArrayList(); ListPrefixRet ret = null; while (true) { @@ -77,14 +76,9 @@ public void testRSF() throws Exception { } if (ret.exception.getClass() != RSFEofException.class) { // error handler - } - + } + assertTrue(all.size() >= 3); } } - - @Override - public void tearDown() { - // do nothing here. - } -} \ No newline at end of file +} diff --git a/src/test/java/com/qiniu/testing/ResumeableioTest.java b/src/test/java/com/qiniu/testing/ResumeableioTest.java index 4081ddfc9..b9851db35 100644 --- a/src/test/java/com/qiniu/testing/ResumeableioTest.java +++ b/src/test/java/com/qiniu/testing/ResumeableioTest.java @@ -112,15 +112,15 @@ private void uploadStream() throws AuthException, JSONException, FileNotFoundExc assertEquals(mimeType, mt); } } - + public void testStream() throws Exception { PutPolicy p = new PutPolicy(bucketName); p.returnBody = "{\"key\": $(key), \"hash\": $(etag),\"mimeType\": $(mimeType)}"; String upToken = p.token(mac); - - HttpEntity en = getHttpEntity("http://qiniuphotos.qiniudn.com/gogopher.jpg"); + + HttpEntity en = getHttpEntity("http://testres.qiniudn.com/gogopher.jpg"); PutRet ret = ResumeableIoApi.put(en.getContent(), upToken, currentKey, en.getContentType().getValue(), en.getContentLength()); - + assertTrue(ret.ok()); } diff --git a/src/test/java/com/qiniu/testing/UtilTest.java b/src/test/java/com/qiniu/testing/UtilTest.java index 86069aaf1..9db81f507 100644 --- a/src/test/java/com/qiniu/testing/UtilTest.java +++ b/src/test/java/com/qiniu/testing/UtilTest.java @@ -6,8 +6,6 @@ import com.qiniu.api.net.EncodeUtils; public class UtilTest extends TestCase { - - // just upload an image in testdata. public void test() throws Exception { String expectString = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+}{:?><-=,./;'[]"; String encodedString = "MTIzNDU2Nzg5MGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVp-IUAjJCVeJiooKV8rfXs6Pz48LT0sLi87J1td"; @@ -15,9 +13,4 @@ public void test() throws Exception { String decoded = new String(rawBytes); assertTrue(expectString.equals(decoded)); } - - @Override - public void tearDown() { - // do nothing here. - } }