How to store a downloaded file object from S3 to a local directory

I'm trying to download a file from S3 using AWS SDK for Java and store the particular file in a local directory in my PC.

The code I wrote for downloading the object is:

public void download(String key) {
S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
}

But what I actually want to do is to pass the local path as a parameter instead of key and store the downloaded file obj in that particular directory say /tmp/AWSStorage/ in my linux box.

Can you please suggest a way of doing it?

Upvotes: 4

Views: 28039

Answers (5)

Shiva Kumar
Shiva Kumar

Reputation: 3161

There is an API to directly download file to local path

ObjectMetadata getObject(GetObjectRequest getObjectRequest,
                     File destinationFile)

Upvotes: 9

I used:

s3Client.getObject(new GetObjectRequest(bucket,key),file);

It worked fine.

Upvotes: 9

Sagar Chilukuri
Sagar Chilukuri

Reputation: 1448

With Java >=1.6, you can directly copy the files downloaded to local directory without any problem of file corruption. Check the code:

S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketName, fileName));
final BufferedInputStream i = new BufferedInputStream(fetchFile.getObjectContent());
InputStream objectData = fetchFile.getObjectContent();
Files.copy(objectData, new File("D:\\" + fileName).toPath()); //location to local path
objectData.close();

With Java 1.6 and above, you can directly specify path in Files.copy function.

Upvotes: 1

Tej Kiran
Tej Kiran

Reputation: 2248

In the line S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key)); bucketname is the S3BucketName and Key is the object name, it is not a local file path. Key - is the combination of common-prefix / objectname

i.e. if you file is saved at root of bucket then only name of the object will be the key i.e. myfile.txt but if your file is save like myfolder1/myfolder2/myfile.txt then myfolder1/myfolder is your common-prefix and myfile.txt is objectname.

S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,"myfolder1/myfolder2/myfile.txt"));

Upvotes: -3

Coder
Coder

Reputation: 7076

You can use obj.getDataInputStream() to get the file. And then org.apache.commons.io.IOUtils copy method to copy.

S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
File file=new File("/tmp/AWSStorage/"+key);

// if the directory does not exist, create it
if (!file.getParentFile().exists()) {
    file.getParentFile().mkdirs();
}

And then you can use either of following.

try {
    IOUtils.copy(obj.getDataInputStream(), new FileOutputStream(file));

} catch (Exception e) {
    e.printStackTrace();
}

OR

BufferedReader reader=null;
BufferedWriter out=null;
String data = null;
try {
    reader = new BufferedReader(new InputStreamReader(fileObj.getDataInputStream()));
    out = new BufferedWriter (new FileWriter(file));
    while ((data = reader.readLine()) != null) {
        out.write(data);
    }
} catch (Exception e) {
    e.printStackTrace();
}
finally {
    reader.close();
    out.close();
}

Upvotes: 1

Related Questions