登录阿里云使用
免费的自己做测试的时候用,
首先创建一个桶,用来存贮资源文件
OS配置类:
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import com.atguigu.scw.oss.OssTemplate;
@SpringBootConfiguration
public class OssConfig {
@Bean
@ConfigurationProperties("oss")
public OssTemplate geTemplate() {
return new OssTemplate();
}
}
上传文件的模板类
package com.atguigu.scw.oss;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.stereotype.Component;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import lombok.Data;
@Data
//@Component
public class OssTemplate {
private String endpoint ;
private String accessKeyId ;
private String accessKeySecret;
private String bucket;
public String upload(String fileName,InputStream inputStream){
// Endpoint以杭州为例,其它Region请按实际情况填写。
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
ossClient.putObject(bucket,"pic/"+fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
String fileUrl="https://"+bucket+"."+endpoint+"/pic/"+fileName;
return fileUrl;
}
//这个不对,我还没弄成呢,哈哈哈真彩
public void downLoad(String fileName) throws IOException{
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
File file = new File("D:\\360downloads\\wpcache");
ossClient.getObject(new GetObjectRequest(bucket, fileName), file);
// 关闭OSSClient。
ossClient.shutdown();
// 关闭OSSClient。;
ossClient.shutdown();
}
}
oss配置文件信息:(配置信息用你自己的呦,我修改了参数,不能直接使用呦,自己去官网看自己的参数信息)
#logging.file=C:\work\scw.log
#logging.level.root=debug
#logging.level.com.atguigu.scw=debug
#logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
#logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
sms.host =http://dingxin.market.alicloudapi.com
sms.path =/dx/sendSms
sms.method =POST
sms.appcode =39ce06890b3d4517b33a42931e7e1d
oss.endpoint = oss-cn-beijing.aliyuncs.com
oss.accessKeyId = LTAI4FqbTiDNFadqrCWgzd9
oss.accessKeySecret = NMLfQq6JBeLuU4I8h38JqPMUzZVus
oss.bucket = wuwang
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=1000MB
可以去查看官方的文档