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;
}
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);
}
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;
}
Aggregations