Search in sources :

Example 6 with ObjectMetadata

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;
    }
}
Also used : ServiceException(com.aliyun.oss.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata)

Example 7 with ObjectMetadata

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();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with ObjectMetadata

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);
    }
}
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 9 with ObjectMetadata

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);
    }
}
Also used : ObjectMetadata(com.aliyun.oss.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)9 FileInputStream (java.io.FileInputStream)4 OSSException (com.aliyun.oss.OSSException)3 ClientException (com.aliyun.oss.ClientException)2 ServiceException (com.aliyun.oss.ServiceException)2 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)2 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)2 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)2 InitiateMultipartUploadRequest (com.aliyun.oss.model.InitiateMultipartUploadRequest)2 InitiateMultipartUploadResult (com.aliyun.oss.model.InitiateMultipartUploadResult)2 PartETag (com.aliyun.oss.model.PartETag)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ObjectListing (com.aliyun.oss.model.ObjectListing)1 PutObjectResult (com.aliyun.oss.model.PutObjectResult)1 UploadPartCopyRequest (com.aliyun.oss.model.UploadPartCopyRequest)1 UploadPartCopyResult (com.aliyun.oss.model.UploadPartCopyResult)1 UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)1