/** * 七牛云OSS对象存储简单上传实现 */ @Override public void store(InputStream inputStream, long contentLength, String contentType, String keyName) { if (uploadManager == null) { if (auth == null) { auth = Auth.create(accessKey, secretKey); } uploadManager = new UploadManager(new Configuration()); } try { String upToken = auth.uploadToken(bucketName); Response response = uploadManager.put(inputStream, keyName, upToken, null, contentType); } catch (QiniuException ex) { ex.printStackTrace(); } }
public synchronized byte[] body() throws QiniuException { if (body != null) { return body; } try { this.body = response.body().bytes(); } catch (IOException e) { throw new QiniuException(e); } return body; }
@Test public void testPost3() { Response r = null; try { r = httpManager.post("http://httpbin.org/status/500", "hello", null); Assert.fail(); } catch (QiniuException e) { if (e.code() != -1) { Assert.assertEquals(500, e.code()); } else { e.printStackTrace(); } } }
} catch (IOException e) { close(); throw new QiniuException(e); response = makeBlock(blockBuffer, bufferIndex); } catch (QiniuException e) { if (e.code() < 0) { host = configuration.upHostBackup(upToken); if (blockInfo0.crc32 != crc) { retry = true; temp = new QiniuException(new Exception("block's crc32 is not match")); throw new QiniuException(new Exception("block's crc32 is not match"));
@Test public void testPost4() { Response r = null; try { r = httpManager.post("http://httpbin.org/status/418", "hello", null); Assert.fail(); } catch (QiniuException e) { if (e.code() != -1) { Assert.assertEquals(418, e.code()); } else { e.printStackTrace(); } } }
private void processUploadException(String fileKey, QiniuException e) { Response r = e.response; String message; try { message = r.bodyString(); } catch (Exception e2) { message = r.toString(); } throw new FSOperErrorException(name(), e.code(), message); }
} catch (IOException e) { close(); throw new QiniuException(e); response = makeBlock(blockBuffer, bufferIndex); } catch (QiniuException e) { if (e.code() < 0) { host = configuration.upHostBackup(upToken); if (blockInfo0.crc32 != crc) { retry = true; temp = new QiniuException(new Exception("block's crc32 is not match")); throw new QiniuException(new Exception("block's crc32 is not match"));
public String error() { if (error != null) { return error; } if (response == null || response.statusCode / 100 == 2 || !response.isJson()) { return null; } Error e = null; try { e = response.jsonToObject(Error.class); } catch (QiniuException e1) { e1.printStackTrace(); } error = e == null ? "" : e.error; return error; } }
@Test public void testInvalidToken() { final String expectKey = "你好"; try { uploadManager.put("hello".getBytes(), expectKey, TestConfig.dummyUptoken); fail(); } catch (QiniuException e) { if (e.code() != -1) { assertEquals(401, e.code()); assertNotNull(e.response.reqId); } else { e.printStackTrace(); } } }
@Override @Transactional(rollbackFor = Exception.class) public void delete(QiniuContent content, QiniuConfig config) { //构造一个带指定Zone对象的配置类 Configuration cfg = QiNiuUtil.getConfiguration(config.getZone()); Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey()); BucketManager bucketManager = new BucketManager(auth, cfg); try { bucketManager.delete(content.getBucket(), content.getKey()); qiniuContentRepository.delete(content); } catch (QiniuException ex) { System.err.println(ex.code()); System.err.println(ex.response.toString()); } }
this.file = new FileInputStream(f); } catch (FileNotFoundException e) { throw new QiniuException(e); } catch (IOException e) { close(); throw new QiniuException(e); } catch (IOException e) { close(); throw new QiniuException(e); response = makeBlock(blockBuffer, blockSize); } catch (QiniuException e) { if (e.code() < 0) { host = configuration.upHostBackup(upToken); if (blockInfo0.crc32 != crc) { retry = true; temp = new QiniuException(new Exception("block's crc32 is not match")); throw new QiniuException(new Exception("block's crc32 is not match"));
public synchronized byte[] body() throws QiniuException { if (body != null) { return body; } try { this.body = response.body().bytes(); } catch (IOException e) { throw new QiniuException(e); } return body; }
public String error() { if (error != null) { return error; } if (response == null || response.statusCode / 100 == 2 || !response.isJson()) { return null; } Error e = null; try { e = response.jsonToObject(Error.class); } catch (QiniuException e1) { e1.printStackTrace(); } error = e == null ? "" : e.error; return error; } }
@Test public void testGetNoExistStream() { try { manager.attribute("nnnoexist"); fail("should not exist"); } catch (QiniuException e) { e.printStackTrace(); assertEquals(612, e.code()); } }
@Test public void testDelete() { Map<String[], Integer> entryCodeMap = new HashMap<String[], Integer>(); entryCodeMap.put(new String[]{TestConfig.testBucket_z0, TestConfig.dummyKey}, TestConfig.ERROR_CODE_KEY_NOT_EXIST); entryCodeMap.put(new String[]{TestConfig.testBucket_z0, null}, TestConfig.ERROR_CODE_BUCKET_NOT_EXIST); entryCodeMap.put(new String[]{TestConfig.dummyBucket, null}, TestConfig.ERROR_CODE_BUCKET_NOT_EXIST); entryCodeMap.put(new String[]{TestConfig.dummyBucket, TestConfig.dummyKey}, TestConfig.ERROR_CODE_BUCKET_NOT_EXIST); for (Map.Entry<String[], Integer> entry : entryCodeMap.entrySet()) { String bucket = entry.getKey()[0]; String key = entry.getKey()[1]; int code = entry.getValue(); try { bucketManager.delete(bucket, key); Assert.fail(bucket + ":" + key + "==> " + "delete failed"); } catch (QiniuException e) { Assert.assertEquals(code, e.code()); } } }
this.file = new FileInputStream(f); } catch (FileNotFoundException e) { throw new QiniuException(e); } catch (IOException e) { close(); throw new QiniuException(e); } catch (IOException e) { close(); throw new QiniuException(e); response = makeBlock(blockBuffer, blockSize); } catch (QiniuException e) { if (e.code() < 0) { host = configuration.upHostBackup(upToken); if (blockInfo0.crc32 != crc) { retry = true; temp = new QiniuException(new Exception("block's crc32 is not match")); throw new QiniuException(new Exception("block's crc32 is not match"));
public static String createTimestampAntiLeechUrl(URL oUrl, String encryptKey, long deadline) throws QiniuException { try { String urlencodedPath = URLEncoder.encode(oUrl.getPath(), "UTF-8").replaceAll("%2F", "/"); String query = oUrl.getQuery(); String file = (query == null) ? urlencodedPath : (urlencodedPath + "?" + query); URL url = new URL(oUrl.getProtocol(), oUrl.getHost(), oUrl.getPort(), file); String expireHex = Long.toHexString(deadline); String toSignStr = String.format("%s%s%s", encryptKey, urlencodedPath, expireHex); String signedStr = StringUtils.md5Lower(toSignStr); String signedUrl; if (url.getQuery() != null) { signedUrl = String.format("%s&sign=%s&t=%s", url, signedStr, expireHex); } else { signedUrl = String.format("%s?sign=%s&t=%s", url, signedStr, expireHex); } return signedUrl; } catch (Exception e) { throw new QiniuException(e, "timestamp anti leech failed"); } }
@Test public void kickUser() { try { Response response = rmanager.kickUser("dg8lz5211", "zwhome", "userid"); System.out.print(getString(response)); } catch (QiniuException e) { e.printStackTrace(); } catch (Exception e1) { } }
@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"); } catch (QiniuException e) { if (e.code() != 614) { throw e; } } }