如何下载S3 Bucket上的整个文件夹?

时间:2016-03-08 10:36:14

标签: java amazon-web-services amazon-s3

我使用过Java SDK并尝试使用 GetObjectRequest 类下载文件夹,但可以下载我的文件夹,包括其子文件夹和所有要下载的文件吗?

4 个答案:

答案 0 :(得分:3)

您可以使用downloadDirectory课程中的TransferManager方法:

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

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

正如documentation中所写,这种方法:

  

下载由给定目标目录的keyPrefix指定的虚拟目录中的所有对象。所有虚拟子目录都将以递归方式下载。

答案 1 :(得分:0)

您必须调用ListBucket API来获取文件列表,然后使用GetObject单独下载每个文件

答案 2 :(得分:0)

是的,使用TransferManager.downloadFolder:)

答案 3 :(得分:0)

以下是下载整个存储桶(经过某种程度的测试)的代码:

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