내용으로 건너뛰기

Generate S3 Presigned URL with 7 Day Expiry via Lambda and Java2.x

0

I am using aws java2x library to generate presigned URL, I am able to generate URL but its getting expired in around 1 days but I need to generate for 7 days.

generating presigned URL using software.amazon.awssdk

from my analysis I found I need to paas credentials but I a not able to find a way to pass credentials in java2x.

Also, do I need to update S3 bucket policy with statement like below:

{
    "Version": "2008-10-17",
    "Id": "PolicyForCloudFrontPrivateContent",
    "Statement": [
        {
            "Sid": "Deny a presigned URL request if the signature is more than 7 days old",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::b-bucketName/*",
            "Condition": {
                "NumericGreaterThan": {
                    "s3:signatureAge": "604800000"
                }
            }
        }
    ]
}

to fix this I added above policy but not able to pass credentials.

질문됨 2년 전782회 조회

4개 답변
2
수락된 답변

You don’t need to explicitly pass credentials if you’re using a credentials provider from the environment. The AWS SDK for Java 2.x automatically uses default providers (e.g., DefaultCredentialsProvider) unless you override it. If you need to pass specific credentials, you can configure them like this:

AwsBasicCredentials awsCreds = AwsBasicCredentials.create("accessKeyId", "secretAccessKey");

S3Presigner presigner = S3Presigner.builder()
        .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
        .build();
전문가

답변함 2년 전

전문가

검토됨 2년 전

0
전문가

답변함 2년 전

0

Thanks Giovanni Lauria for the response.

Currently we are not explicitly passing credentials.

following your suggestion I should create S3Presigner like below?

S3Presigner presigner = S3Presigner.builder() .credentialsProvider(DefaultCredentialsProvider.create()) .build();

How long these credentials DefaultCredentialsProvider will be valid in Lambda? I am looking for some option for 7 days.

답변함 2년 전

0

To have an expiration of 7 days, you will need to create an IAM user and create the URL as a user (Via Access Keys) and not a role.

전문가

답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

관련 콘텐츠