Search in sources :

Example 6 with ObsException

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

the class HuaweiCloudOssService method download.

@Override
public InputStream download(String objectKey) {
    ObsClient ossClient = null;
    ObsObject ossObject = null;
    try {
        ossClient = new ObsClient(config.getAccessKeyId(), config.getAccessKeySecret(), config.getEndPoint());
        ossObject = ossClient.getObject(config.getBucketName(), objectKey);
        return ossObject.getObjectContent();
    } 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 client close error", e);
            }
        }
    }
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) ObsException(com.obs.services.exception.ObsException) ObsObject(com.obs.services.model.ObsObject) ObsClient(com.obs.services.ObsClient)

Example 7 with ObsException

use of com.obs.services.exception.ObsException in project alluxio by Alluxio.

the class OBSInputStream method createStream.

@Override
protected InputStream createStream(long startPos, long endPos) throws IOException {
    GetObjectRequest req = new GetObjectRequest(mBucketName, mKey);
    req.setRangeStart(startPos);
    req.setRangeEnd(endPos < mContentLength ? endPos - 1 : mContentLength - 1);
    ObsException lastException = null;
    while (mRetryPolicy.attempt()) {
        try {
            ObsObject obj = mObsClient.getObject(req);
            return new BufferedInputStream(obj.getObjectContent());
        } catch (ObsException e) {
            System.out.println(e.getResponseCode());
            LOG.warn("Attempt {} to open key {} in bucket {} failed with exception : {}", mRetryPolicy.getAttemptCount(), mKey, mBucketName, e.toString());
            if (e.getResponseCode() != HttpStatus.SC_NOT_FOUND) {
                throw new IOException(e);
            }
            // Key does not exist
            lastException = e;
        }
    }
    // Failed after retrying key does not exist
    throw new IOException(lastException);
}
Also used : ObsException(com.obs.services.exception.ObsException) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) ObsObject(com.obs.services.model.ObsObject) GetObjectRequest(com.obs.services.model.GetObjectRequest)

Example 8 with ObsException

use of com.obs.services.exception.ObsException in project alluxio by Alluxio.

the class OBSOutputStreamTest method testCloseError.

/**
 * Tests to ensure IOException is thrown if
 * {@link ObsClient#putObject(String, String, InputStream, ObjectMetadata)} throws an
 * {@link ObsException}.
 */
@Test
@PrepareForTest(OBSOutputStream.class)
public void testCloseError() throws Exception {
    String errorMessage = "Invoke the createEmptyObject method error.";
    BufferedInputStream inputStream = PowerMockito.mock(BufferedInputStream.class);
    PowerMockito.whenNew(BufferedInputStream.class).withArguments(Mockito.any(FileInputStream.class)).thenReturn(inputStream);
    PowerMockito.when(mObsClient.putObject(Mockito.anyString(), Mockito.anyString(), Mockito.any(InputStream.class), Mockito.any(ObjectMetadata.class))).thenThrow(new ObsException(errorMessage));
    OBSOutputStream stream = new OBSOutputStream("testBucketName", "testKey", mObsClient, sConf.getList(PropertyKey.TMP_DIRS));
    mThrown.expect(IOException.class);
    mThrown.expectMessage(errorMessage);
    stream.close();
}
Also used : ObsException(com.obs.services.exception.ObsException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ObjectMetadata(com.obs.services.model.ObjectMetadata) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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