Search in sources :

Example 1 with DefaultBceCredentials

use of com.baidubce.auth.DefaultBceCredentials in project halo by ruibaby.

the class BaiduBosFileHandler method upload.

@Override
public UploadResult upload(MultipartFile file) {
    Assert.notNull(file, "Multipart file must not be null");
    // Get config
    Object protocol = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_PROTOCOL);
    String domain = optionService.getByPropertyOrDefault(BaiduBosProperties.BOS_DOMAIN, String.class, "");
    String endPoint = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ENDPOINT).toString();
    String accessKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ACCESS_KEY).toString();
    String secretKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_SECRET_KEY).toString();
    String bucketName = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_BUCKET_NAME).toString();
    String styleRule = optionService.getByPropertyOrDefault(BaiduBosProperties.BOS_STYLE_RULE, String.class, "");
    String thumbnailStyleRule = optionService.getByPropertyOrDefault(BaiduBosProperties.BOS_THUMBNAIL_STYLE_RULE, String.class, "");
    String source = StringUtils.join(protocol, bucketName, "." + endPoint);
    BosClientConfiguration config = new BosClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
    config.setEndpoint(endPoint);
    // Init OSS client
    BosClient client = new BosClient(config);
    domain = protocol + domain;
    try {
        FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(domain).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.BAIDUBOS) > 0).setOriginalName(file.getOriginalFilename()).build();
        // Upload
        PutObjectResponse putObjectResponseFromInputStream = client.putObject(bucketName, pathDescriptor.getFullName(), file.getInputStream());
        if (putObjectResponseFromInputStream == null) {
            throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到百度云失败 ");
        }
        // Response result
        UploadResult uploadResult = new UploadResult();
        uploadResult.setFilename(pathDescriptor.getFullName());
        String fullPath = pathDescriptor.getFullPath();
        uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule);
        uploadResult.setKey(pathDescriptor.getRelativePath());
        uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
        uploadResult.setSuffix(pathDescriptor.getExtension());
        uploadResult.setSize(file.getSize());
        // Handle thumbnail
        handleImageMetadata(file, uploadResult, () -> {
            if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
                return fullPath;
            } else {
                return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
            }
        });
        return uploadResult;
    } catch (Exception e) {
        throw new FileOperationException("附件 " + file.getOriginalFilename() + " 上传失败(百度云)", e);
    } finally {
        client.shutdown();
    }
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials) PutObjectResponse(com.baidubce.services.bos.model.PutObjectResponse) FileOperationException(run.halo.app.exception.FileOperationException) BosClient(com.baidubce.services.bos.BosClient) UploadResult(run.halo.app.model.support.UploadResult) FileOperationException(run.halo.app.exception.FileOperationException)

Example 2 with DefaultBceCredentials

use of com.baidubce.auth.DefaultBceCredentials in project halo by ruibaby.

the class BaiduBosFileHandler method delete.

@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String endPoint = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ENDPOINT).toString();
    String accessKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ACCESS_KEY).toString();
    String secretKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_SECRET_KEY).toString();
    String bucketName = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_BUCKET_NAME).toString();
    BosClientConfiguration config = new BosClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
    config.setEndpoint(endPoint);
    // Init OSS client
    BosClient client = new BosClient(config);
    try {
        client.deleteObject(bucketName, key);
    } catch (Exception e) {
        throw new FileOperationException("附件 " + key + " 从百度云删除失败", e);
    } finally {
        client.shutdown();
    }
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials) FileOperationException(run.halo.app.exception.FileOperationException) BosClient(com.baidubce.services.bos.BosClient) FileOperationException(run.halo.app.exception.FileOperationException)

Example 3 with DefaultBceCredentials

use of com.baidubce.auth.DefaultBceCredentials in project simpleFS by shengdingbox.

the class BaiduBosApiClient method check.

@Override
protected void check() {
    if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey) || StringUtils.isNullOrEmpty(bucketName)) {
        throw new ServiceException("[" + this.storageType + "]尚未配置,文件上传功能暂时不可用!");
    }
    BosClientConfiguration config = new BosClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
    config.setEndpoint(endPoint);
    config.setProtocol(Protocol.HTTPS);
    this.bos = new BosClient(config);
    boolean bucketExist = bos.doesBucketExist(bucketName);
    if (!bucketExist) {
        bos.createBucket(bucketName);
    }
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) ServiceException(com.zhouzifei.tool.common.ServiceException) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials) BosClient(com.baidubce.services.bos.BosClient)

Example 4 with DefaultBceCredentials

use of com.baidubce.auth.DefaultBceCredentials in project oss-spring-boot-starter by ArtIsLong.

the class BaiduOssConfiguration method bosClientConfiguration.

public BosClientConfiguration bosClientConfiguration(BaiduOssConfig baiduOssConfig) {
    BosClientConfiguration config = new BosClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(baiduOssConfig.getAccessKeyId(), baiduOssConfig.getSecretAccessKey()));
    config.setEndpoint(baiduOssConfig.getEndPoint());
    return config;
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials)

Example 5 with DefaultBceCredentials

use of com.baidubce.auth.DefaultBceCredentials in project halo by halo-dev.

the class BaiduBosFileHandler method delete.

@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String endPoint = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ENDPOINT).toString();
    String accessKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ACCESS_KEY).toString();
    String secretKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_SECRET_KEY).toString();
    String bucketName = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_BUCKET_NAME).toString();
    BosClientConfiguration config = new BosClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
    config.setEndpoint(endPoint);
    // Init OSS client
    BosClient client = new BosClient(config);
    try {
        client.deleteObject(bucketName, key);
    } catch (Exception e) {
        throw new FileOperationException("附件 " + key + " 从百度云删除失败", e);
    } finally {
        client.shutdown();
    }
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials) FileOperationException(run.halo.app.exception.FileOperationException) BosClient(com.baidubce.services.bos.BosClient) FileOperationException(run.halo.app.exception.FileOperationException)

Aggregations

DefaultBceCredentials (com.baidubce.auth.DefaultBceCredentials)8 BosClientConfiguration (com.baidubce.services.bos.BosClientConfiguration)8 BosClient (com.baidubce.services.bos.BosClient)7 FileOperationException (run.halo.app.exception.FileOperationException)6 PutObjectResponse (com.baidubce.services.bos.model.PutObjectResponse)3 UploadResult (run.halo.app.model.support.UploadResult)3 ServiceException (com.zhouzifei.tool.common.ServiceException)1