Search in sources :

Example 6 with ObjectMetadata

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;
}
Also used : AccessControlList(com.obs.services.model.AccessControlList) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) PutObjectResult(com.obs.services.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) File(java.io.File) CObjectMetadata(com.mendmix.cos.CObjectMetadata) ObjectMetadata(com.obs.services.model.ObjectMetadata) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 7 with ObjectMetadata

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();
}
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)

Example 8 with ObjectMetadata

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"));
}
Also used : ObjectMetadata(com.obs.services.model.ObjectMetadata) Date(java.util.Date) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

ObjectMetadata (com.obs.services.model.ObjectMetadata)8 ObsException (com.obs.services.exception.ObsException)4 Test (org.junit.Test)3 AlluxioURI (alluxio.AlluxioURI)2 CObjectMetadata (com.mendmix.cos.CObjectMetadata)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)1 CUploadObject (com.mendmix.cos.CUploadObject)1 CUploadResult (com.mendmix.cos.CUploadResult)1 AccessControlList (com.obs.services.model.AccessControlList)1 ObsObject (com.obs.services.model.ObsObject)1 PutObjectResult (com.obs.services.model.PutObjectResult)1 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1