Search in sources :

Example 1 with PutObjectResponse

use of com.baidubce.services.bos.model.PutObjectResponse 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 PutObjectResponse

use of com.baidubce.services.bos.model.PutObjectResponse in project halo by halo-dev.

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 3 with PutObjectResponse

use of com.baidubce.services.bos.model.PutObjectResponse in project halo-plugin-experimental by guqing.

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)

Aggregations

DefaultBceCredentials (com.baidubce.auth.DefaultBceCredentials)3 BosClient (com.baidubce.services.bos.BosClient)3 BosClientConfiguration (com.baidubce.services.bos.BosClientConfiguration)3 PutObjectResponse (com.baidubce.services.bos.model.PutObjectResponse)3 FileOperationException (run.halo.app.exception.FileOperationException)3 UploadResult (run.halo.app.model.support.UploadResult)3