Search in sources :

Example 1 with AccessControlList

use of com.obs.services.model.AccessControlList in project jeesuite-libs by vakinge.

the class HuaweicloudProvider method isBucketPrivate.

public boolean isBucketPrivate(String bucketName) {
    if (!existsBucket(bucketName)) {
        throw new RuntimeException("bucket[" + bucketName + "]不存在");
    }
    AccessControlList acl = obsClient.getBucketAcl(bucketName);
    Set<GrantAndPermission> grants = acl.getGrants();
    if (grants != null) {
        for (GrantAndPermission grant : grants) {
            if (grant.getGrantee().equals(GroupGrantee.ALL_USERS) && grant.getPermission().equals(Permission.PERMISSION_READ)) {
                return false;
            }
        }
    }
    return true;
}
Also used : AccessControlList(com.obs.services.model.AccessControlList) GrantAndPermission(com.obs.services.model.GrantAndPermission)

Example 2 with AccessControlList

use of com.obs.services.model.AccessControlList in project jeesuite-libs by vakinge.

the class HuaweicloudProvider method createBucket.

@Override
public void createBucket(String bucketName, boolean isPrivate) {
    if (existsBucket(bucketName)) {
        throw new RuntimeException("bucket[" + bucketName + "] 已经存在");
    }
    CreateBucketRequest request = new CreateBucketRequest(bucketName, conf.getRegionName());
    ObsBucket bucket = new ObsBucket();
    bucket.setBucketName(bucketName);
    AccessControlList acl = null;
    if (isPrivate) {
        acl = AccessControlList.REST_CANNED_PRIVATE;
    } else {
        acl = AccessControlList.REST_CANNED_PUBLIC_READ;
    }
    request.setAcl(acl);
    obsClient.createBucket(request);
}
Also used : AccessControlList(com.obs.services.model.AccessControlList) CreateBucketRequest(com.obs.services.model.CreateBucketRequest) ObsBucket(com.obs.services.model.ObsBucket)

Example 3 with AccessControlList

use of com.obs.services.model.AccessControlList in project jeesuite-libs by vakinge.

the class HuaweicloudProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    String bucketName = object.getBucketName();
    if (StringUtils.isBlank(bucketName)) {
        throw new JeesuiteBaseException("BucketName 不能为空");
    }
    InputStream inputStream = object.getInputStream();
    File file = object.getFile();
    String fileKey = object.getFileKey();
    byte[] bytes = object.getBytes();
    long size = 0;
    logger.info("bucknetName={}, fileKey={}", bucketName, fileKey);
    PutObjectResult putObjectResult = null;
    try {
        if (file != null) {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(object.getMimeType());
            putObjectResult = obsClient.putObject(bucketName, fileKey, file, metadata);
            size = file.length();
        } else if (bytes != null) {
            ByteArrayInputStream input = new ByteArrayInputStream(bytes);
            putObjectResult = obsClient.putObject(bucketName, fileKey, input);
            size = bytes.length;
            input.close();
        } else if (inputStream != null) {
            putObjectResult = obsClient.putObject(bucketName, fileKey, inputStream);
            size = inputStream.available();
        } else {
            throw new JeesuiteBaseException("upload object is NULL");
        }
        if (putObjectResult != null) {
            AccessControlList acl = new AccessControlList();
            if (!isBucketPrivate(bucketName)) {
                acl = AccessControlList.REST_CANNED_PUBLIC_READ;
            }
            obsClient.setObjectAcl(bucketName, fileKey, acl);
            CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(object.getBucketName(), fileKey, 300), null);
            uploadResult.setMimeType(object.getMimeType());
            uploadResult.setFileSize(size);
            return uploadResult;
        }
    } catch (Exception e) {
        logger.error("上传文件出错, bucketName={}, fileKey={}, e={}", bucketName, fileKey, ExceptionUtils.getMessage(e));
        throw new JeesuiteBaseException(e.getMessage());
    }
    return null;
}
Also used : AccessControlList(com.obs.services.model.AccessControlList) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) PutObjectResult(com.obs.services.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) File(java.io.File) CObjectMetadata(com.mendmix.cos.CObjectMetadata) ObjectMetadata(com.obs.services.model.ObjectMetadata) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Aggregations

AccessControlList (com.obs.services.model.AccessControlList)3 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)1 CObjectMetadata (com.mendmix.cos.CObjectMetadata)1 CUploadResult (com.mendmix.cos.CUploadResult)1 CreateBucketRequest (com.obs.services.model.CreateBucketRequest)1 GrantAndPermission (com.obs.services.model.GrantAndPermission)1 ObjectMetadata (com.obs.services.model.ObjectMetadata)1 ObsBucket (com.obs.services.model.ObsBucket)1 PutObjectResult (com.obs.services.model.PutObjectResult)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1