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