Java Code Examples for com.qiniu.storage.BucketManager#delete()

The following examples show how to use com.qiniu.storage.BucketManager#delete() . 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: 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 2
Source File: QiniuStorageImpl.java    From mblog with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void deleteFile(String storePath) {
    String accessKey = options.getValue(oss_key);
    String secretKey = options.getValue(oss_secret);
    String domain = options.getValue(oss_domain);
    String bucket = options.getValue(oss_bucket);

    if (StringUtils.isAnyBlank(accessKey, secretKey, domain, bucket)) {
        throw new MtonsException("请先在后台设置阿里云配置信息");
    }

    String path = StringUtils.remove(storePath, domain.trim());

    Zone z = Zone.autoZone();
    Configuration configuration = new Configuration(z);
    Auth auth = Auth.create(accessKey, secretKey);

    BucketManager bucketManager = new BucketManager(auth, configuration);
    try {
        bucketManager.delete(bucket, path);
    } catch (QiniuException e) {
        Response r = e.response;
        log.error(e.getMessage(), r.toString());
    }
}
 
Example 3
Source File: QiniuApiClient.java    From OneBlog with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 删除七牛空间图片方法
 *
 * @param key 七牛空间中文件名称
 */
@Override
public boolean removeFile(String key) {
    this.check();

    if (StringUtils.isNullOrEmpty(key)) {
        throw new QiniuApiException("[" + this.storageType + "]删除文件失败:文件key为空");
    }
    Auth auth = Auth.create(this.accessKey, this.secretKey);
    Configuration config = new Configuration(Region.autoRegion());
    BucketManager bucketManager = new BucketManager(auth, config);
    try {
        Response re = bucketManager.delete(this.bucket, key);
        return re.isOK();
    } catch (QiniuException e) {
        Response r = e.response;
        throw new QiniuApiException("[" + this.storageType + "]删除文件发生异常:" + r.toString());
    }
}
 
Example 4
Source File: QiniuCloud.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 删除文件
 * 
 * @param key
 * @return
 */
protected boolean delete(String key) {
	Assert.notNull(auth, "云存储账户未配置");
	BucketManager bucketManager = new BucketManager(auth, CONFIGURATION);
	Response resp;
	try {
		resp = bucketManager.delete(this.bucketName, key);
		if (resp.isOK()) {
			return true;
		} else {
			throw new RebuildException("删除文件失败 : " + this.bucketName + " < " + key + " : " + resp.bodyString());
		}
	} catch (QiniuException e) {
		throw new RebuildException("删除文件失败 : " + this.bucketName + " < " + key, e);
	}
}
 
Example 5
Source File: FileService.java    From ml-blog with MIT License 6 votes vote down vote up
/**
 * 文件删除
 * @param key
 * @return
 */
public Response delete(String key) throws GlobalException{

    // 有配置数据,但上传凭证为空
    if (!commonMap.containsKey("upToken")) {
        // 创建七牛云组件
        createQiniuComponent();
    }

    try {
        BucketManager bucketManager = (BucketManager) commonMap.get("bucketManager");
        String bucket = commonMap.get("qn_bucket").toString();
        Response response = bucketManager.delete(bucket, key);
        int retry = 0;
        while(response.needRetry() && retry < 3) {
            response = bucketManager.delete(bucket, key);
            retry++;
        }
        return response;
    } catch (QiniuException ex) {
        log.error("文件删除异常:",ex.toString());
        throw new GlobalException(500, ex.toString());
    }
}
 
Example 6
Source File: QiNiuStorage.java    From springboot-learn with MIT License 6 votes vote down vote up
public void deleteFile() {
    if (StringUtils.isAnyBlank(accessKey, secretKey, domain, bucket)) {
        throw new IllegalArgumentException("请先设置配置信息");
    }

    //文件所在的目录名
    String fileDir = "image/jpg/";
    //文件名
    String key = "user.png";

    String path = StringUtils.remove(fileDir + key, domain.trim());

    Zone z = Zone.autoZone();
    Configuration configuration = new Configuration(z);
    Auth auth = Auth.create(accessKey, secretKey);

    BucketManager bucketManager = new BucketManager(auth, configuration);
    try {
        bucketManager.delete(bucket, path);
    } catch (QiniuException e) {
        Response r = e.response;
        System.out.println(e.response.getInfo());
    }
}
 
Example 7
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 8
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 9
Source File: QiNiuServiceImpl.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
@Override
//    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(QiniuContent content, QiniuConfig config) {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(QiNiuUtil.getRegion(config.getZone()));
        Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(content.getBucket(), content.getName() + "." + content.getSuffix());
            qiniuContentService.removeById(content.getId());
        } catch (QiniuException ex) {
            qiniuConfigService.removeById(content.getId());
        }
    }
 
Example 10
Source File: QiNiuServiceImpl.java    From eladmin with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(QiniuContent content, QiniuConfig config) {
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiNiuUtil.getRegion(config.getZone()));
    Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
    BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        bucketManager.delete(content.getBucket(), content.getKey() + "." + content.getSuffix());
        qiniuContentRepository.delete(content);
    } catch (QiniuException ex) {
        qiniuContentRepository.delete(content);
    }
}
 
Example 11
Source File: QiNiuServiceImpl.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
@Override
public int deleteQiNiuContent(String ids) {
    QiNiuConfig qiNiuConfig = getQiNiuConfig();
    if (!qiNiuConfig.check()) {
        throw new CustomException("七牛云配置信息不完整,请先填写七牛云配置信息");
    }
    Long[] idArray = ConvertUtils.toLongArray(ids);
    //查询
    List<QiNiuContent> qiNiuContentList = qiNiuContentMapper.selectContentByIds(idArray);

    for (QiNiuContent qiNiuContent : qiNiuContentList) {
        if (Objects.isNull(qiNiuContent) || StringUtils.isEmpty(qiNiuContent.getName())) {
            throw new CustomException("数据异常");
        }
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(QiNiuUtils.getRegion(qiNiuConfig.getZone()));
        Auth auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(qiNiuContent.getBucket(), qiNiuContent.getName());
        } catch (QiniuException e) {
            log.error("删除七牛云图片出错,{},", e.getMessage(), e);
            //出错后删除本地数据库文件
        }
    }
    return qiNiuContentMapper.deleteContentByIds(idArray);
}
 
Example 12
Source File: QiNiuServiceImpl.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void delete(QiniuContent content, QiniuConfig config) {
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiNiuUtil.getRegion(config.getZone()));
    Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
    BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        bucketManager.delete(content.getBucket(), content.getKey() + "." + content.getSuffix());
        qiniuContentDao.delete(content);
    } catch (QiniuException ex) {
        qiniuContentDao.delete(content);
    }
}
 
Example 13
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 14
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 15
Source File: QiniuStorageManager.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
/**
 *
 根据名字删除文件
 */
public void ueditorDeleteFile(String filename) {
    Auth auth = Auth.create(accessKey, secretKey);
    BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        bucketManager.delete(ueditor_bucket,filename);
    } catch (QiniuException e) {
        //删除失败
        log.error(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 16
Source File: QiniuFileProvider.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 删除文件
 *
 * @param path 文件路径
 */
@Override
public boolean delete(String path) throws IOException {
    Auth auth = Auth.create(ConfigUtils.getQiniuAccessKey(), ConfigUtils.getQiniuSecretKey());
    BucketManager bucketManager = new BucketManager(auth);
    try {
        bucketManager.delete(ConfigUtils.getBucketURL(), path);
        return true;
    } catch (QiniuException e) {
        Response r = e.response;
        throw new IOException(r.bodyString());
    }
}
 
Example 17
Source File: QiNiuFileManage.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteFile(String key) {

    OssSetting os = getOssSetting();
    Auth auth = Auth.create(os.getAccessKey(), os.getSecretKey());
    BucketManager bucketManager = new BucketManager(auth, getConfiguration(os.getZone()));
    try {
        bucketManager.delete(os.getBucket(), key);
    } catch (QiniuException ex) {
        throw new SkException("删除文件失败," + ex.response.toString());
    }
}