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

发布于 2024-10-02 07:52:38 字数 1101 浏览 7 评论 0原文

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

下载对象到本地文件

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

对于这些案例有什么建议吗? 预先感谢

安东尼斯

from the sample code examples of jetS3t the code that follows:
What to do if I don't know the number of files to be downloaded from
the beginning?
E.g an application with thumbnails in S3, and a client application
that the user scrolls through a list of thumbnails.

Download objects to local files

The multi-threading services provide a method to download
multiple objects at a time, but
to use this you must first prepare somewhere to put the
data associated with each object.
The most obvious place to put this data is into a file, so
let's go through an example of
downloading object data into files.
To download our objects into files we first must create a
DownloadPackage class for
each object. This class is a simple container which merely
associates an object with a
file, to which the object's data will be written.
Create a DownloadPackage for each object, to associate the
object with an output file.

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

Any suggestions for that cases?
Thanks in advance

Antonis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-10-09 07:52:38

您可以列出下载的存储桶中的对象,并根据您定义的规则过滤密钥。然后您就可以开始分段下载。

从存储桶中获取密钥:


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

<代码>

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

You can list the objects in the bucket you download from and filter keys according to the rules you define. Then you can start a multipart download.

get keys from the bucket:


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

after filtering the keys you can download particular part of the key-list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文