com.qiniu.storage.Region Java Examples

The following examples show how to use com.qiniu.storage.Region. 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 sk-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 得到机房的对应关系
 *
 * @param zone 机房名称
 * @return Region
 */
public static Region getRegion(String zone) {

    if (HUAD.equals(zone)) {
        return Region.huadong();
    } else if (HUAB.equals(zone)) {
        return Region.huabei();
    } else if (HUAN.equals(zone)) {
        return Region.huanan();
    } else if (BEIM.equals(zone)) {
        return Region.beimei();
        // 否则就是东南亚
    } else {
        return Region.qvmHuadong();
    }
}
 
Example #2
Source File: FileUploadImpl.java    From uexam with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public String uploadFile(InputStream inputStream, long size, String extName) {
    QnConfig qnConfig = systemConfig.getQn();
    Configuration cfg = new Configuration(Region.region2());
    UploadManager uploadManager = new UploadManager(cfg);
    Auth auth = Auth.create(qnConfig.getAccessKey(), qnConfig.getSecretKey());
    String upToken = auth.uploadToken(qnConfig.getBucket());
    try {
        Response response = uploadManager.put(inputStream, null, upToken, null, null);
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return qnConfig.getUrl() + "/" + putRet.key;
    } catch (QiniuException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return null;
}
 
Example #3
Source File: FileUploadImpl.java    From uexam-mysql with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public String uploadFile(InputStream inputStream, long size, String extName) {
    QnConfig qnConfig = systemConfig.getQn();
    Configuration cfg = new Configuration(Region.region2());
    UploadManager uploadManager = new UploadManager(cfg);
    Auth auth = Auth.create(qnConfig.getAccessKey(), qnConfig.getSecretKey());
    String upToken = auth.uploadToken(qnConfig.getBucket());
    try {
        Response response = uploadManager.put(inputStream, null, upToken, null, null);
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return qnConfig.getUrl() + "/" + putRet.key;
    } catch (QiniuException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return null;
}
 
Example #4
Source File: QiNiuUtil.java    From yshopmall with Apache License 2.0 6 votes vote down vote up
/**
 * 得到机房的对应关系
 * @param zone 机房名称
 * @return Region
 */
public static Region getRegion(String zone){

    if(HUAD.equals(zone)){
        return Region.huadong();
    } else if(HUAB.equals(zone)){
        return Region.huabei();
    } else if(HUAN.equals(zone)){
        return Region.huanan();
    } else if (BEIM.equals(zone)){
        return Region.beimei();
        // 否则就是东南亚
    } else {
        return Region.qvmHuadong();
    }
}
 
Example #5
Source File: QiNiuUtil.java    From eladmin with Apache License 2.0 6 votes vote down vote up
/**
 * 得到机房的对应关系
 * @param zone 机房名称
 * @return Region
 */
public static Region getRegion(String zone){

    if(HUAD.equals(zone)){
        return Region.huadong();
    } else if(HUAB.equals(zone)){
        return Region.huabei();
    } else if(HUAN.equals(zone)){
        return Region.huanan();
    } else if (BEIM.equals(zone)){
        return Region.beimei();
        // 否则就是东南亚
    } else {
        return Region.qvmHuadong();
    }
}
 
Example #6
Source File: QiniuConfigure.java    From cms with Apache License 2.0 6 votes vote down vote up
private Region getRegion() {

        switch (region) {
            case HUADONG:
                return Region.huadong();
            case HUABEI:
                return Region.huabei();
            case HUANAN:
                return Region.huanan();
            case BEIMEI:
                return Region.beimei();
            case XINJIAPO:
                return Region.xinjiapo();
            default:
                return Region.autoRegion();
        }
    }
 
Example #7
Source File: QiNiuUtil.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
public QiNiuUtil() {
	qiNiuConfig = SpringContextHolder.getApplicationContext().getBean(QiNiuConfig.class);
	if (StringUtils.isNotBlank(qiNiuConfig.getAccessKey()) && StringUtils.isNotBlank(qiNiuConfig.getSecretKey())) {
		instance = new QiNiuUtil();
		instance.auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
		Configuration config = new Configuration(Region.region2());
		instance.uploadManager = new UploadManager(config);
		instance.bucketManager = new BucketManager(instance.auth, config);
	}
}
 
Example #8
Source File: QiNiuUtils.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
/**
 * 得到机房的对应关系
 *
 * @param zone 机房名称
 * @return Region
 */
public static Region getRegion(String zone) {
    if (HUAD.equals(zone)) {
        return Region.huadong();
    } else if (HUAB.equals(zone)) {
        return Region.huabei();
    } else if (HUAN.equals(zone)) {
        return Region.huanan();
    } else if (BEIM.equals(zone)) {
        return Region.beimei();
        // 否则就是东南亚
    } else {
        return Region.qvmHuadong();
    }
}
 
Example #9
Source File: QiniuOssFileHandler.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");

    Region region = optionService.getQiniuRegion();

    String accessKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_ACCESS_KEY).toString();
    String secretKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_SECRET_KEY).toString();
    String bucket = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_BUCKET).toString();

    // Create configuration
    Configuration configuration = new Configuration(region);

    // Create auth
    Auth auth = Auth.create(accessKey, secretKey);

    BucketManager bucketManager = new BucketManager(auth, configuration);

    try {
        Response response = bucketManager.delete(bucket, key);
        if (!response.isOK()) {
            log.warn("附件 " + key + " 从七牛云删除失败");
        }
    } catch (QiniuException e) {
        log.error("Qiniu oss error response: [{}]", e.response);
        throw new FileOperationException("附件 " + key + " 从七牛云删除失败", e);
    }
}
 
Example #10
Source File: OptionServiceImpl.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Region getQiniuRegion() {
    return getByProperty(QiniuOssProperties.OSS_ZONE).map(qiniuZone -> {

        Region region;
        switch (qiniuZone.toString()) {
            case "z0":
                region = Region.region0();
                break;
            case "z1":
                region = Region.region1();
                break;
            case "z2":
                region = Region.region2();
                break;
            case "na0":
                region = Region.regionNa0();
                break;
            case "as0":
                region = Region.regionAs0();
                break;
            default:
                // Default is detecting zone automatically
                region = Region.autoRegion();
        }
        return region;

    }).orElseGet(Region::autoRegion);
}
 
Example #11
Source File: UploadConfiguration.java    From spring-cloud-shop with MIT License 4 votes vote down vote up
/**
 * 获取设置的UploadManager配置中心
 */
private static UploadManager getUploadManager() {
    Configuration configuration = new Configuration(Region.region0());
    return new UploadManager(configuration);
}
 
Example #12
Source File: QiniuAutoConfiguration.java    From spring-cloud-shop with MIT License 4 votes vote down vote up
@Bean
public com.qiniu.storage.Configuration configuration() {
    return new com.qiniu.storage.Configuration(Region.autoRegion());
}
 
Example #13
Source File: QiniuOssFileHandler.java    From halo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UploadResult upload(MultipartFile file) {
    Assert.notNull(file, "Multipart file must not be null");

    Region region = optionService.getQiniuRegion();

    String accessKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_ACCESS_KEY).toString();
    String secretKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_SECRET_KEY).toString();
    String bucket = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_BUCKET).toString();
    String protocol = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_PROTOCOL).toString();
    String domain = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_DOMAIN).toString();
    String source = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_SOURCE, String.class, "");
    String styleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_STYLE_RULE, String.class, "");
    String thumbnailStyleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");

    // Create configuration
    Configuration configuration = new Configuration(region);

    // Create auth
    Auth auth = Auth.create(accessKey, secretKey);
    // Build put plicy
    StringMap putPolicy = new StringMap();
    putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"size\":$(fsize),\"width\":$(imageInfo.width),\"height\":$(imageInfo.height)}");
    // Get upload token
    String uploadToken = auth.uploadToken(bucket, null, 60 * 60, putPolicy);

    // Create temp path
    Path tmpPath = Paths.get(System.getProperty("java.io.tmpdir"), bucket);

    StringBuilder basePath = new StringBuilder(protocol)
        .append(domain)
        .append(URL_SEPARATOR);

    try {
        String basename = FilenameUtils.getBasename(Objects.requireNonNull(file.getOriginalFilename()));
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String timestamp = String.valueOf(System.currentTimeMillis());
        StringBuilder upFilePath = new StringBuilder();
        if (StringUtils.isNotEmpty(source)) {
            upFilePath.append(source)
                .append(URL_SEPARATOR);
        }
        upFilePath.append(basename)
            .append("_")
            .append(timestamp)
            .append(".")
            .append(extension);

        // Get file recorder for temp directory
        FileRecorder fileRecorder = new FileRecorder(tmpPath.toFile());
        // Get upload manager
        UploadManager uploadManager = new UploadManager(configuration, fileRecorder);
        // Put the file
        Response response = uploadManager.put(file.getInputStream(), upFilePath.toString(), uploadToken, null, null);

        if (log.isDebugEnabled()) {
            log.debug("Qiniu oss response: [{}]", response.toString());
            log.debug("Qiniu oss response body: [{}]", response.bodyString());
        }

        // Convert response
        PutSet putSet = JsonUtils.jsonToObject(response.bodyString(), PutSet.class);

        // Get file full path
        String filePath = StringUtils.join(basePath.toString(), upFilePath.toString());

        // Build upload result
        UploadResult result = new UploadResult();
        result.setFilename(basename);
        result.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
        result.setKey(upFilePath.toString());
        result.setSuffix(extension);
        result.setWidth(putSet.getWidth());
        result.setHeight(putSet.getHeight());
        result.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
        result.setSize(file.getSize());

        if (isImageType(result.getMediaType())) {
            if (ImageUtils.EXTENSION_ICO.equals(extension)) {
                result.setThumbPath(filePath);
            } else {
                result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
            }
        }

        return result;
    } catch (IOException e) {
        if (e instanceof QiniuException) {
            log.error("Qiniu oss error response: [{}]", ((QiniuException) e).response);
        }

        throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到七牛云失败", e);
    }
}
 
Example #14
Source File: OptionService.java    From halo with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get qiniu oss region.
 *
 * @return qiniu region
 */
@NonNull
Region getQiniuRegion();