简体   繁体   中英

Is it possible to use AWS SDK for Java 2 to create a presigned URL to access AliCloud/Aliyun OSS bucket content?

I am trying to use the AWS SDK for Java 2 API to access S3. I got it working for the most part except for the presign URLs.

Per https://www.alibabacloud.com/help/doc-detail/31952.htm it looks like there's a different query parameter to build. It looks like I have to use their library and have a switch as an add on or reimplement parts of SignUtils .

Apparently you can use AWS's library to do this. You have to make sure the Presigner also has the endpointOverride configuration set. Here's the code I have and I have annotated the relevant places

@Configuration
@Slf4j
public class S3ClientConfiguration {

    // SPECIFY OVERRIDE
    @Value("${aws.s3.portfolio.endpoint:#{null}}")
    private URI endpointOverride;

    @Bean
    @ConditionalOnProperty(prefix = "aws.s3", name = "enabled", matchIfMissing = true)
    public S3AsyncClient amazonS3AsyncClient(
        final AwsCredentialsProvider credentialsProvider,
        final AwsRegionProvider regionProvider,
        final ClientOverrideConfiguration clientOverrideConfiguration) {

        final S3AsyncClientBuilder builder = S3AsyncClient.builder()
            .credentialsProvider(credentialsProvider)
            .overrideConfiguration(clientOverrideConfiguration)
            .region(regionProvider.getRegion());

        // SPECIFY OVERRIDE
        if (endpointOverride != null) {
            builder.endpointOverride(endpointOverride);
        }
        return builder
            .build();
    }

    @Bean
    @ConditionalOnProperty(prefix = "aws.s3", name = "enabled", matchIfMissing = true)
    public S3Client amazonS3Client(
        final AwsCredentialsProvider credentialsProvider,
        final AwsRegionProvider regionProvider,
        final ClientOverrideConfiguration clientOverrideConfiguration) {

        final S3ClientBuilder builder = S3Client.builder()
            .credentialsProvider(credentialsProvider)
            .overrideConfiguration(clientOverrideConfiguration)
            .region(regionProvider.getRegion());

        // SPECIFY OVERRIDE
        if (endpointOverride != null) {
            builder.endpointOverride(endpointOverride);
        }
        return builder
            .build();

    }

    @Bean
    public ClientOverrideConfiguration clientOverrideConfiguration(final HttpTracing httpTracing) {

        final var awsSdkTracing = AwsSdkTracing.create(httpTracing);
        return ClientOverrideConfiguration.builder()
            .addExecutionInterceptor(awsSdkTracing.executionInterceptor())
            .build();

    }

    @Bean
    @ConditionalOnProperty(prefix = "aws.s3", name = "enabled", matchIfMissing = true)
    public AwsCredentialsProvider credentialsProvider() {

        return DefaultCredentialsProvider.create();
    }

    @Bean
    @ConditionalOnProperty(prefix = "aws.s3", name = "enabled", matchIfMissing = true)
    public S3Presigner presigner(
        final AwsCredentialsProvider credentialsProvider,
        final AwsRegionProvider regionProvider) {

        final var builder = S3Presigner.builder()
            .credentialsProvider(credentialsProvider)
            .region(regionProvider.getRegion());

        // SPECIFY OVERRIDE
        if (endpointOverride != null) {
            builder.endpointOverride(endpointOverride);
        }
        return builder
            .build();

    }

    @Bean
    @ConditionalOnProperty(prefix = "aws.s3", name = "enabled", matchIfMissing = true)
    public AwsRegionProvider regionProvider() {

        return DefaultAwsRegionProviderChain.builder().build();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM