Search in sources :

Example 1 with ObsException

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);
    }
}
Also used : ObsException(com.obs.services.exception.ObsException) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) ObjectMetadata(com.obs.services.model.ObjectMetadata) FileInputStream(java.io.FileInputStream)

Example 2 with ObsException

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;
    }
}
Also used : ObsException(com.obs.services.exception.ObsException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.obs.services.model.ObjectMetadata)

Example 3 with ObsException

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;
    }
}
Also used : ObsException(com.obs.services.exception.ObsException) ObjectMetadata(com.obs.services.model.ObjectMetadata) Date(java.util.Date)

Example 4 with ObsException

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;
    }
}
Also used : ObsException(com.obs.services.exception.ObsException) RenameRequest(com.obs.services.model.fs.RenameRequest) RenameResult(com.obs.services.model.fs.RenameResult)

Example 5 with ObsException

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;
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) ObsException(com.obs.services.exception.ObsException) ObsClient(com.obs.services.ObsClient)

Aggregations

ObsException (com.obs.services.exception.ObsException)8 ObjectMetadata (com.obs.services.model.ObjectMetadata)4 BufferedInputStream (java.io.BufferedInputStream)3 OnexException (com.nb6868.onex.common.exception.OnexException)2 ObsClient (com.obs.services.ObsClient)2 ObsObject (com.obs.services.model.ObsObject)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 GetObjectRequest (com.obs.services.model.GetObjectRequest)1 RenameRequest (com.obs.services.model.fs.RenameRequest)1 RenameResult (com.obs.services.model.fs.RenameResult)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1