java 如何下载位于 S3 Bucket 上的整个文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35865045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Download entire Folder located on S3 Bucket?
提问by Abhishek
I have used Java SDK and try to download Folder using GetObjectRequestclass, but it is possible to download my folder incuding its subFolder and all files to download ?
我已经使用 Java SDK 并尝试使用GetObjectRequest类下载文件夹,但是可以下载我的文件夹,包括它的子文件夹和所有要下载的文件吗?
回答by velika12
You can use downloadDirectory
method from TransferManager
class:
您可以使用类中的downloadDirectory
方法TransferManager
:
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.
下载给定目标目录的 keyPrefix 指定的虚拟目录中的所有对象。所有虚拟子目录都将递归下载。
回答by Max
You have to call the ListBucket API to get the list of files, then download each one individually with GetObject
您必须调用 ListBucket API 来获取文件列表,然后使用 GetObject 单独下载每个文件
回答by Tiziano
Yes, use TransferManager.downloadFolder :)
是的,使用 TransferManager.downloadFolder :)
回答by Reece
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);
}
}
}