Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Download entire Folder located on S3 Bucket?

I have used Java SDK and try to download Folder using GetObjectRequest class, but it is possible to download my folder incuding its subFolder and all files to download ?

like image 900
Abhishek Avatar asked Mar 08 '16 10:03

Abhishek


People also ask

Can we download folder from S3 bucket?

Use the cp command to download a folder inside a bucket from S3 to local. recursive option will download all files and folders if you have a recursive folder/file structure.

How do I download multiple files from S3 bucket to local?

If you have Visual Studio with the AWS Explorer extension installed, you can also browse to Amazon S3 (step 1), select your bucket (step 2), select al the files you want to download (step 3) and right click to download them all (step 4).

How do I download an entire directory to AWS S3?

How to Download a Folder from AWS S3 # Use the s3 cp command with the --recursive parameter to download an S3 folder to your local file system. The s3 cp command takes the S3 source folder and the destination directory as inputs and downloads the folder.

Can I download multiple files from S3?

Download multiple files from AWS CloudShell using Amazon S3 Now you need to download the contents of the bucket to your local machine. Because the Amazon S3 console doesn't support the downloading of multiple objects, you need to use the AWS CLI tool that's installed on your local machine.


2 Answers

You can use downloadDirectory method from TransferManager class:

TransferManager transferManager = new TransferManager(new DefaultAWSCredentialsProviderChain());
File dir = new File("destDir");

MultipleFileDownload download =  transferManager.downloadDirectory("myBucket", "myKey", dir);
download.waitForCompletion();

As it is written in the documentation, this method:

Downloads all objects in the virtual directory designated by the keyPrefix given to the destination directory given. All virtual subdirectories will be downloaded recursively.

like image 126
velika12 Avatar answered Oct 16 '22 23:10

velika12


Here's the code to download a whole bucket (somewhat tested):

import com.amazonaws.AmazonServiceException;
import aws.example.s3.XferMgrProgress;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.MultipleFileDownload;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import java.io.*;
import com.amazonaws.auth.PropertiesFileCredentialsProvider;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.AmazonClientException;
public class S3DownloadApp {

    public static void main(String [] args){
        AWSCredentials credentials = null;
        try {
            credentials = new PropertiesFileCredentialsProvider("keys.props").getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException(
                    "Cannot load the credentials from the credential profiles file. " , e);
        }

        TransferManager xfer_mgr = TransferManagerBuilder.standard().withS3Client(AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion("us-west-2").build()).build();//TransferManagerBuilder.standard().build();

        try {
            MultipleFileDownload xfer = xfer_mgr.downloadDirectory(
                    "bucketName", null, new File("/Users/admin/Desktop/downloadFolder"));
            XferMgrProgress.showTransferProgress(xfer);
            XferMgrProgress.waitForCompletion(xfer);
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }
    }
}
like image 43
Reece Avatar answered Oct 17 '22 00:10

Reece