Amazon S3 Signature Does Not Match - AWS SDK Java

前端 未结 8 1894
不知归路
不知归路 2021-01-01 12:18

I have a play application that needs to upload files to S3. We are developing in scala and using the Java AWS SDK.

I\'m having trouble trying to upload files, I keep

相关标签:
8条回答
  • 2021-01-01 13:12

    I just encountered this problem using the NodeJs AWS SDK. It was due to using credentials that were valid, but without sufficient permissions. Changing to my admin key fixed this with no code changes!

    0 讨论(0)
  • 2021-01-01 13:15

    I had the same issue, but removing content-type works fine. Hereby sharing the complete code.

    public class GeneratePresignedUrlAndUploadObject {
        private static final String BUCKET_NAME = "<YOUR_AWS_BUCKET_NAME>"; 
        private static final String OBJECT_KEY  = "<YOUR_AWS_KEY>";
        private static final String AWS_ACCESS_KEY = "<YOUR_AWS_ACCESS_KEY>";
        private static final String AWS_SECRET_KEY = "<YOUR_AWS_SECRET_KEY>";
    
        public static void main(String[] args) throws IOException {
            BasicAWSCredentials awsCreds = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);
    
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1)
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
    
            try {
                System.out.println("Generating pre-signed URL.");
                java.util.Date expiration = new java.util.Date();
                long milliSeconds = expiration.getTime();
                milliSeconds += 1000 * 60 * 60;
                expiration.setTime(milliSeconds);
    
                GeneratePresignedUrlRequest generatePresignedUrlRequest = 
                        new GeneratePresignedUrlRequest(BUCKET_NAME, OBJECT_KEY);
                generatePresignedUrlRequest.setMethod(HttpMethod.PUT); 
                generatePresignedUrlRequest.setExpiration(expiration);
                URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); 
    
                UploadObject(url);
    
                System.out.println("Pre-Signed URL = " + url.toString());
            } catch (AmazonServiceException exception) {
                System.out.println("Caught an AmazonServiceException, " +
                        "which means your request made it " +
                        "to Amazon S3, but was rejected with an error response " +
                "for some reason.");
                System.out.println("Error Message: " + exception.getMessage());
                System.out.println("HTTP  Code: "    + exception.getStatusCode());
                System.out.println("AWS Error Code:" + exception.getErrorCode());
                System.out.println("Error Type:    " + exception.getErrorType());
                System.out.println("Request ID:    " + exception.getRequestId());
            } catch (AmazonClientException ace) {
                System.out.println("Caught an AmazonClientException, " +
                        "which means the client encountered " +
                        "an internal error while trying to communicate" +
                        " with S3, " +
                "such as not being able to access the network.");
                System.out.println("Error Message: " + ace.getMessage());
            }
        }
    
        public static void UploadObject(URL url) throws IOException
        {
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            OutputStreamWriter out = new OutputStreamWriter(
                    connection.getOutputStream());
            out.write("This text uploaded as object.");
            out.close();
            int responseCode = connection.getResponseCode();
            System.out.println("Service returned response code " + responseCode);
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题