Java Code Examples for com.qiniu.common.Zone#zone0()

The following examples show how to use com.qiniu.common.Zone#zone0() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QiNiuUtil.java    From javabase with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    Configuration cfg = new Configuration(Zone.zone0());
    cfg.connectTimeout=5000;
    cfg.readTimeout=5000;
    cfg.writeTimeout=2000;
    auth = Auth.create(accessKey, secretKey);
    uploadManager = new UploadManager(cfg);
    // 实例化一个BucketManager对象
    bucketManager = new BucketManager(auth, cfg);

    new Thread() {
        public void run() {
            deleteBlockingDequeImage();
        }
    }.start();
}
 
Example 2
Source File: AttachmentServiceImpl.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 七牛云删除附件
 *
 * @param key key
 * @return boolean
 */
@Override
public boolean deleteQiNiuAttachment(String key) {
    boolean flag = true;
    final Configuration cfg = new Configuration(Zone.zone0());
    final String accessKey = HaloConst.OPTIONS.get("qiniu_access_key");
    final String secretKey = HaloConst.OPTIONS.get("qiniu_secret_key");
    final String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
    if (StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(bucket)) {
        return false;
    }
    final Auth auth = Auth.create(accessKey, secretKey);
    final BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        bucketManager.delete(bucket, key);
    } catch (QiniuException ex) {
        System.err.println(ex.code());
        System.err.println(ex.response.toString());
        flag = false;
    }
    return flag;
}
 
Example 3
Source File: AttachmentServiceImpl.java    From blog-sharon with Apache License 2.0 6 votes vote down vote up
/**
 * 七牛云删除附件
 *
 * @param key key
 * @return boolean
 */
@Override
public boolean deleteQiNiuAttachment(String key) {
    boolean flag = true;
    Configuration cfg = new Configuration(Zone.zone0());
    String accessKey = HaloConst.OPTIONS.get("qiniu_access_key");
    String secretKey = HaloConst.OPTIONS.get("qiniu_secret_key");
    String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
    if (StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(bucket)) {
        return false;
    }
    Auth auth = Auth.create(accessKey, secretKey);
    BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        bucketManager.delete(bucket, key);
    } catch (QiniuException ex) {
        System.err.println(ex.code());
        System.err.println(ex.response.toString());
        flag = false;
    }
    return flag;
}
 
Example 4
Source File: QiniuFileUtil.java    From mysiteforme with Apache License 2.0 6 votes vote down vote up
/**
 * 上传base64位的图片
 * @param base64
 * @return
 */
public static String uploadBase64(String base64,String name) {
	Zone z = Zone.zone0();
	Configuration config = new Configuration(z);
	UploadManager uploadManager = new UploadManager(config);
	Auth auth = Auth.create(qiniuAccess, qiniuKey);
	String token = auth.uploadToken(bucketName),filePath;

	byte[] data = Base64.decodeBase64(base64);
	try {
		uploadManager.put(data,name,token);
	} catch (IOException e) {
		e.printStackTrace();
	}
	filePath = path+name;
	return filePath;
}
 
Example 5
Source File: AttachmentServiceImpl.java    From SENS with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 七牛云删除附件
 *
 * @param key key
 * @return boolean
 */
@Override
public boolean deleteQiNiuAttachment(String key) {
    boolean flag = true;
    final Configuration cfg = new Configuration(Zone.zone0());
    final String accessKey = SensConst.OPTIONS.get("qiniu_access_key");
    final String secretKey = SensConst.OPTIONS.get("qiniu_secret_key");
    final String bucket = SensConst.OPTIONS.get("qiniu_bucket");
    if (StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(bucket)) {
        return false;
    }
    final Auth auth = Auth.create(accessKey, secretKey);
    final BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        bucketManager.delete(bucket, key);
    } catch (QiniuException ex) {
        System.err.println(ex.code());
        System.err.println(ex.response.toString());
        flag = false;
    }
    return flag;
}
 
Example 6
Source File: QiniuOssClientTest.java    From markdown-image-kit with MIT License 5 votes vote down vote up
public static String upload(InputStream inputStream, String bucket, String preFix, String accessKey, String secretKey) {
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    //...其他参数参考类注释

    UploadManager uploadManager = new UploadManager(cfg);

    //默认不指定key的情况下,以文件内容的hash值作为文件名
    String key = null;

    byte[] uploadBytes = "hello qiniu cloud".getBytes(StandardCharsets.UTF_8);
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(uploadBytes);
    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);

    try {
        Response response = uploadManager.put(byteInputStream, key, upToken, null, null);
        //解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        log.info(putRet.key);
        log.info(putRet.hash);
    } catch (QiniuException ex) {
        Response r = ex.response;
        log.info(r.toString());
        try {
            log.info(r.bodyString());
        } catch (QiniuException ex2) {
            //ignore
        }
    }

    return "";
}
 
Example 7
Source File: QiniuOssClientTest.java    From markdown-image-kit with MIT License 5 votes vote down vote up
public static String upload(File file, String bucket, String accessKey, String secretKey, String endpoint) {
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    //...其他参数参考类注释

    UploadManager uploadManager = new UploadManager(cfg);
    //默认不指定key的情况下,以文件内容的hash值作为文件名
    String key = "test.png";

    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    BucketManager bucketManager = new BucketManager(auth, cfg);

    try {
        Response response = uploadManager.put(file, key, upToken);
        //解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        log.info(putRet.key);
        log.info(putRet.hash);

        FileInfo fileInfo = bucketManager.stat(bucket, key);
        System.out.println(fileInfo.hash);
        System.out.println(fileInfo.fsize);
        System.out.println(fileInfo.mimeType);
        System.out.println(fileInfo.putTime);
        return endpoint + key;
    } catch (QiniuException ex) {
        Response r = ex.response;
        log.info(r.toString());
        try {
            log.info(r.bodyString());
        } catch (QiniuException ex2) {
            //ignore
        }
    }
    return "";
}
 
Example 8
Source File: QiniuCloudService.java    From my-site with Apache License 2.0 5 votes vote down vote up
public String upload(MultipartFile file, String fileName) {

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(BUCKET);
        try {
            Response response = null;

            response = uploadManager.put(file.getInputStream(), fileName, upToken, null, null);

            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            return putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
Example 9
Source File: QiniuOssUtil.java    From FEBS-Security with Apache License 2.0 5 votes vote down vote up
public static String upload(FileInputStream file, String fileName) {
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    // 其他参数参考类注释
    UploadManager uploadManager = new UploadManager(cfg);

    try {
        Auth auth = Auth.create(properies.getOss().getQiniu().getAccessKey(), properies.getOss().getQiniu().getSecretKey());
        String upToken = auth.uploadToken(properies.getOss().getQiniu().getBucket());
        try {
            Response response = uploadManager.put(file, fileName, upToken, null, null);
            // 解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

            return properies.getOss().getQiniu().getPath() + "/" + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            logger.error(r.toString());
            try {
                logger.error(r.bodyString());
            } catch (QiniuException ignore) {

            }
        }
    } catch (Exception e) {
        logger.error("error", e);
    }
    return null;
}
 
Example 10
Source File: QiNiuUtil.java    From javabase with Apache License 2.0 5 votes vote down vote up
public void test(){
        String key="1539702607173729.mp4";
        String saveJpgEntry = String.format("%s:test.jpg", bucketName);
        String persistentOpfs = String.format("vframe/jpg/offset/1/w/500/h/500|saveas/%s", UrlSafeBase64.encodeToString(saveJpgEntry));

        //数据处理队列名称,必须
        String persistentPipeline = "mps-pipe1"+System.currentTimeMillis();
//数据处理完成结果通知地址
        String persistentNotifyUrl = "http://api.example.com/qiniu/pfop/notify";
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
//构建持久化数据处理对象
        OperationManager operationManager = new OperationManager(auth, cfg);
        try {
            String persistentId = operationManager.pfop(bucketName, key, persistentOpfs, persistentPipeline, persistentNotifyUrl, true);
            //可以根据该 persistentId 查询任务处理进度
            System.out.println(persistentId);
            OperationStatus operationStatus = operationManager.prefop(persistentId);

            System.out.println("operationStatus");
            //解析 operationStatus 的结果
        } catch (Exception e) {
            log.error("",e);
        }

    }
 
Example 11
Source File: QiniuFileUtil.java    From mysiteforme with Apache License 2.0 5 votes vote down vote up
/***
 * 普通上传图片
 * @param file
 * @return
 * @throws QiniuException
 * @throws IOException
 */
public static String upload(MultipartFile file) throws IOException, NoSuchAlgorithmException {
	Zone z = Zone.zone0();
	Configuration config = new Configuration(z);
	String fileName = "", extName = "", filePath = "";
	if (null != file && !file.isEmpty()) {
		extName = file.getOriginalFilename().substring(
				file.getOriginalFilename().lastIndexOf("."));
		fileName = UUID.randomUUID() + extName;
		UploadManager uploadManager = new UploadManager(config);
		Auth auth = Auth.create(qiniuAccess, qiniuKey);
		String token = auth.uploadToken(bucketName);
		byte[] data = file.getBytes();
		QETag tag = new QETag();
		String hash = tag.calcETag(file);
		Rescource rescource = new Rescource();
		EntityWrapper<RestResponse> wrapper = new EntityWrapper<>();
		wrapper.eq("hash",hash);
		rescource = rescource.selectOne(wrapper);
		if( rescource!= null){
			return rescource.getWebUrl();
		}
		Response r = uploadManager.put(data, fileName, token);
		if (r.isOK()) {
			filePath = path + fileName;
			rescource = new Rescource();
			rescource.setFileName(fileName);
			rescource.setFileSize(new java.text.DecimalFormat("#.##").format(file.getSize()/1024)+"kb");
			rescource.setHash(hash);
			rescource.setFileType(StringUtils.isBlank(extName)?"unknown":extName);
			rescource.setWebUrl(filePath);
			rescource.setSource("qiniu");
			rescource.insert();
		}
	}
	return filePath;
}
 
Example 12
Source File: QiniuFileUtil.java    From mysiteforme with Apache License 2.0 5 votes vote down vote up
/***
 * 删除已经上传的图片
 * @param imgPath
 */
public static void deleteQiniuP(String imgPath) {
	Zone z = Zone.zone0();
	Configuration config = new Configuration(z);
	Auth auth = Auth.create(qiniuAccess, qiniuKey);
	BucketManager bucketManager = new BucketManager(auth,config);
	imgPath = imgPath.replace(path, "");
	try {
		bucketManager.delete(bucketName, imgPath);
	} catch (QiniuException e) {
		e.printStackTrace();
	}
}
 
Example 13
Source File: QiniuConfiguration.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Bean
public com.qiniu.storage.Configuration qiniuConfiguration() {
	return new com.qiniu.storage.Configuration(Zone.zone0());
}
 
Example 14
Source File: UploadConfig.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 华东机房
 */
@Bean
public com.qiniu.storage.Configuration qiniuConfig() {
	return new com.qiniu.storage.Configuration(Zone.zone0());
}
 
Example 15
Source File: QiNiuUtilTest.java    From javabase with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    String bucket = "product";
    String key = "1539702607173729.mp4";
    String accessKey = "Bip6vCz50yIiMGjecwhvbRXluTP9XVWr8_NaA9Py";
    String secretKey = "f3KjUz5LUjbscU-FG-pyk_qkYTCyah25qS9WFmZW";
    // 待处理文件所在空间
    // 待处理文件名
    Auth auth = Auth.create(accessKey, secretKey);
    // 数据处理指令,支持多个指令
    String persistentOpfs = String.format("vframe/jpg/offset/0/w/480/h/360");
    // 数据处理队列名称,必须
    String persistentPipeline = "";
    // 数据处理完成结果通知地址
    String persistentNotifyUrl = "";
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    // ...其他参数参考类注释
    // 构建持久化数据处理对象
    OperationManager operationManager = new OperationManager(auth, cfg);
    try {
        String persistentId = operationManager.pfop(bucket,
                URLEncoder.encode(key),
                persistentOpfs,
                UrlSafeBase64.encodeToString(persistentPipeline),
                UrlSafeBase64.encodeToString(persistentNotifyUrl), true);
        // 可以根据该 persistentId 查询任务处理进度
        System.out.println(persistentId);
        OperationStatus operationStatus;
        while (true) {
            operationStatus = operationManager.prefop(persistentId);
            if (operationStatus.code == 0) {
                break;
            }
        }
        // 解析 operationStatus 的结果
        log.info("视频缩略图地址:" + "http://images.3dclo.com/" + operationStatus.items[0].key);
        String result = Request.Get("http://images.3dclo.com/1539702607173729.mp4?avinfo")
                .execute().returnContent().asString(Charset.forName("utf-8"));

        JSONObject jsonObject = JSONObject.parseObject(result);
        String duration = jsonObject.getJSONObject("format").getString("duration");
        log.info("视频长度:" + duration);
    } catch (Exception e) {
        System.err.println(e.toString());
    }

}
 
Example 16
Source File: QiniuUploadServiceImpl.java    From mysiteforme with Apache License 2.0 4 votes vote down vote up
private BucketManager getBucketManager(){
    Zone z = Zone.zone0();
    Configuration config = new Configuration(z);
    Auth auth = Auth.create(getUploadInfo().getQiniuAccessKey(), getUploadInfo().getQiniuSecretKey());
    return new BucketManager(auth,config);
}
 
Example 17
Source File: QiniuUploadServiceImpl.java    From mysiteforme with Apache License 2.0 4 votes vote down vote up
private UploadManager getUploadManager(){
    Zone z = Zone.zone0();
    Configuration config = new Configuration(z);
    return new UploadManager(config);
}
 
Example 18
Source File: UploadConfig.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 华东机房
 */
@Bean
public com.qiniu.storage.Configuration qiniuConfig() {
	return new com.qiniu.storage.Configuration(Zone.zone0());
}
 
Example 19
Source File: UploadConfig.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 华东机房
 */
@Bean
public com.qiniu.storage.Configuration qiniuConfig() {
	return new com.qiniu.storage.Configuration(Zone.zone0());
}
 
Example 20
Source File: QiniuOSSConfig.java    From open-capacity-platform with Apache License 2.0 4 votes vote down vote up
/**
 * 华东机房
 */
@Bean
public com.qiniu.storage.Configuration qiniuConfig() {
    return new com.qiniu.storage.Configuration(Zone.zone0());
}