use of com.aliyun.oss.model.ObjectMetadata in project alluxio by Alluxio.
the class OSSUnderFileSystem method createEmptyObject.
@Override
protected boolean createEmptyObject(String key) {
try {
ObjectMetadata objMeta = new ObjectMetadata();
objMeta.setContentLength(0);
mClient.putObject(mBucketName, key, new ByteArrayInputStream(new byte[0]), objMeta);
return true;
} catch (ServiceException e) {
LOG.error("Failed to create object: {}", key, e);
return false;
}
}
use of com.aliyun.oss.model.ObjectMetadata in project alluxio by Alluxio.
the class OSSOutputStreamTest method testCloseError.
/**
* Tests to ensure IOException is thrown if
* {@link OSSClient#putObject(String, String, InputStream, ObjectMetadata)} throws an
* OSSException.
*
* @throws Exception when the IOException is thrown
*/
@Test
@PrepareForTest(OSSOutputStream.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(mOssClient.putObject(Mockito.anyString(), Mockito.anyString(), Mockito.any(InputStream.class), Mockito.any(ObjectMetadata.class))).thenThrow(new OSSException(errorMessage));
OSSOutputStream stream = new OSSOutputStream("testBucketName", "testKey", mOssClient);
mThrown.expect(IOException.class);
mThrown.expectMessage(errorMessage);
stream.close();
}
use of com.aliyun.oss.model.ObjectMetadata 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.
*
* @throws IOException if an I/O error occurs
*/
@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);
mFile.delete();
} catch (ServiceException e) {
LOG.error("Failed to upload {}. Temporary file @ {}", mKey, mFile.getPath());
throw new IOException(e);
}
}
use of com.aliyun.oss.model.ObjectMetadata in project hadoop by apache.
the class AliyunOSSFileSystemStore method copyFile.
/**
* Copy an object from source key to destination key.
*
* @param srcKey source key.
* @param dstKey destination key.
* @return true if file is successfully copied.
*/
public boolean copyFile(String srcKey, String dstKey) {
ObjectMetadata objectMeta = ossClient.getObjectMetadata(bucketName, srcKey);
long contentLength = objectMeta.getContentLength();
if (contentLength <= multipartThreshold) {
return singleCopy(srcKey, dstKey);
} else {
return multipartCopy(srcKey, contentLength, dstKey);
}
}
Aggregations