public FileUtils.FileCopyResult getSegmentFiles( String region, String container, String path, File outDir) throws SegmentLoadingException { CloudFilesObjectApiProxy objectApi = new CloudFilesObjectApiProxy(cloudFilesApi, region, container); final CloudFilesByteSource byteSource = new CloudFilesByteSource(objectApi, path); try { final FileUtils.FileCopyResult result = CompressionUtils.unzip(byteSource, outDir, CloudFilesUtils.CLOUDFILESRETRY, true); log.info("Loaded %d bytes from [%s] to [%s]", result.size(), path, outDir.getAbsolutePath()); return result; } catch (Exception e) { try { org.apache.commons.io.FileUtils.deleteDirectory(outDir); } catch (IOException ioe) { log.warn( ioe, "Failed to remove output directory [%s] for segment pulled from [%s]", outDir.getAbsolutePath(), path); } throw new SegmentLoadingException(e, e.getMessage()); } finally { try { byteSource.closeStream(); } catch (IOException ioe) { log.warn(ioe, "Failed to close payload for segmente pulled from [%s]", path); } } }
@Test public void testGZUncompressRetries() throws ServiceException, IOException, SegmentLoadingException { final String bucket = "bucket"; final String keyPrefix = "prefix/dir/0"; final RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class); final byte[] value = bucket.getBytes("utf8"); final File tmpFile = temporaryFolder.newFile("gzTest.gz"); try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) { outputStream.write(value); } S3Object object0 = new S3Object(); object0.setBucketName(bucket); object0.setKey(keyPrefix + "/renames-0.gz"); object0.setLastModifiedDate(new Date(0)); object0.setDataInputStream(new FileInputStream(tmpFile)); File tmpDir = temporaryFolder.newFolder("gzTestDir"); S3ServiceException exception = new S3ServiceException(); exception.setErrorCode("NoSuchKey"); exception.setResponseCode(404); EasyMock.expect( s3Client.getObjectDetails( EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))) .andReturn(null) .once(); EasyMock.expect( s3Client.getObjectDetails( EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))) .andReturn(object0) .once(); EasyMock.expect(s3Client.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey()))) .andThrow(exception) .once(); EasyMock.expect( s3Client.getObjectDetails( EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))) .andReturn(object0) .once(); EasyMock.expect(s3Client.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey()))) .andReturn(object0) .once(); S3DataSegmentPuller puller = new S3DataSegmentPuller(s3Client); EasyMock.replay(s3Client); FileUtils.FileCopyResult result = puller.getSegmentFiles(new S3DataSegmentPuller.S3Coords(bucket, object0.getKey()), tmpDir); EasyMock.verify(s3Client); Assert.assertEquals(value.length, result.size()); File expected = new File(tmpDir, "renames-0"); Assert.assertTrue(expected.exists()); Assert.assertEquals(value.length, expected.length()); }