Search in sources :

Example 16 with OSS

use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.

the class DoesObjectExistTest method testUnormalDoesObjectExist.

@Test
public void testUnormalDoesObjectExist() {
    final String nonexistentKey = "test-unormal-does-object-exist";
    // SignatureDoesNotMatch
    OSS client = new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET + " ");
    try {
        client.doesObjectExist(bucketName, nonexistentKey);
        Assert.fail("Does object exist should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.SIGNATURE_DOES_NOT_MATCH, ex.getErrorCode());
    } finally {
        client.shutdown();
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 17 with OSS

use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.

the class MultiInstanceClientTest method keepUsingAfterClose.

@Test
public void keepUsingAfterClose() {
    final String key = "key0";
    OSS client = new OSSClientBuilder().build(OSS_TEST_ENDPOINT, OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET);
    InputStream content = TestUtils.genFixedLengthInputStream(128 * 1024);
    client.putObject(bucketName, key, content, null);
    client.shutdown();
    try {
        content = TestUtils.genFixedLengthInputStream(128 * 1024);
        client.putObject(bucketName, key, content, null);
    } catch (ClientException ce) {
        System.out.println(ce.getMessage());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) ClientException(com.aliyun.oss.ClientException) Test(org.junit.Test)

Example 18 with OSS

use of com.aliyun.oss.OSS in project druid by druid-io.

the class OssDataSegmentKiller method kill.

@Override
public void kill(DataSegment segment) throws SegmentLoadingException {
    try {
        Map<String, Object> loadSpec = segment.getLoadSpec();
        String bucket = MapUtils.getString(loadSpec, "bucket");
        String path = MapUtils.getString(loadSpec, "key");
        final OSS client = this.clientSupplier.get();
        if (client.doesObjectExist(bucket, path)) {
            log.info("Removing index file[%s://%s/%s] from aliyun OSS!", OssStorageDruidModule.SCHEME, bucket, path);
            client.deleteObject(bucket, path);
        }
    } catch (OSSException e) {
        throw new SegmentLoadingException(e, "Couldn't kill segment[%s]: [%s]", segment.getId(), e);
    }
}
Also used : SegmentLoadingException(org.apache.druid.segment.loading.SegmentLoadingException) OSSException(com.aliyun.oss.OSSException) OSS(com.aliyun.oss.OSS)

Example 19 with OSS

use of com.aliyun.oss.OSS in project druid by druid-io.

the class OssDataSegmentPullerTest method testGZUncompressRetries.

@Test
public void testGZUncompressRetries() throws IOException, SegmentLoadingException {
    final String bucket = "bucket";
    final String keyPrefix = "prefix/dir/0";
    final OSS ossClient = EasyMock.createStrictMock(OSS.class);
    final byte[] value = bucket.getBytes(StandardCharsets.UTF_8);
    final File tmpFile = temporaryFolder.newFile("gzTest.gz");
    try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) {
        outputStream.write(value);
    }
    OSSObject object0 = new OSSObject();
    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.getObjectMetadata().setLastModified(new Date(0));
    object0.setObjectContent(new FileInputStream(tmpFile));
    final OSSObjectSummary objectSummary = new OSSObjectSummary();
    objectSummary.setBucketName(bucket);
    objectSummary.setKey(keyPrefix + "/renames-0.gz");
    objectSummary.setLastModified(new Date(0));
    final ObjectListing listObjectsResult = new ObjectListing();
    listObjectsResult.getObjectSummaries().add(objectSummary);
    File tmpDir = temporaryFolder.newFolder("gzTestDir");
    OSSException exception = new OSSException("OssDataSegmentPullerTest", "NoSuchKey", null, null, null, null, null);
    EasyMock.expect(ossClient.doesObjectExist(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(true).once();
    EasyMock.expect(ossClient.listObjects(EasyMock.anyObject(ListObjectsRequest.class))).andReturn(listObjectsResult).once();
    EasyMock.expect(ossClient.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey()))).andThrow(exception).once();
    EasyMock.expect(ossClient.listObjects(EasyMock.anyObject(ListObjectsRequest.class))).andReturn(listObjectsResult).once();
    EasyMock.expect(ossClient.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    OssDataSegmentPuller puller = new OssDataSegmentPuller(ossClient);
    EasyMock.replay(ossClient);
    FileUtils.FileCopyResult result = puller.getSegmentFiles(new CloudObjectLocation(bucket, object0.getKey()), tmpDir);
    EasyMock.verify(ossClient);
    Assert.assertEquals(value.length, result.size());
    File expected = new File(tmpDir, "renames-0");
    Assert.assertTrue(expected.exists());
    Assert.assertEquals(value.length, expected.length());
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) FileUtils(org.apache.druid.java.util.common.FileUtils) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) OSS(com.aliyun.oss.OSS) Date(java.util.Date) FileInputStream(java.io.FileInputStream) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) GZIPOutputStream(java.util.zip.GZIPOutputStream) CloudObjectLocation(org.apache.druid.data.input.impl.CloudObjectLocation) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Example 20 with OSS

use of com.aliyun.oss.OSS in project druid by druid-io.

the class OssDataSegmentPullerTest method testGZUncompress.

@Test
public void testGZUncompress() throws IOException, SegmentLoadingException {
    final String bucket = "bucket";
    final String keyPrefix = "prefix/dir/0";
    final OSS ossClient = EasyMock.createStrictMock(OSS.class);
    final byte[] value = bucket.getBytes(StandardCharsets.UTF_8);
    final File tmpFile = temporaryFolder.newFile("gzTest.gz");
    try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) {
        outputStream.write(value);
    }
    final OSSObject object0 = new OSSObject();
    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.getObjectMetadata().setLastModified(new Date(0));
    object0.setObjectContent(new FileInputStream(tmpFile));
    final OSSObjectSummary objectSummary = new OSSObjectSummary();
    objectSummary.setBucketName(bucket);
    objectSummary.setKey(keyPrefix + "/renames-0.gz");
    objectSummary.setLastModified(new Date(0));
    final ObjectListing listObjectsResult = new ObjectListing();
    listObjectsResult.getObjectSummaries().add(objectSummary);
    final File tmpDir = temporaryFolder.newFolder("gzTestDir");
    EasyMock.expect(ossClient.doesObjectExist(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(true).once();
    EasyMock.expect(ossClient.listObjects(EasyMock.anyObject(ListObjectsRequest.class))).andReturn(listObjectsResult).once();
    EasyMock.expect(ossClient.getObject(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    OssDataSegmentPuller puller = new OssDataSegmentPuller(ossClient);
    EasyMock.replay(ossClient);
    FileUtils.FileCopyResult result = puller.getSegmentFiles(new CloudObjectLocation(bucket, object0.getKey()), tmpDir);
    EasyMock.verify(ossClient);
    Assert.assertEquals(value.length, result.size());
    File expected = new File(tmpDir, "renames-0");
    Assert.assertTrue(expected.exists());
    Assert.assertEquals(value.length, expected.length());
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) FileUtils(org.apache.druid.java.util.common.FileUtils) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ObjectListing(com.aliyun.oss.model.ObjectListing) OSS(com.aliyun.oss.OSS) Date(java.util.Date) FileInputStream(java.io.FileInputStream) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) GZIPOutputStream(java.util.zip.GZIPOutputStream) CloudObjectLocation(org.apache.druid.data.input.impl.CloudObjectLocation) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Aggregations

OSS (com.aliyun.oss.OSS)50 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)40 Test (org.junit.Test)24 OSSException (com.aliyun.oss.OSSException)19 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ClientException (com.aliyun.oss.ClientException)17 ObjectListing (com.aliyun.oss.model.ObjectListing)10 Date (java.util.Date)10 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)9 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)9 OSSObject (com.aliyun.oss.model.OSSObject)8 Ignore (org.junit.Ignore)8 File (java.io.File)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)5 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)5 Credentials (com.aliyun.oss.common.auth.Credentials)4 URI (java.net.URI)4 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)3