Search in sources :

Example 1 with Response

use of com.qiniu.http.Response in project xmall by Exrick.

the class QiniuUtil method qiniuUpload.

public static String qiniuUpload(String filePath) {
    // 构造一个带指定Zone对象的配置类 zone2华南
    Configuration cfg = new Configuration(Zone.zone2());
    UploadManager uploadManager = new UploadManager(cfg);
    String localFilePath = filePath;
    // 默认不指定key的情况下,以文件内容的hash值作为文件名
    String key = null;
    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    try {
        Response response = uploadManager.put(localFilePath, key, upToken);
        // 解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return origin + putRet.key;
    } catch (QiniuException ex) {
        Response r = ex.response;
        log.warn(r.toString());
        try {
            log.warn(r.bodyString());
            return r.bodyString();
        } catch (QiniuException ex2) {
        // ignore
        }
    }
    return null;
}
Also used : Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) Gson(com.google.gson.Gson) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) UploadManager(com.qiniu.storage.UploadManager)

Example 2 with Response

use of com.qiniu.http.Response in project paascloud-master by paascloud.

the class OptQiniuOssServiceImpl method deleteFile.

@Override
@Retryable(value = Exception.class, backoff = @Backoff(delay = 5000, multiplier = 2))
public void deleteFile(String fileName, String bucketName) throws QiniuException {
    log.info("deleteFile - 删除OSS文件. fileName={}, bucketName={}", fileName, bucketName);
    Preconditions.checkArgument(StringUtils.isNotEmpty(fileName), ErrorCodeEnum.OPC10040010.msg());
    Preconditions.checkArgument(StringUtils.isNotEmpty(bucketName), "存储空间不能为空");
    Response response = bucketManager.delete(bucketName, fileName);
    log.info("deleteFile - 删除OSS文件. [OK] response={}", response);
}
Also used : Response(com.qiniu.http.Response) Retryable(org.springframework.retry.annotation.Retryable)

Example 3 with Response

use of com.qiniu.http.Response in project paascloud-master by paascloud.

the class OptQiniuOssServiceImpl method batchDeleteFile.

@Override
public Set<String> batchDeleteFile(String[] fileNameList, String bucketName) throws QiniuException {
    log.info("batchDeleteFile - 删除OSS文件. fileNameList={}, bucketName={}", fileNameList, bucketName);
    BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
    batchOperations.addDeleteOp(bucketName, fileNameList);
    Response response = bucketManager.batch(batchOperations);
    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
    Set<String> failSet = Sets.newHashSet();
    for (int i = 0; i < fileNameList.length; i++) {
        BatchStatus status = batchStatusList[i];
        String fileName = fileNameList[i];
        if (status.code != 200) {
            failSet.add(fileName);
            log.error("batchDeleteFile - 删除OSS文件. [FAIL] fileName={}, error={}", fileName, status.data.error);
        } else {
            log.info("batchDeleteFile - 删除OSS文件. [OK] fileName={}, bucketName={}", fileName, bucketName);
        }
    }
    return failSet;
}
Also used : Response(com.qiniu.http.Response) BucketManager(com.qiniu.storage.BucketManager) BatchStatus(com.qiniu.storage.model.BatchStatus)

Example 4 with Response

use of com.qiniu.http.Response in project paascloud-master by paascloud.

the class OptQiniuOssServiceImpl method uploadFile.

@Override
public OptUploadFileRespDto uploadFile(byte[] uploadBytes, String fileName, String filePath, String bucketName) throws IOException {
    log.info("uploadFile - 上传文件. fileName={}, bucketName={}", fileName, bucketName);
    Preconditions.checkArgument(uploadBytes != null, "读取文件失败");
    Preconditions.checkArgument(StringUtils.isNotEmpty(fileName), ErrorCodeEnum.OPC10040010.msg());
    Preconditions.checkArgument(StringUtils.isNotEmpty(filePath), "文件路径不能为空");
    Preconditions.checkArgument(StringUtils.isNotEmpty(bucketName), "存储节点不能为空");
    InputStream is = new ByteArrayInputStream(uploadBytes);
    String inputStreamFileType = FileTypeUtil.getType(is);
    String newFileName = UniqueIdGenerator.generateId() + "." + inputStreamFileType;
    // 检查数据大小
    this.checkFileSize(uploadBytes);
    Response response = uploadManager.put(uploadBytes, filePath + newFileName, getUpToken(bucketName));
    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    log.info("uploadFile - 上传文件. [OK] putRet={}", putRet);
    if (PublicUtil.isEmpty(putRet) || StringUtils.isEmpty(putRet.key)) {
        throw new OpcBizException(ErrorCodeEnum.OPC10040009);
    }
    String fileUrl;
    // 获取图片路径
    if (StringUtils.equals(OPEN_IMG_BUCKET, bucketName)) {
        fileUrl = paascloudProperties.getQiniu().getOss().getPublicHost() + "/" + filePath + newFileName;
    } else {
        String domainUrl = paascloudProperties.getQiniu().getOss().getPrivateHost();
        fileUrl = this.getFileUrl(domainUrl, fileName);
    }
    OptUploadFileRespDto result = new OptUploadFileRespDto();
    result.setAttachmentUrl(fileUrl);
    result.setAttachmentName(newFileName);
    result.setAttachmentPath(filePath);
    return result;
}
Also used : Response(com.qiniu.http.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) OpcBizException(com.paascloud.provider.exceptions.OpcBizException) OptUploadFileRespDto(com.paascloud.provider.model.dto.oss.OptUploadFileRespDto)

Aggregations

Response (com.qiniu.http.Response)4 Gson (com.google.gson.Gson)2 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)2 OpcBizException (com.paascloud.provider.exceptions.OpcBizException)1 OptUploadFileRespDto (com.paascloud.provider.model.dto.oss.OptUploadFileRespDto)1 QiniuException (com.qiniu.common.QiniuException)1 BucketManager (com.qiniu.storage.BucketManager)1 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 BatchStatus (com.qiniu.storage.model.BatchStatus)1 Auth (com.qiniu.util.Auth)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Retryable (org.springframework.retry.annotation.Retryable)1