oss私有bucket 访问,当然公有bucket可以直接访问,私有可以获取临时访问地址
先配置一些参数 记得maven引入oss SDK
<!--Add oss--><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.10.2</version></dependency>
类以及方法定义
@Getter
@Setter
@Component
public class OssUtils {@Value("${oss.accessKeyId}")public String accessKeyId;@Value("${oss.accessKeySecret}")public String accessKeySecret;@Value("${oss.endpoint}")public String endpoint;@Value("${oss.bucketName}")public String bucketName;public OSSClient initClient() {if (null == client) {ClientConfiguration conf = new ClientConfiguration();conf.setConnectionTimeout(5000);conf.setMaxErrorRetry(10);CredentialsProvider provider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);client = new OSSClient(endpoint, provider, conf);}return client;}public String getUrl(String key) {initClient();// 设置URL过期时间为1小时。Date expiration = new Date(new Date().getTime() + 3600 * 1000);// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。URL url = client.generatePresignedUrl(bucketName, key, expiration);return url.toString();}
}