简体   繁体   中英

How to Change Amazon S3 file URL Like s3.amazonaws.com/bucket/key from bucket.s3.amazonaws.com/key in Java?

Working with AmazonS3 bucket - After uploading file to bucket, we can get uploaded file URL using below code :

String fileDownloadUrl = AmzonS3Client.getUrl(bucketName, fileName);

In Result it will give url like ie : bucket.s3.amazonaws.com/key but I want s3.amazonaws.com/bucket/key. So Can anyone help me how can I solve this in java?

By default pathstyleaccess is false so your uploaded file should be bucket.s3.amazonaws.com/key but when you explicitly add clientOptions -- pathStyleAccess to true then it will generate URL like s3.amazonaws.com/bucket/key. Please find below code snippet

S3ClientOptions clientOptions = new S3ClientOptions(); clientOptions.setPathStyleAccess(true);

And set this clientOptions to Amazons3client.

Another Solution:

Create AmazonS3Client object using AmazonS3ClientBuilder with enablePathStyleAccess() .

AmazonS3 client = AmazonS3ClientBuilder.standard()
        .enablePathStyleAccess()
        .withRegion(regionName)
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .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