1

I’m trying to get some info from my config.properties file which contains all my credential information something like this below:

cloud.aws.credentials.accessKey=Hello
cloud.aws.credentials.secretKey=Hellobebe
cloud.aws.s3.bucket=hellome
could.aws.region.static=ap-northeast-2
cloud.stack.auto=false

and in java file, I’m trying access it by using @Value but i can't get the value. here's my code for S3Service.java

package com.aplusinternational.goTrip.service;

import java.io.IOException;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;

import lombok.NoArgsConstructor;


@Service
@NoArgsConstructor
public class S3Service {
    private AmazonS3 s3Client;

    @Value("${cloud.aws.credentials.accessKey}")
    private String accessKey;

    @Value("${cloud.aws.credentials.secretKey}")
    private String secretKey;

    @Value("${cloud.aws.s3.bucket}")
    private String bucket;

    @Value("${cloud.aws.region.static}")
    private String region;

    @PostConstruct
    public void setS3Client() {
        AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
        s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(this.region)
                .build();
    }

    public String upload(MultipartFile multipartFile, String dirName) throws IOException {
        String fileName = dirName + "/" + multipartFile.getOriginalFilename();
        String uploadImageUrl = putS3(multipartFile, fileName);
        //removeNewFile(multipartFile);
        return uploadImageUrl;
    }

    private String putS3(MultipartFile multipartFile, String fileName) throws IOException {
        System.out.println("file is in!: "+ fileName+" putS3");
        System.out.println(this.bucket);
        s3Client.putObject(new PutObjectRequest(bucket, fileName, multipartFile.getInputStream(), null).withCannedAcl(CannedAccessControlList.PublicRead));
        return s3Client.getUrl(bucket, fileName).toString();
    }

}

and when i run it, it will give me the result of ${cloud.aws.s3.bucket} itself. error's would be : enter image description here

i'm spending thousand of hours for this. Please help me!

ps. i was able to get db info as from the same .properties file.

Doyeon
  • 7
  • 7

1 Answers1

0

@PropertySource(value = {"classpath:config.properties",}, encoding = "utf-8") try to add this annotation.

mujx
  • 41
  • 1
  • is my classpath correct? "classpath:src/main/webapp/config/config.properties" and my service java file is located at src/main/java/com/aplusinternational/goTrip/service/S3Service.java – Doyeon Feb 25 '21 at 04:19
  • classpath:config/config.properties is worked,and I have marked the webapp as a Resources ROOT first . – mujx Feb 25 '21 at 05:37
  • @Doyeon usually properties files are meant to be under "src/main/resources". If your project has the properties file elsewhere, I guess you will have to load the file manually on application start up so it registers the properties in the file. See: https://stackoverflow.com/a/16313425/6200354 – atish.s Feb 25 '21 at 10:43
  • thanks! I use the path as classpath:config/config.properties but it says it can't find the destination.. do you know why am i getting this error? for the root context, it worked, but in java file under pakages it won't work. – Doyeon Mar 03 '21 at 07:58