To download the file we need a file name which is a key to represent file in the S3 bucket. To implement this we are using Spring boot with aws-java-sdk-s3. Amazon S3 Java SDK provides a simple interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web.
You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.
Downloading and extracting the SDK Download the SDK from https://sdk-for-java.amazonwebservices.com/latest/aws-java-sdk.zip .
We can get these credentials in two ways, either by using AWS root account credentials from the access keys section of the Security Credentials page, or by using IAM user credentials from the IAM console. Choosing AWS Region: We also have to select the AWS region(s) where we want to store our Amazon S3 data.
Though the code written in Mauricio's answer will work - and his point about streams is of course correct - Amazon offers a quicker way to save files in their SDK. I don't know if it wasn't available in 2011 or not, but it is now.
AmazonS3Client s3Client = new AmazonS3Client(myCredentials);
File localFile = new File("localFilename");
ObjectMetadata object = s3Client.getObject(new GetObjectRequest("bucket", "s3FileName"), localFile);
Instead of Reader
and Writer
classes you should be using InputStream
and OutputStream
classes:
InputStream reader = new BufferedInputStream(
object.getObjectContent());
File file = new File("localFilename");
OutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
int read = -1;
while ( ( read = reader.read() ) != -1 ) {
writer.write(read);
}
writer.flush();
writer.close();
reader.close();
Eyals answer gets you half way there but its not all that clear so I will clarify.
AmazonS3Client s3Client = new AmazonS3Client(myCredentials);
//This is where the downloaded file will be saved
File localFile = new File("localFilename");
//This returns an ObjectMetadata file but you don't have to use this if you don't want
s3Client.getObject(new GetObjectRequest(bucketName, id.getId()), localFile);
//Now your file will have your image saved
boolean success = localFile.exists() && localFile.canRead();
There is even much simpler way to get this. I used below snippet. Got reference from http://docs.ceph.com/docs/mimic/radosgw/s3/java/
AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_1).build();
s3client.getObject(
new GetObjectRequest("nomad-prop-pics", "Documents/1.pdf"),
new File("D:\\Eka-Contract-Physicals-Dev\\Contracts-Physicals\\utility-service\\downlods\\1.pdf")
);
Use java.nio.file.Files
to copy S3Object
to local file.
public File getFile(String fileName) throws Exception {
if (StringUtils.isEmpty(fileName)) {
throw new Exception("file name can not be empty");
}
S3Object s3Object = amazonS3.getObject("bucketname", fileName);
if (s3Object == null) {
throw new Exception("Object not found");
}
File file = new File("you file path");
Files.copy(s3Object.getObjectContent(), file.toPath());
return file;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With