将Amazon S3对象供应到Java对象

发布于 2025-02-09 20:54:59 字数 1026 浏览 1 评论 0 原文

我有一个用户定义的对象,例如,MyClass保存在一个文件夹中的Amazon S3存储桶中。存储桶结构看起来如下:

<bucket>
-<folder>
--<my unique numbered file containing MyClass>

我需要读取此文件并将其验证为myclass。

我浏览了AWS文档,建议使用以下

Regions clientRegion = Regions.DEFAULT_REGION;
 String bucketName = "*** Bucket name ***";
 String key = "*** Object key ***";

 S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
 try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

   fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));

示例之类的内容: https://docs.aws.amazon.com/amazons3/latest/userguide/download-objects.html

但是,我需要将其归因于myclass而不是S3Object。 我无法获得如何做到这一点的例子。谁能建议这样做?

I have a user defined object, say MyClass, saved in an Amazon S3 bucket inside a folder. The bucket structure looks like the following:

<bucket>
-<folder>
--<my unique numbered file containing MyClass>

I need to read this file and deserialize it to MyClass.

I went through AWS docs and it suggested to use something like the following

Regions clientRegion = Regions.DEFAULT_REGION;
 String bucketName = "*** Bucket name ***";
 String key = "*** Object key ***";

 S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
 try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

   fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));

Example: https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html

However, I need to deserialize it to MyClass instead of S3Object.
I couldn't get an example of how to do this. Can anyone suggest how this is done?

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

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

发布评论

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

评论(1

御守 2025-02-16 20:54:59

您正在查看正确的指南 - 最新Java API的错误主题。您正在查看的主题是旧的V1代码

查看此主题要使用 aws sdk作为Java V2 - 被认为是最佳实践。 V2软件包以 software.amazon.awssdk 开始。

一旦从S3存储桶中获得对象,就可以将其转换为字节数组,并从那里转换为您需要的任何事情。此V2示例显示了如何从对象中获取字节数组。

package com.example.s3;

// snippet-start:[s3.java2.getobjectdata.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
// snippet-end:[s3.java2.getobjectdata.import]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */

public class GetObjectData {

    public static void main(String[] args) {

     final String usage = "\n" +
                "Usage:\n" +
                "    <bucketName> <keyName> <path>\n\n" +
                "Where:\n" +
                "    bucketName - The Amazon S3 bucket name. \n\n"+
                "    keyName - The key name. \n\n"+
                "    path - The path where the file is written to. \n\n";

        if (args.length != 3) {
            System.out.println(usage);
            System.exit(1);
        }

        String bucketName = args[0];
        String keyName = args[1];
        String path = args[2];

        ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder()
                .region(region)
                .credentialsProvider(credentialsProvider)
                .build();

        getObjectBytes(s3,bucketName,keyName, path);
        s3.close();
    }

    // snippet-start:[s3.java2.getobjectdata.main]
    public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path ) {

        try {
            GetObjectRequest objectRequest = GetObjectRequest
                    .builder()
                    .key(keyName)
                    .bucket(bucketName)
                    .build();

            ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
            byte[] data = objectBytes.asByteArray();

            // Write the data to a local file.
            File myFile = new File(path );
            OutputStream os = new FileOutputStream(myFile);
            os.write(data);
            System.out.println("Successfully obtained bytes from an S3 object");
            os.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (S3Exception e) {
          System.err.println(e.awsErrorDetails().errorMessage());
           System.exit(1);
        }
    }
    // snippet-end:[s3.java2.getobjectdata.main]
}

You are looking in the correct Guide - wrong topic for latest Java API to use. The topic you are looking at is old V1 code.

Look at this topic to use AWS SDK for Java V2 - which is considered best practice. V2 packages start with software.amazon.awssdk.

https://docs.aws.amazon.com/AmazonS3/latest/userguide/example_s3_GetObject_section.html

Once you get an object from an S3 bucket, you can convert it into a byte array and from there - do what ever you need to. This V2 example shows how to get a byte array from an object.

package com.example.s3;

// snippet-start:[s3.java2.getobjectdata.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
// snippet-end:[s3.java2.getobjectdata.import]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */

public class GetObjectData {

    public static void main(String[] args) {

     final String usage = "\n" +
                "Usage:\n" +
                "    <bucketName> <keyName> <path>\n\n" +
                "Where:\n" +
                "    bucketName - The Amazon S3 bucket name. \n\n"+
                "    keyName - The key name. \n\n"+
                "    path - The path where the file is written to. \n\n";

        if (args.length != 3) {
            System.out.println(usage);
            System.exit(1);
        }

        String bucketName = args[0];
        String keyName = args[1];
        String path = args[2];

        ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder()
                .region(region)
                .credentialsProvider(credentialsProvider)
                .build();

        getObjectBytes(s3,bucketName,keyName, path);
        s3.close();
    }

    // snippet-start:[s3.java2.getobjectdata.main]
    public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path ) {

        try {
            GetObjectRequest objectRequest = GetObjectRequest
                    .builder()
                    .key(keyName)
                    .bucket(bucketName)
                    .build();

            ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
            byte[] data = objectBytes.asByteArray();

            // Write the data to a local file.
            File myFile = new File(path );
            OutputStream os = new FileOutputStream(myFile);
            os.write(data);
            System.out.println("Successfully obtained bytes from an S3 object");
            os.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (S3Exception e) {
          System.err.println(e.awsErrorDetails().errorMessage());
           System.exit(1);
        }
    }
    // snippet-end:[s3.java2.getobjectdata.main]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文