使用JetS3t将未知数量的文件多次上传到S3

时间:2010-11-15 13:16:31

标签: java amazon-s3 jets3t

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

将对象下载到本地文件

多线程服务提供了一种下载方法 一次多个对象,但是 使用这个你必须先准备一个地方放置 与每个对象关联的数据。 放置这些数据最明显的地方是文件,所以 让我们来看一个例子 将对象数据下载到文件中。 要将我们的对象下载到文件中,首先必须创建一个 DownloadPackage类 每个对象。这个类只是一个简单的容器 将对象与a关联 文件,将写入对象的数据。  为每个对象创建一个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");

对这些案件的任何建议? 提前致谢

安东尼

1 个答案:

答案 0 :(得分: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; }
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; }

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