简体   繁体   English

software.amazon.awssdk.services.s3.model.s3exception: null 使用 GetObjectRequest

[英]software.amazon.awssdk.services.s3.model.s3exception: null with using GetObjectRequest

I am trying to use this code but i'm getting an exeption我正在尝试使用此代码,但出现异常

var awsBasicCredentials  =  Packages.software.amazon.awssdk.auth.credentials.AwsBasicCredentials.create("ackey","secretkey");
var credentials =  Packages.software.amazon.awssdk.auth.credentials.StaticCredentialsProvider.create(awsBasicCredentials);

var region = Packages.software.amazon.awssdk.regions.Region.AWS_CN_GLOBAL;
var uri =  Packages.java.net.URI.create("http://host");
var client =  Packages.software.amazon.awssdk.services.s3.S3Client.builder()
                                                            
                                                            .credentialsProvider(credentials)
                                                            .region(region)
                                                            .endpointOverride(uri)
                                                            .build(); 
var request  = Packages.software.amazon.awssdk.services.s3.model.GetObjectRequest.builder()
                                                                        .bucket("/bucketname")                                      
                                                                        .key("key")
                                                                        .build();
var response = client.getObject(request);
return response;

I am using /bucketname because the final link looks like host/bucketname instead bucketname.host我正在使用 /bucketname 因为最终链接看起来像 host/bucketname 而不是 bucketname.host

S3Exeption: S3异常:

Caused by: software.amazon.awssdk.services.s3.model.S3Exception: null (Service: S3, Status Code: 403, Request ID: tx0000000000000162eece0-00633fea7f-306fc-msk-rt1)
    at software.amazon.awssdk.core.internal.http.CombinedResponseHandler.handleErrorResponse(CombinedResponseHandler.java:125)
    at software.amazon.awssdk.core.internal.http.CombinedResponseHandler.handleResponse(CombinedResponseHandler.java:82)
    at software.amazon.awssdk.core.internal.http.CombinedResponseHandler.handle(CombinedResponseHandler.java:60)
    

Bucket names can consist only of lowercase letters, numbers, dots (.), and hyphens (-).桶名只能由小写字母、数字、点(.)和连字符(-)组成。

Here is S3 Java code that works and returns a byte[] that represents the object that is located in the given Amazon S3 bucket .这是 S3 Java 代码,它可以工作并返回一个 byte[],表示位于给定Amazon S3 bucket中的 object。

In this example, the path represents the local file system where the object is written to.在此示例中,路径表示写入 object 的本地文件系统。 FOr example, a PDF file.例如,一个 PDF 文件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 class software.amazon.awssdk.services.s3.model.CommonPrefix 无法转换为 class java.lang.String - class software.amazon.awssdk.services.s3.model.CommonPrefix cannot be cast to class java.lang.String 来自 AwsSDK S3AsyncClient GetObjectRequest 的 SSLException - SSLException from AwsSDK S3AsyncClient GetObjectRequest 软件.amazon.awssdk.core.exception.SdkClientException - software.amazon.awssdk.core.exception.SdkClientException 有没有办法在仅依赖 software.amazon.awssdk:s3:2.17.173 的情况下访问 S3? - Is there a way to access S3 while relying only on software.amazon.awssdk:s3:2.17.173? 任何人都可以使用 software.amazon.awssdk.http.service.impl 帮助解决这个运行时错误“在类路径上发现多个 HTTP 实现” - Can anyone help resolve this runtime error "Multiple HTTP implementations were found on the classpath" using software.amazon.awssdk.http.service.impl 亚马逊 AWS S3 生命周期规则异常? - Amazon AWS S3 Lifecycle rule exception? AWS Java SDK V2中的package software.amazon.awssdk.core.auth.policy go在哪里? - Where did the package software.amazon.awssdk.core.auth.policy go in the AWS Java SDK V2? 口是心非,亚马逊 s3 后端异常 - Duplicity, amazon s3 backend exception 使用 multer-s3 nodejs 将图像上传到 amazon s3 - Uploading image to amazon s3 using multer-s3 nodejs 如何从 Amazon S3 存储桶中读取数据并调用 AWS 服务 - How to read from an Amazon S3 Bucket and call AWS services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM