1.购买服务
2.创建桶:自己设置,加密方式也自己设置
3.将桶的3个基本参数记住,后台开发要用
4.在我的凭证里面设置访问密钥,会导出csv文件,将里面的key和keyid拿出来
5.后台开发
5.0 主要依赖
<dependency> <groupId>com.huaweicloud</groupId> <artifactId>esdk-obs-java</artifactId> <version>3.19.7</version> </dependency>
5.1 将之前提到的5个参数放在yml里面
5.2 service层
@Service public class UploadService { @Value("${huawei.accessKeyId}") private String accessKeyId;// 华为云的 Access Key Id @Value("${huawei.accessKey}") private String accessKeySecret;// 华为云的 Access Key Secret @Value("${huawei.endpoint}") private String endpoint; // 华为云连接的地址节点 @Value("${huawei.obsBucketName}") private String obsBucketName; // 创建的桶的名称 @Value("${huawei.url}") private String url; // 访问OBS文件的url private ObsClient obsClient=null; /** * @Description: 文件上传 * @Param: [bucketName, fileName, localFile] * @return: com.obs.services.model.PutObjectResult * @Author: hyy * @Date: 2021/6/25 */ public String putLocalFile() { String path="D:\other\demo.jpg"; File file=new File(path); UUID uuid=UUID.randomUUID(); String originalFileName = uuid.toString().replace("-","")+"_demo.jpg"; FileInputStream fis=null; ObsClient obsClient =null; PutObjectResult putObjectResult = null; String requestId=null; try { fis = new FileInputStream(file); obsClient = getObsClient(this.obsClient); putObjectResult = obsClient.putObject(obsBucketName, originalFileName, fis); requestId = putObjectResult.getRequestId(); }catch (Exception e){ e.printStackTrace(); }finally { try { obsClient.close(); } catch (IOException e) { e.printStackTrace(); } } return requestId; } public ObsClient getObsClient(ObsClient obsClient) { if(obsClient == null) { obsClient = new ObsClient(accessKeyId, accessKeySecret, endpoint); } return obsClient; } }
5.3 controller层
@RestController public class UpLoadFile { @Autowired private UploadService uploadService; @RequestMapping("/upload") public String load(){ return uploadService.putLocalFile(); } }
6.在云服务器看结果,完美储存