use of software.amazon.awssdk.services.s3.model.S3Object in project aws-doc-sdk-examples by awsdocs.
the class GetObject method main.
public static void main(String[] args) {
final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and object to\n" + "download from it.\n" + "\n" + "Ex: GetObject <bucketname> <filename>\n";
if (args.length < 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucket_name = args[0];
String key_name = args[1];
System.out.format("Downloading %s from S3 bucket %s...\n", key_name, bucket_name);
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
try {
S3Object o = s3.getObject(bucket_name, key_name);
S3ObjectInputStream s3is = o.getObjectContent();
FileOutputStream fos = new FileOutputStream(new File(key_name));
byte[] read_buf = new byte[1024];
int read_len = 0;
while ((read_len = s3is.read(read_buf)) > 0) {
fos.write(read_buf, 0, read_len);
}
s3is.close();
fos.close();
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println("Done!");
}
use of software.amazon.awssdk.services.s3.model.S3Object in project athenz by yahoo.
the class S3ChangeLogStoreTest method testGetUpdatedSignedDomainsWithChange.
@Test
public void testGetUpdatedSignedDomainsWithChange() throws FileNotFoundException {
MockS3ChangeLogStore store = new MockS3ChangeLogStore(null);
ArrayList<S3ObjectSummary> objectList = new ArrayList<>();
S3ObjectSummary objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas");
objectSummary.setLastModified(new Date(100));
objectList.add(objectSummary);
objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas.athenz");
objectSummary.setLastModified(new Date(200));
objectList.add(objectSummary);
ObjectListing objectListing = mock(ObjectListing.class);
when(objectListing.getObjectSummaries()).thenReturn(objectList);
when(objectListing.isTruncated()).thenReturn(false);
when(store.awsS3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(objectListing);
InputStream is = new FileInputStream("src/test/resources/iaas.json");
MockS3ObjectInputStream s3Is = new MockS3ObjectInputStream(is, null);
S3Object object = mock(S3Object.class);
when(object.getObjectContent()).thenReturn(s3Is);
when(store.awsS3Client.getObject("athenz-domain-sys.auth", "iaas")).thenReturn(object);
when(store.awsS3Client.getObject("athenz-domain-sys.auth", "iaas.athenz")).thenReturn(object);
// set the last modification time to return one of the domains
store.lastModTime = (new Date(150)).getTime();
StringBuilder lastModTimeBuffer = new StringBuilder(512);
SignedDomains signedDomains = store.getUpdatedSignedDomains(lastModTimeBuffer);
assertTrue(lastModTimeBuffer.length() > 0);
List<SignedDomain> domainList = signedDomains.getDomains();
assertEquals(domainList.size(), 1);
DomainData domainData = domainList.get(0).getDomain();
assertNotNull(domainData);
assertEquals(domainData.getName(), "iaas");
}
use of software.amazon.awssdk.services.s3.model.S3Object in project athenz by yahoo.
the class S3ChangeLogStore method getSignedDomain.
SignedDomain getSignedDomain(AmazonS3 s3, String domainName) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("AWSS3ChangeLog: getting signed domain {}", domainName);
}
SignedDomain signedDomain = null;
try {
S3Object object = s3.getObject(s3BucketName, domainName);
try (S3ObjectInputStream s3is = object.getObjectContent()) {
byte[] data = ByteStreams.toByteArray(s3is);
signedDomain = JSON.fromBytes(data, SignedDomain.class);
}
} catch (Exception ex) {
LOGGER.error("AWSS3ChangeLog: getSignedDomain - unable to get domain {} error: {}", domainName, ex.getMessage());
}
return signedDomain;
}
use of software.amazon.awssdk.services.s3.model.S3Object in project Singularity by HubSpot.
the class S3ArtifactChunkDownloader method createDownloader.
private Callable<Path> createDownloader(final int retryNum) {
return new Callable<Path>() {
public Path call() throws Exception {
final Path chunkPath = (chunk == 0) ? downloadTo : Paths.get(downloadTo + "_" + chunk + "_" + retryNum);
chunkPath.toFile().deleteOnExit();
final long startTime = System.currentTimeMillis();
final long byteRangeStart = chunk * chunkSize;
final long byteRangeEnd = Math.min((chunk + 1) * chunkSize - 1, length);
log.info("Downloading {} - chunk {} (retry {}) ({}-{}) to {}", s3Artifact.getFilename(), chunk, retryNum, byteRangeStart, byteRangeEnd, chunkPath);
GetObjectRequest getObjectRequest = new GetObjectRequest(s3Artifact.getS3Bucket(), s3Artifact.getS3ObjectKey()).withRange(byteRangeStart, byteRangeEnd);
S3Object fetchedObject = s3.getObject(getObjectRequest);
try (InputStream is = fetchedObject.getObjectContent()) {
Files.copy(is, chunkPath, StandardCopyOption.REPLACE_EXISTING);
}
log.info("Finished downloading chunk {} (retry {}) of {} ({} bytes) in {}", chunk, retryNum, s3Artifact.getFilename(), byteRangeEnd - byteRangeStart, JavaUtils.duration(startTime));
return chunkPath;
}
};
}
use of software.amazon.awssdk.services.s3.model.S3Object in project incubator-gobblin by apache.
the class AWSSdkClient method downloadS3Object.
/**
* Download a S3 object to local directory
*
* @param s3ObjectSummary S3 object summary for the object to download
* @param targetDirectory Local target directory to download the object to
* @throws IOException If any errors were encountered in downloading the object
*/
public void downloadS3Object(S3ObjectSummary s3ObjectSummary, String targetDirectory) throws IOException {
final AmazonS3 amazonS3 = getS3Client();
final GetObjectRequest getObjectRequest = new GetObjectRequest(s3ObjectSummary.getBucketName(), s3ObjectSummary.getKey());
final S3Object s3Object = amazonS3.getObject(getObjectRequest);
final String targetFile = StringUtils.removeEnd(targetDirectory, File.separator) + File.separator + s3Object.getKey();
FileUtils.copyInputStreamToFile(s3Object.getObjectContent(), new File(targetFile));
LOGGER.info("S3 object downloaded to file: " + targetFile);
}
Aggregations