diff --git a/src/main/java/com/qiniu/common/Region.java b/src/main/java/com/qiniu/common/Region.java index 3c983eff8..d283f68b6 100644 --- a/src/main/java/com/qiniu/common/Region.java +++ b/src/main/java/com/qiniu/common/Region.java @@ -58,7 +58,7 @@ public String getApiHost() { /** * 域名构造器 */ - static class Builder { + public static class Builder { protected Region region; public Builder() { diff --git a/src/main/java/com/qiniu/storage/UploadManager.java b/src/main/java/com/qiniu/storage/UploadManager.java index 41a5b9041..c8911fba3 100644 --- a/src/main/java/com/qiniu/storage/UploadManager.java +++ b/src/main/java/com/qiniu/storage/UploadManager.java @@ -3,6 +3,7 @@ import com.qiniu.common.QiniuException; import com.qiniu.http.Client; import com.qiniu.http.Response; +import com.qiniu.util.IOUtils; import com.qiniu.util.StringMap; import java.io.File; @@ -86,6 +87,28 @@ public void accept(String key, Object value) { }); return ret; } + + /** + * 上传字节流,小文件走表单,大文件走分片 + * @param inputStream + * @param size + * @param key + * @param token + * @param params + * @param mime + * @param checkCrc + * @return + * @throws QiniuException + * @throws IOException + */ + public Response put(InputStream inputStream, long size, String key, String token, StringMap params, + String mime, boolean checkCrc) throws QiniuException, IOException { + if (size < 0 || size > configuration.putThreshold) { + return put(inputStream, key, token, params, mime); + } + byte[] data = IOUtils.toByteArray(inputStream); + return put(data, key, token, params, mime, checkCrc); + } /** * 上传字节数组 diff --git a/src/main/java/com/qiniu/util/IOUtils.java b/src/main/java/com/qiniu/util/IOUtils.java new file mode 100644 index 000000000..e56fd2248 --- /dev/null +++ b/src/main/java/com/qiniu/util/IOUtils.java @@ -0,0 +1,29 @@ +package com.qiniu.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ByteArrayOutputStream; + +public class IOUtils { + + private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; + + /** + * 输入InputSteam,返回byte[]. + * 参考:https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/IOUtils.java
+ * @param input + * @return + * @throws IOException + */ + public static byte[] toByteArray(final InputStream input) throws IOException { + try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) { + byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; + int n; + while (-1 != (n = input.read(buffer))) { + output.write(buffer, 0, n); + } + return output.toByteArray(); + } + } + +} diff --git a/src/test/java/test/com/qiniu/TestConfig.java b/src/test/java/test/com/qiniu/TestConfig.java index f81f4c8eb..fd59b7692 100644 --- a/src/test/java/test/com/qiniu/TestConfig.java +++ b/src/test/java/test/com/qiniu/TestConfig.java @@ -2,6 +2,8 @@ import com.qiniu.util.Auth; +import test.com.qiniu.storage.FormUploadTest; + public final class TestConfig { //dummy: ak, sk, ... @@ -45,6 +47,8 @@ public static boolean isTravis() { public static void main(String[] args) { try { + FormUploadTest t = new FormUploadTest(); + t.testFormUploadWithInputStream(); System.out.println("done"); } catch (Exception e) { e.printStackTrace(); diff --git a/src/test/java/test/com/qiniu/storage/FormUploadTest.java b/src/test/java/test/com/qiniu/storage/FormUploadTest.java index 42cbc09b5..595f0cb7a 100644 --- a/src/test/java/test/com/qiniu/storage/FormUploadTest.java +++ b/src/test/java/test/com/qiniu/storage/FormUploadTest.java @@ -14,6 +14,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; @@ -450,6 +451,47 @@ public void testFormLargeSize2() { } } } + + /** + * 测试inputStream 表单上传 + * 检测reqid是否为Null + * 检测状态码是否为200 + */ + @Test + public void testFormUploadWithInputStream() { + testFormUploadWithInputStream(1, -1); + testFormUploadWithInputStream(1, 0); + testFormUploadWithInputStream(1, 1000); + testFormUploadWithInputStream(4 * 1024, 4 * 1024 * 1024); + testFormUploadWithInputStream(5 * 1024, -1); + testFormUploadWithInputStream(5 * 1024, 5 * 1024 * 1024); + } + + /** + * 测试inputStream 表单上传 + * 检测reqid是否为Null + * 检测状态码是否为200 + */ + public void testFormUploadWithInputStream(long kiloSize, long size) { + + String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_z0, TestConfig.testBucket_z0, 3600, null); + System.out.println("token="+token); + + try { + File file = TempFile.createFile(kiloSize); + InputStream inputStream = new FileInputStream(file); + System.out.println("length=" + file.length()); + System.out.println("size=" + size); + Response response = uploadManager.put(inputStream, size, TestConfig.testBucket_z0, token, null, null, false); + System.out.println("code="+response.statusCode); + System.out.println("reqid="+response.reqId); + System.out.println(response.bodyString()); + assertNotNull(response.reqId); + assertEquals(200, response.statusCode); + } catch (Exception e) { + e.printStackTrace(); + } + } class MyRet { public String hash; diff --git a/src/test/java/test/com/qiniu/streaming/StreamingTest.java b/src/test/java/test/com/qiniu/streaming/StreamingTest.java index 25ba03a6c..e3b49cac1 100644 --- a/src/test/java/test/com/qiniu/streaming/StreamingTest.java +++ b/src/test/java/test/com/qiniu/streaming/StreamingTest.java @@ -1,14 +1,11 @@ package test.com.qiniu.streaming; -import com.google.gson.JsonSyntaxException; import com.qiniu.common.QiniuException; import com.qiniu.streaming.StreamingManager; import com.qiniu.streaming.model.ActivityRecords; import com.qiniu.streaming.model.StreamAttribute; import com.qiniu.streaming.model.StreamListing; -import com.qiniu.streaming.model.StreamStatus; import com.qiniu.util.Auth; -import com.qiniu.util.Json; import org.junit.Test; import test.com.qiniu.TestConfig; @@ -16,82 +13,85 @@ /** * Created by bailong on 16/9/22 + * Updated by panyuan on 19/3/12 */ public class StreamingTest { + private Auth auth = TestConfig.testAuth; - private String hub = "pilisdktest"; - private String streamKeyPrefix = "pilijava" + System.currentTimeMillis(); + private String stream = "javasdk"; + private String streamNoExist = "javasdk" + "NoExist"; + private String streamKeyPrefix = "javasdk"; private StreamingManager manager = new StreamingManager(auth, hub); - - //@Test + /** + * 测试获取不存在的流的信息 + * 检测返回状态码是否是612 + */ + @Test public void testGetNoExistStream() { try { - manager.attribute("nnnoexist"); + manager.attribute(streamNoExist); fail("should not exist"); } catch (QiniuException e) { - e.printStackTrace(); assertEquals(612, e.code()); } } - // CHECKSTYLE:OFF - //@Test + /** + * 测试创建、禁用、启用、获取流信息、列举 + * @throws QiniuException + */ + @Test public void testStreamOperation() throws QiniuException { - // CHECKSTYLE:ON - String streamKey = streamKeyPrefix + "-a"; - - manager.create(streamKey); - - StreamAttribute attr = manager.attribute(streamKey); + StreamAttribute attr = manager.attribute(stream); assertEquals(0, attr.disabledTill); assertNotEquals(0, attr.createdAt); try { - manager.create(streamKey); + manager.create(stream); fail("has already existed"); } catch (QiniuException e) { assertEquals(614, e.code()); } - manager.disableTill(streamKey, -1); + manager.disableTill(stream, -1); - attr = manager.attribute(streamKey); + attr = manager.attribute(stream); assertEquals(-1, attr.disabledTill); assertNotEquals(0, attr.updatedAt); - manager.enable(streamKey); - attr = manager.attribute(streamKey); + manager.enable(stream); + attr = manager.attribute(stream); assertEquals(0, attr.disabledTill); assertNotEquals(0, attr.updatedAt); long t = System.currentTimeMillis() / 1000 + 3600; - manager.disableTill(streamKey, t); - attr = manager.attribute(streamKey); + manager.disableTill(stream, t); + attr = manager.attribute(stream); assertEquals(t, attr.disabledTill); assertNotEquals(0, attr.updatedAt); - manager.enable(streamKey); - attr = manager.attribute(streamKey); + manager.enable(stream); + attr = manager.attribute(stream); assertEquals(0, attr.disabledTill); assertNotEquals(0, attr.updatedAt); try { - StreamStatus status = manager.status(streamKey); + manager.status(stream); fail(); } catch (QiniuException e) { assertEquals(619, e.code()); } try { - manager.saveAs(streamKey, null, 0, 0); + manager.saveAs(stream, null, 0, 0); fail(); } catch (QiniuException e) { assertEquals(619, e.code()); } - ActivityRecords records = manager.history(streamKey, System.currentTimeMillis() / 1000 - 1000, 0); + ActivityRecords records = manager.history(stream, System.currentTimeMillis() / 1000 - 1000, 0); assertEquals(0, records.items.length); StreamListing l = manager.listStreams(false, streamKeyPrefix, null); @@ -118,34 +118,31 @@ public void testStreamOperation() throws QiniuException { assertFalse(it.hasNext()); } - //@Test + /** + * 测试saveas + * 检测返回状态码是否是404 + * @throws QiniuException + */ + @Test public void testSaveAs() throws QiniuException { try { - manager.saveAs("test--sd", "f\"ff.m3u8"); + manager.saveAs(streamNoExist, "f\"ff.m3u8"); } catch (QiniuException e) { - // 619 , no data; 612 stream not found, 但请求正常 // - if (e.code() != 619 && e.code() != 612) { - throw e; - } + assertEquals(404, e.response.statusCode); } } - //@Test + /** + * 测试创建流 + * 检测返回状态码是否为614 + * @throws QiniuException + */ + @Test public void testCreate() throws QiniuException { try { - String body = String.format("{\"key\":\"%s\"}", "stream\"Key"); - System.out.println(body); - Json.decode(body); - fail("json 解析不正确"); - } catch (JsonSyntaxException e) { - - } - try { - manager.create("streamKey"); + manager.create(stream); } catch (QiniuException e) { - if (e.code() != 614) { - throw e; - } + assertEquals(614, e.code()); } }