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

20,298

Solution 1

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"));

Solution 2

I used:

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

It worked fine.

Solution 3

There is an API to directly download file to local path

ObjectMetadata getObject(GetObjectRequest getObjectRequest,
                     File destinationFile)

Solution 4

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.

Share:
20,298
Shyamala Selvaganapathy
Author by

Shyamala Selvaganapathy

Updated on November 02, 2020

Comments

  • Shyamala Selvaganapathy
    Shyamala Selvaganapathy over 3 years

    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?

  • Shyamala Selvaganapathy
    Shyamala Selvaganapathy over 10 years
    I found this API for s3Client: s3Client.getObject(new GetObjectRequest(bucketName,key),file) which gets the object metadata and stores it in the file in the local location. Are there similar methods in s3Client to store the complete file in the location???
  • Shyamala Selvaganapathy
    Shyamala Selvaganapathy over 10 years
    Does this method store only the object metadata it returns in the file or the object contents? Its really confusing...Help please
  • hadooper
    hadooper almost 7 years
    The question here is where to specify local path, not the path on S3