Search in sources :

Example 1 with OSSException

use of com.aliyun.oss.OSSException in project hadoop by apache.

the class AliyunOSSFileSystemStore method multipartUploadObject.

/**
   * Upload a file as an OSS object, using multipart upload.
   *
   * @param key object key.
   * @param file local file to upload.
   * @throws IOException if failed to upload object.
   */
public void multipartUploadObject(String key, File file) throws IOException {
    File object = file.getAbsoluteFile();
    long dataLen = object.length();
    long realPartSize = AliyunOSSUtils.calculatePartSize(dataLen, partSize);
    int partNum = (int) (dataLen / realPartSize);
    if (dataLen % realPartSize != 0) {
        partNum += 1;
    }
    InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
    ObjectMetadata meta = new ObjectMetadata();
    if (StringUtils.isNotEmpty(serverSideEncryptionAlgorithm)) {
        meta.setServerSideEncryption(serverSideEncryptionAlgorithm);
    }
    initiateMultipartUploadRequest.setObjectMetadata(meta);
    InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
    List<PartETag> partETags = new ArrayList<PartETag>();
    String uploadId = initiateMultipartUploadResult.getUploadId();
    try {
        for (int i = 0; i < partNum; i++) {
            // TODO: Optimize this, avoid opening the object multiple times
            FileInputStream fis = new FileInputStream(object);
            try {
                long skipBytes = realPartSize * i;
                AliyunOSSUtils.skipFully(fis, skipBytes);
                long size = (realPartSize < dataLen - skipBytes) ? realPartSize : dataLen - skipBytes;
                UploadPartRequest uploadPartRequest = new UploadPartRequest();
                uploadPartRequest.setBucketName(bucketName);
                uploadPartRequest.setKey(key);
                uploadPartRequest.setUploadId(uploadId);
                uploadPartRequest.setInputStream(fis);
                uploadPartRequest.setPartSize(size);
                uploadPartRequest.setPartNumber(i + 1);
                UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
                statistics.incrementWriteOps(1);
                partETags.add(uploadPartResult.getPartETag());
            } finally {
                fis.close();
            }
        }
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        LOG.debug(completeMultipartUploadResult.getETag());
    } catch (OSSException | ClientException e) {
        AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
        ossClient.abortMultipartUpload(abortMultipartUploadRequest);
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) OSSException(com.aliyun.oss.OSSException) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) FileInputStream(java.io.FileInputStream) UploadPartResult(com.aliyun.oss.model.UploadPartResult) ClientException(com.aliyun.oss.ClientException) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest)

Example 2 with OSSException

use of com.aliyun.oss.OSSException in project hadoop by apache.

the class AliyunOSSFileSystemStore method multipartCopy.

/**
   * Use multipart copy to copy an OSS object.
   * (The caller should make sure srcPath is a file and dstPath is valid)
   *
   * @param srcKey source key.
   * @param contentLength data size of the object to copy.
   * @param dstKey destination key.
   * @return true if success, or false if upload is aborted.
   */
private boolean multipartCopy(String srcKey, long contentLength, String dstKey) {
    long realPartSize = AliyunOSSUtils.calculatePartSize(contentLength, uploadPartSize);
    int partNum = (int) (contentLength / realPartSize);
    if (contentLength % realPartSize != 0) {
        partNum++;
    }
    InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, dstKey);
    ObjectMetadata meta = new ObjectMetadata();
    if (StringUtils.isNotEmpty(serverSideEncryptionAlgorithm)) {
        meta.setServerSideEncryption(serverSideEncryptionAlgorithm);
    }
    initiateMultipartUploadRequest.setObjectMetadata(meta);
    InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
    String uploadId = initiateMultipartUploadResult.getUploadId();
    List<PartETag> partETags = new ArrayList<PartETag>();
    try {
        for (int i = 0; i < partNum; i++) {
            long skipBytes = realPartSize * i;
            long size = (realPartSize < contentLength - skipBytes) ? realPartSize : contentLength - skipBytes;
            UploadPartCopyRequest partCopyRequest = new UploadPartCopyRequest();
            partCopyRequest.setSourceBucketName(bucketName);
            partCopyRequest.setSourceKey(srcKey);
            partCopyRequest.setBucketName(bucketName);
            partCopyRequest.setKey(dstKey);
            partCopyRequest.setUploadId(uploadId);
            partCopyRequest.setPartSize(size);
            partCopyRequest.setBeginIndex(skipBytes);
            partCopyRequest.setPartNumber(i + 1);
            UploadPartCopyResult partCopyResult = ossClient.uploadPartCopy(partCopyRequest);
            statistics.incrementWriteOps(1);
            partETags.add(partCopyResult.getPartETag());
        }
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, dstKey, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        LOG.debug(completeMultipartUploadResult.getETag());
        return true;
    } catch (OSSException | ClientException e) {
        AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, dstKey, uploadId);
        ossClient.abortMultipartUpload(abortMultipartUploadRequest);
        return false;
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest)

Example 3 with OSSException

use of com.aliyun.oss.OSSException in project hadoop by apache.

the class AliyunOSSFileSystemStore method purge.

/**
   * Clean up all objects matching the prefix.
   *
   * @param prefix Aliyun OSS object prefix.
   * @throws IOException if failed to clean up objects.
   */
public void purge(String prefix) throws IOException {
    String key;
    try {
        ObjectListing objects = listObjects(prefix, maxKeys, null, true);
        for (OSSObjectSummary object : objects.getObjectSummaries()) {
            key = object.getKey();
            ossClient.deleteObject(bucketName, key);
        }
        for (String dir : objects.getCommonPrefixes()) {
            deleteDirs(dir);
        }
    } catch (OSSException | ClientException e) {
        LOG.error("Failed to purge " + prefix);
    }
}
Also used : OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException)

Example 4 with OSSException

use of com.aliyun.oss.OSSException in project hadoop by apache.

the class AliyunOSSFileSystemStore method retrieve.

/**
   * Retrieve a part of an object.
   *
   * @param key the object name that is being retrieved from the Aliyun OSS.
   * @param byteStart start position.
   * @param byteEnd end position.
   * @return This method returns null if the key is not found.
   */
public InputStream retrieve(String key, long byteStart, long byteEnd) {
    try {
        GetObjectRequest request = new GetObjectRequest(bucketName, key);
        request.setRange(byteStart, byteEnd);
        return ossClient.getObject(request).getObjectContent();
    } catch (OSSException | ClientException e) {
        return null;
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest)

Example 5 with OSSException

use of com.aliyun.oss.OSSException 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)

Aggregations

OSSException (com.aliyun.oss.OSSException)5 ClientException (com.aliyun.oss.ClientException)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)3 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 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 UploadPartCopyRequest (com.aliyun.oss.model.UploadPartCopyRequest)1 UploadPartCopyResult (com.aliyun.oss.model.UploadPartCopyResult)1 UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)1 UploadPartResult (com.aliyun.oss.model.UploadPartResult)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1