Search in sources :

Example 1 with ObsClient

use of com.obs.services.ObsClient in project alluxio by Alluxio.

the class OBSUnderFileSystem method createInstance.

/**
 * Constructs a new instance of {@link OBSUnderFileSystem}.
 *
 * @param uri the {@link AlluxioURI} for this UFS
 * @param conf the configuration for this UFS
 * @return the created {@link OBSUnderFileSystem} instance
 */
public static OBSUnderFileSystem createInstance(AlluxioURI uri, UnderFileSystemConfiguration conf) {
    Preconditions.checkArgument(conf.isSet(PropertyKey.OBS_ACCESS_KEY), "Property %s is required to connect to OBS", PropertyKey.OBS_ACCESS_KEY);
    Preconditions.checkArgument(conf.isSet(PropertyKey.OBS_SECRET_KEY), "Property %s is required to connect to OBS", PropertyKey.OBS_SECRET_KEY);
    Preconditions.checkArgument(conf.isSet(PropertyKey.OBS_ENDPOINT), "Property %s is required to connect to OBS", PropertyKey.OBS_ENDPOINT);
    Preconditions.checkArgument(conf.isSet(PropertyKey.OBS_BUCKET_TYPE), "Property %s is required to connect to OBS", PropertyKey.OBS_BUCKET_TYPE);
    String accessKey = conf.getString(PropertyKey.OBS_ACCESS_KEY);
    String secretKey = conf.getString(PropertyKey.OBS_SECRET_KEY);
    String endPoint = conf.getString(PropertyKey.OBS_ENDPOINT);
    String bucketType = conf.getString(PropertyKey.OBS_BUCKET_TYPE);
    ObsClient obsClient = new ObsClient(accessKey, secretKey, endPoint);
    String bucketName = UnderFileSystemUtils.getBucketName(uri);
    return new OBSUnderFileSystem(uri, obsClient, bucketName, bucketType, conf);
}
Also used : ObsClient(com.obs.services.ObsClient)

Example 2 with ObsClient

use of com.obs.services.ObsClient in project halo by ruibaby.

the class HuaweiObsFileHandler method upload.

@Override
@NonNull
public UploadResult upload(@NonNull MultipartFile file) {
    Assert.notNull(file, "Multipart file must not be null");
    // Get config
    String protocol = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_PROTOCOL).toString();
    String domain = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_DOMAIN, String.class, "");
    String source = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_SOURCE, String.class, "");
    String endPoint = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ENDPOINT).toString();
    String accessKey = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_KEY).toString();
    String accessSecret = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_SECRET).toString();
    String bucketName = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_BUCKET_NAME).toString();
    String styleRule = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_STYLE_RULE, String.class, "");
    String thumbnailStyleRule = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
    // Init OSS client
    final ObsClient obsClient = new ObsClient(accessKey, accessSecret, endPoint);
    StringBuilder basePath = new StringBuilder(protocol);
    if (StringUtils.isNotEmpty(domain)) {
        basePath.append(domain).append(URL_SEPARATOR);
    } else {
        basePath.append(bucketName).append(".").append(endPoint).append(URL_SEPARATOR);
    }
    try {
        FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(basePath.toString()).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.HUAWEIOBS) > 0).setOriginalName(file.getOriginalFilename()).build();
        log.info(basePath.toString());
        // Upload
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, pathDescriptor.getRelativePath(), file.getInputStream());
        if (putObjectResult == null) {
            throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到华为云失败 ");
        }
        // Response result
        UploadResult uploadResult = new UploadResult();
        uploadResult.setFilename(pathDescriptor.getName());
        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());
        handleImageMetadata(file, uploadResult, () -> {
            if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
                return fullPath;
            } else {
                return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
            }
        });
        log.info("Uploaded file: [{}] successfully", file.getOriginalFilename());
        return uploadResult;
    } catch (Exception e) {
        throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到华为云失败 ", e).setErrorData(file.getOriginalFilename());
    } finally {
        try {
            obsClient.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : PutObjectResult(com.obs.services.model.PutObjectResult) FileOperationException(run.halo.app.exception.FileOperationException) UploadResult(run.halo.app.model.support.UploadResult) IOException(java.io.IOException) ObsClient(com.obs.services.ObsClient) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException) NonNull(org.springframework.lang.NonNull)

Example 3 with ObsClient

use of com.obs.services.ObsClient in project halo by ruibaby.

the class HuaweiObsFileHandler method delete.

@Override
public void delete(@NonNull String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String endPoint = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ENDPOINT).toString();
    String accessKey = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_KEY).toString();
    String accessSecret = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_SECRET).toString();
    String bucketName = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_BUCKET_NAME).toString();
    // Init OSS client
    final ObsClient obsClient = new ObsClient(accessKey, accessSecret, endPoint);
    try {
        obsClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        throw new FileOperationException("附件 " + key + " 从华为云删除失败", e);
    } finally {
        try {
            obsClient.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException) ObsClient(com.obs.services.ObsClient) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException)

Example 4 with ObsClient

use of com.obs.services.ObsClient in project onex-boot by zhangchaoxu.

the class HuaweiCloudOssService method upload.

@Override
public String upload(String prefix, InputStream inputStream, String fileName) {
    String prefixTotal = StrUtil.isNotEmpty(config.getPrefix()) ? config.getPrefix() : "";
    if (StrUtil.isNotEmpty(prefix)) {
        if (StrUtil.isNotEmpty(prefixTotal)) {
            prefixTotal += "/" + prefix;
        } else {
            prefixTotal = prefix;
        }
    }
    String objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), false);
    ObsClient ossClient = null;
    try {
        ossClient = new ObsClient(config.getAccessKeyId(), config.getAccessKeySecret(), config.getEndPoint());
        if (ossClient.doesObjectExist(config.getBucketName(), objectKey)) {
            // 文件已存在,则需要对文件重命名
            objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), true);
        }
        ossClient.putObject(config.getBucketName(), objectKey, inputStream);
    } catch (ObsException e) {
        throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
    } finally {
        // ObsClient在调用ObsClient.close方法关闭后不能再次使用
        if (ossClient != null) {
            try {
                ossClient.close();
            } catch (IOException e) {
                log.error("huaweicloud obs close error", e);
            }
        }
    }
    return config.getDomain() + objectKey;
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) ObsException(com.obs.services.exception.ObsException) ObsClient(com.obs.services.ObsClient)

Example 5 with ObsClient

use of com.obs.services.ObsClient in project halo by halo-dev.

the class HuaweiObsFileHandler method upload.

@Override
@NonNull
public UploadResult upload(@NonNull MultipartFile file) {
    Assert.notNull(file, "Multipart file must not be null");
    // Get config
    String protocol = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_PROTOCOL).toString();
    String domain = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_DOMAIN, String.class, "");
    String source = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_SOURCE, String.class, "");
    String endPoint = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ENDPOINT).toString();
    String accessKey = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_KEY).toString();
    String accessSecret = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_ACCESS_SECRET).toString();
    String bucketName = optionService.getByPropertyOfNonNull(HuaweiObsProperties.OSS_BUCKET_NAME).toString();
    String styleRule = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_STYLE_RULE, String.class, "");
    String thumbnailStyleRule = optionService.getByPropertyOrDefault(HuaweiObsProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");
    // Init OSS client
    final ObsClient obsClient = new ObsClient(accessKey, accessSecret, endPoint);
    StringBuilder basePath = new StringBuilder(protocol);
    if (StringUtils.isNotEmpty(domain)) {
        basePath.append(domain).append(URL_SEPARATOR);
    } else {
        basePath.append(bucketName).append(".").append(endPoint).append(URL_SEPARATOR);
    }
    try {
        FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(basePath.toString()).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.HUAWEIOBS) > 0).setOriginalName(file.getOriginalFilename()).build();
        log.info(basePath.toString());
        // Upload
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, pathDescriptor.getRelativePath(), file.getInputStream());
        if (putObjectResult == null) {
            throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到华为云失败 ");
        }
        // Response result
        UploadResult uploadResult = new UploadResult();
        uploadResult.setFilename(pathDescriptor.getName());
        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());
        handleImageMetadata(file, uploadResult, () -> {
            if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
                return fullPath;
            } else {
                return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
            }
        });
        log.info("Uploaded file: [{}] successfully", file.getOriginalFilename());
        return uploadResult;
    } catch (Exception e) {
        throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到华为云失败 ", e).setErrorData(file.getOriginalFilename());
    } finally {
        try {
            obsClient.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : PutObjectResult(com.obs.services.model.PutObjectResult) FileOperationException(run.halo.app.exception.FileOperationException) UploadResult(run.halo.app.model.support.UploadResult) IOException(java.io.IOException) ObsClient(com.obs.services.ObsClient) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException) NonNull(org.springframework.lang.NonNull)

Aggregations

ObsClient (com.obs.services.ObsClient)8 IOException (java.io.IOException)4 FileOperationException (run.halo.app.exception.FileOperationException)4 OnexException (com.nb6868.onex.common.exception.OnexException)2 ObsException (com.obs.services.exception.ObsException)2 PutObjectResult (com.obs.services.model.PutObjectResult)2 NonNull (org.springframework.lang.NonNull)2 UploadResult (run.halo.app.model.support.UploadResult)2 ObsObject (com.obs.services.model.ObsObject)1 ServiceException (com.zhouzifei.tool.common.ServiceException)1