use of com.obs.services.model.ObjectMetadata in project jeesuite-libs by vakinge.
the class HuaweicloudProvider method upload.
@Override
public CUploadResult upload(CUploadObject object) {
String bucketName = object.getBucketName();
if (StringUtils.isBlank(bucketName)) {
throw new JeesuiteBaseException("BucketName 不能为空");
}
InputStream inputStream = object.getInputStream();
File file = object.getFile();
String fileKey = object.getFileKey();
byte[] bytes = object.getBytes();
long size = 0;
logger.info("bucknetName={}, fileKey={}", bucketName, fileKey);
PutObjectResult putObjectResult = null;
try {
if (file != null) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(object.getMimeType());
putObjectResult = obsClient.putObject(bucketName, fileKey, file, metadata);
size = file.length();
} else if (bytes != null) {
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
putObjectResult = obsClient.putObject(bucketName, fileKey, input);
size = bytes.length;
input.close();
} else if (inputStream != null) {
putObjectResult = obsClient.putObject(bucketName, fileKey, inputStream);
size = inputStream.available();
} else {
throw new JeesuiteBaseException("upload object is NULL");
}
if (putObjectResult != null) {
AccessControlList acl = new AccessControlList();
if (!isBucketPrivate(bucketName)) {
acl = AccessControlList.REST_CANNED_PUBLIC_READ;
}
obsClient.setObjectAcl(bucketName, fileKey, acl);
CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(object.getBucketName(), fileKey, 300), null);
uploadResult.setMimeType(object.getMimeType());
uploadResult.setFileSize(size);
return uploadResult;
}
} catch (Exception e) {
logger.error("上传文件出错, bucketName={}, fileKey={}, e={}", bucketName, fileKey, ExceptionUtils.getMessage(e));
throw new JeesuiteBaseException(e.getMessage());
}
return null;
}
use of com.obs.services.model.ObjectMetadata 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();
}
use of com.obs.services.model.ObjectMetadata in project alluxio by Alluxio.
the class OBSUnderFileSystemTest method judgeDirectoryInBucket.
@Test
public void judgeDirectoryInBucket() throws Exception {
ObjectMetadata fileMeta = new ObjectMetadata();
fileMeta.setLastModified(new Date());
fileMeta.getMetadata().put("mode", 33152);
fileMeta.setContentLength(10L);
ObjectMetadata dirMeta = new ObjectMetadata();
dirMeta.setLastModified(new Date());
dirMeta.getMetadata().put("mode", 16877);
dirMeta.setContentLength(0L);
/**
* /xx/file1/ ( File1 actually exists, which is a file) , there is / after file1 name.
* When OBS, the path object meta is null.
* When PFS, the path object meta is not null. The object meta is same as /xx/file1
*/
Mockito.when(mClient.getObjectMetadata(BUCKET_NAME, "pfs_file1")).thenReturn(fileMeta);
Mockito.when(mClient.getObjectMetadata(BUCKET_NAME, "pfs_file1/")).thenReturn(fileMeta);
Mockito.when(mClient.getObjectMetadata(BUCKET_NAME, "obs_file1")).thenReturn(fileMeta);
Mockito.when(mClient.getObjectMetadata(BUCKET_NAME, "obs_file1/")).thenReturn(null);
Mockito.when(mClient.getObjectMetadata(BUCKET_NAME, "dir1")).thenReturn(dirMeta);
Mockito.when(mClient.getObjectMetadata(BUCKET_NAME, "dir1/")).thenReturn(dirMeta);
// PFS Bucket
mOBSUnderFileSystem = new OBSUnderFileSystem(new AlluxioURI(""), mClient, BUCKET_NAME, "pfs", UnderFileSystemConfiguration.defaults(ConfigurationTestUtils.defaults()));
Assert.assertNotNull(mOBSUnderFileSystem.getObjectStatus("pfs_file1"));
Assert.assertNull(mOBSUnderFileSystem.getObjectStatus("pfs_file1/"));
Assert.assertNull(mOBSUnderFileSystem.getObjectStatus("dir1"));
Assert.assertTrue(mOBSUnderFileSystem.isDirectory("dir1"));
Assert.assertFalse(mOBSUnderFileSystem.isDirectory("pfs_file1"));
// OBS Bucket
mOBSUnderFileSystem = new OBSUnderFileSystem(new AlluxioURI(""), mClient, BUCKET_NAME, "obs", UnderFileSystemConfiguration.defaults(ConfigurationTestUtils.defaults()));
Mockito.when(mClient.getObjectMetadata(BUCKET_NAME, "dir1")).thenReturn(null);
Assert.assertNotNull(mOBSUnderFileSystem.getObjectStatus("obs_file1"));
Assert.assertNull(mOBSUnderFileSystem.getObjectStatus("obs_file1/"));
Assert.assertNull(mOBSUnderFileSystem.getObjectStatus("dir1"));
Assert.assertTrue(mOBSUnderFileSystem.isDirectory("dir1"));
Assert.assertFalse(mOBSUnderFileSystem.isDirectory("obs_file1"));
}
Aggregations