Search in sources :

Example 6 with ServiceException

use of com.aliyun.oss.ServiceException in project alluxio by Alluxio.

the class OSSOutputStream 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)) {
        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)));
        }
        mOssClient.putObject(mBucketName, mKey, in, objMeta);
    } catch (ServiceException e) {
        LOG.error("Failed to upload {}.", mKey);
        throw new IOException(e);
    } finally {
        // upload or if the upload failed.
        if (!mFile.delete()) {
            LOG.error("Failed to delete temporary file @ {}", mFile.getPath());
        }
    }
}
Also used : ServiceException(com.aliyun.oss.ServiceException) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) FileInputStream(java.io.FileInputStream)

Example 7 with ServiceException

use of com.aliyun.oss.ServiceException in project alluxio by Alluxio.

the class OSSUnderFileSystem method getObjectStatus.

@Override
protected ObjectStatus getObjectStatus(String key) {
    try {
        ObjectMetadata meta = mClient.getObjectMetadata(mBucketName, key);
        if (meta == null) {
            return null;
        }
        Date lastModifiedDate = meta.getLastModified();
        Long lastModifiedTime = lastModifiedDate == null ? null : lastModifiedDate.getTime();
        return new ObjectStatus(key, meta.getETag(), meta.getContentLength(), lastModifiedTime);
    } catch (ServiceException e) {
        return null;
    }
}
Also used : ServiceException(com.aliyun.oss.ServiceException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) Date(java.util.Date)

Aggregations

ServiceException (com.aliyun.oss.ServiceException)7 ClientException (com.aliyun.oss.ClientException)4 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)3 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)3 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)3 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ClientConfiguration (com.aliyun.oss.ClientConfiguration)1 RequestSigner (com.aliyun.oss.common.auth.RequestSigner)1 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)1 LogUtils.logException (com.aliyun.oss.common.utils.LogUtils.logException)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 Date (java.util.Date)1 Test (org.junit.Test)1