java - 加载正确的 AWS 凭证时,您提供的 AWS 访问 key ID 不存在于我们的记录中

标签 java spring amazon-web-services amazon-s3

我正在创建一个 Spring Boot 后端,它似乎无法上传带有我现在从三个不同客户端( postman 、React、后端本身)尝试过的预签名 URL 的图像。

所有客户端返回相同的客户端。

在最顶部我声明了这样的配置:


    @Bean
    @Scope("singleton")
    public S3Service provideS3Service(){

         String secretKey = environment.getProperty("aws.key.secret");
         String accessKey = environment.getProperty("aws.key.access");
         String bucketName = environment.getProperty("aws.bucket.name");
         AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

         String regionName = environment.getProperty("aws.bucket.region");
         Regions clientRegion = Regions.valueOf(regionName);

         System.out.println(awsCredentials.getAWSAccessKeyId());
         System.out.println(awsCredentials.getAWSSecretKey());
         AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withRegion(clientRegion)
                .build();
         S3Service s3Service = new S3Service(bucketName, s3Client);


         return s3Service;


    }

在我的 S3 服务中,我有以下函数来发出请求。现在我还模拟了写入 aws 的请求。


    public PresignedUrlResponse createSignedUrlPost() throws IOException {
        String objectKey = UUID.randomUUID().toString();
        LocalDateTime expiration = getExpirationDate();

        Date expirationDate = Date.from(expiration.atZone(ZoneId.systemDefault()).toInstant());
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(this.bucketName, objectKey)
                .withMethod(HttpMethod.POST)
                .withExpiration(expirationDate);

        URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest);

        // Create the connection and use it to upload the new object using the pre-signed URL.
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("This text uploaded as an object via presigned URL.");
        out.close();

        // Check the HTTP response code. To complete the upload and make the object available,
        // you must interact with the connection object in some way.
        connection.getResponseCode();
        System.out.println("HTTP response code: " + connection.getResponseCode());

        // Check to make sure that the object was uploaded successfully.
        S3Object object = amazonS3.getObject(bucketName, objectKey);
        System.out.println("Object " + object.getKey() + " created in bucket " + object.getBucketName());


        return new PresignedUrlResponse(url.toString(), expiration);
    }
    

这对应于以下错误:

{
  "timestamp": "Jul 18, 2020, 7:46:43 PM",
  "status": 500,
  "error": "Internal Server Error",
  "message": "The AWS Access Key Id you provided does not exist in our records. (Service: Amazon S3; Status Code: 403; Error Code: InvalidAccessKeyId; Request ID: DD94BBA0C28610CA; S3 Extended Request ID: tyyQDCnKD6ni+6uwumWh3psUvWM8TOVNguP8Y0dvkBPgmmUdebnrq+RBKqmXQcVQUyOxbqM5//o=; Proxy: null)",
  "path": "/chat/channels/signedurl"
}

我已确保凭据正确,并且也可以从控制台记录它们。


         System.out.println(awsCredentials.getAWSAccessKeyId());
         System.out.println(awsCredentials.getAWSSecretKey());

printed credentials

我已检查凭证是否与我的 AWS 账户上的凭证相符。 有谁知道这个问题可能是由什么引起的?

我现在已经尝试了一切。

最佳答案

如果您以这种方式打印访问 key 和 secret key ,看起来它们的方向是错误的。

访问 key 应始终是大写 A-Z 和数字组成的短字符串,而 secret key 是较长的字符串。

检查如何将它们传递到代码库中,以确保没有以错误的顺序放置它们。

关于java - 加载正确的 AWS 凭证时,您提供的 AWS 访问 key ID 不存在于我们的记录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62971927/

相关文章:

java - 资源 <资源 ID 的编号> 类型 0x12 无效

java - 在 Eclipse 中使用 Android build.xml?

java - 如何在 Web API 中使用 Spring Security?

spring - 使用 Spring Boot 配置集成测试还是在单元测试中使用 Spring JDBC?

java - 创建注释来执行常见操作并返回结果

amazon-web-services - 在没有服务器的情况下将文件拉入 S3

java - Action 监听器太快了,我进入下一个菜单

java - Apache Camel Spring Web 服务

amazon-web-services - 如何使用 Elastic Load Balancer 在 Amazon EC2 上设置 HTTPS

database - AWS DynamoDB - 如何设计一个表/索引来有效检索大量具有特定值/状态的项目