InvalidRequest when getting download url from S3

0

I want to get the download URL of my file on s3 and I have set WithContentDisposition, and when I call s3Client.generatePresignedUrl it returns the download URL for me, and when I navigate to the browser with the URL, I get InvalidRequest Request specific response headers cannot be used for anonymous GET requests. The confusing part is that this worked a few weeks ago when I tested my code, so I'm not sure what may have changed on aws to start causing this.

asked a year ago557 views
2 Answers
0

Would it be possible for you to share what code you are currently using?

profile picture
EXPERT
answered a year ago
  • here is the code, to add, the only update I remember making on the bucket was making it public, I have now reverted that setting but the issue persists. This same code worked in the past. public String getPresignedDownloadUrl(String filePath)throws Exception{

        //the filePath here represents the filename. this is how I saved the file
        try {
    
            // Create a new instance of the AWS SDK S3 client
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
    
            // Generate a presigned URL for downloading the S3 object with response headers
            Date expiration = new Date(System.currentTimeMillis() + 3600000);
            ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides()
                    .withContentDisposition("attachment;filename=" + filePath);
    
            GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName,  filePath)
                    .withMethod(HttpMethod.GET)
                    .withExpiration(expiration)
                    .withResponseHeaders(responseHeaders);
    
            URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
    
            // Use the generated URL to download the S3 object
            return url.toString();
        }
        catch (Exception ex)
        {
            throw new Exception(ex);
        }
    }
    
0

I was able to resolve it by injecting the AmazonS3 service rather than creating a new instance as I did in the code I provided. I'm not sure how the SDKs are different, but the download URL was generated properly and also worked properly with the amazons3 client.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions