2

来自jetS3t的示例代码示例如下代码:如果我不知道从一开始要下载的文件数怎么办?例如,在 S3 中具有缩略图的应用程序,以及用户滚动浏览缩略图列表的客户端应用程序。

将对象下载到本地文件

多线程服务提供了一次下载多个对象的方法,但要使用此方法,您必须首先准备好放置与每个对象关联的数据的地方。将这些数据放在文件中最明显的地方是,让我们来看一个将对象数据下载到文件中的示例。要将我们的对象下载到文件中,我们首先必须为每个对象创建一个 DownloadPackage 类。此类是一个简单的容器,它仅将对象与文件相关联,对象的数据将写入该文件。为每个对象创建一个 DownloadPackage,以将对象与输出文件相关联。

 DownloadPackage[] downloadPackages = new DownloadPackage[5];
   downloadPackages[0] = new DownloadPackage(objects[0],
       new File(objects[0].getKey()));
   downloadPackages[1] = new DownloadPackage(objects[1],
       new File(objects[1].getKey()));
   downloadPackages[2] = new DownloadPackage(objects[2],
       new File(objects[2].getKey()));
   downloadPackages[3] = new DownloadPackage(objects[3],
       new File(objects[3].getKey()));
   downloadPackages[4] = new DownloadPackage(objects[4],
       new File(objects[4].getKey()));

   // Download the objects.
   simpleMulti.downloadObjects(bucket, downloadPackages);
   System.out.println("Downloaded objects to current working directory");

对这种情况有什么建议吗?提前致谢

安东尼斯

4

1 回答 1

0

您可以列出您从中下载的存储桶中的对象,并根据您定义的规则过滤键。然后您可以开始多部分下载。

从存储桶中获取密钥:


public List getFilesList(String accessKey, String secretKey ,String bucketName ,String directoryPathRelativeToBucket) { List keys = new ArrayList(); org.jets3t.service.model.S3Object[] objects = new org.jets3t.service.model.S3Object[]{}; try { // Create a credentials object and service to access S3 account org.jets3t.service.security.AWSCredentials myCredentials = new org.jets3t.service.security.AWSCredentials(accessKey, secretKey);

S3Service service = new RestS3Service(myCredentials); objects = service.listObjects(bucketName ,directoryPathRelativeToBucket, null); log.info("got bucket listing for bucket[" + bucketName + "]"); } catch (S3ServiceException e) { log.error("Failed to get or object listing for bucket[" + bucketName + "] due to exception:", e); } for (org.jets3t.service.model.S3Object s3Object : objects) { if (s3Object.getKey().contains("$") == false) { keys.add(s3Object.getKey()); } } return keys; }

过滤密钥后,您可以下载密钥列表的特定部分。

于 2011-04-10T07:28:02.813 回答