use of com.obs.services.exception.ObsException in project alluxio by Alluxio.
the class OBSOutputStream method close.
/**
* Closes this output stream. When an output stream is closed, the local temporary file is
* uploaded to OSS Service. Once the file is uploaded, the temporary file is deleted.
*/
@Override
public void close() throws IOException {
if (mClosed.getAndSet(true)) {
LOG.warn("OBSOutputStream is already closed");
return;
}
mLocalOutputStream.close();
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile));
ObjectMetadata objMeta = new ObjectMetadata();
objMeta.setContentLength(mFile.length());
if (mHash != null) {
byte[] hashBytes = mHash.digest();
objMeta.setContentMd5(new String(Base64.encodeBase64(hashBytes)));
}
mObsClient.putObject(mBucketName, mKey, in, objMeta);
mFile.delete();
} catch (ObsException e) {
LOG.error("Failed to upload {}. Temporary file @ {}", mKey, mFile.getPath());
throw new IOException(e);
}
}
use of com.obs.services.exception.ObsException in project alluxio by Alluxio.
the class OBSUnderFileSystem method createEmptyObject.
@Override
public boolean createEmptyObject(String key) {
try {
ObjectMetadata objMeta = new ObjectMetadata();
objMeta.setContentLength(0L);
mClient.putObject(mBucketName, key, new ByteArrayInputStream(new byte[0]), objMeta);
return true;
} catch (ObsException e) {
LOG.error("Failed to create object: {}", key, e);
return false;
}
}
use of com.obs.services.exception.ObsException in project alluxio by Alluxio.
the class OBSUnderFileSystem method getObjectStatus.
@Override
protected ObjectStatus getObjectStatus(String key) {
try {
ObjectMetadata meta = mClient.getObjectMetadata(mBucketName, key);
if (meta == null) {
return null;
}
if (isEnvironmentPFS()) {
/**
* When in PFS environment:
* 1. Directory will be explicitly created and have object meta.
* 2. File will have object meta even if there is `/` at
* the end of the file name (e.g. `/dir1/file1/`).
* However we should return null meta here.
*/
if (isDirectoryInPFS(meta)) {
return null;
}
if (!isDirectoryInPFS(meta) && key.endsWith(PATH_SEPARATOR)) {
return null;
}
}
Date lastModifiedDate = meta.getLastModified();
Long lastModifiedTime = lastModifiedDate == null ? null : lastModifiedDate.getTime();
return new ObjectStatus(key, meta.getEtag(), meta.getContentLength(), lastModifiedTime);
} catch (ObsException e) {
LOG.warn("Failed to get Object {}, return null", key, e);
return null;
}
}
use of com.obs.services.exception.ObsException in project alluxio by Alluxio.
the class OBSUnderFileSystem method renameDirectory.
@Override
public boolean renameDirectory(String src, String dst) throws IOException {
if (!isEnvironmentPFS()) {
return super.renameDirectory(src, dst);
}
try {
RenameRequest request = new RenameRequest(mBucketName, stripPrefixIfPresent(src), stripPrefixIfPresent(dst));
RenameResult response = mClient.renameFolder(request);
if (isSuccessResponse(response.getStatusCode())) {
return true;
} else {
LOG.error("Failed to rename directory from {} to {}.", src, dst);
return false;
}
} catch (ObsException e) {
LOG.error("Failed to rename directory from {} to {}.", src, dst, e);
return false;
}
}
use of com.obs.services.exception.ObsException 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;
}
Aggregations