最近使用java代码操作阿里云存储,分享给大家
目录
2.获取accessKeyId和accessKeySecret
登录阿里云后,找到OSS云存储,地址为:





-
-
com.aliyun.oss -
aliyun-sdk-oss -
3.10.2 -
完整pom.xml文件如下
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.springframework.boot -
spring-boot-starter-parent -
2.7.5 -
-
-
com.example -
springbootalioss -
0.0.1-SNAPSHOT -
SpringBootAliOSS -
Demo project for Spring Boot -
-
1.8 -
-
-
-
org.springframework.boot -
spring-boot-starter -
-
-
-
org.projectlombok -
lombok -
true -
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
com.aliyun.oss -
aliyun-sdk-oss -
3.10.2 -
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
-
org.projectlombok -
lombok -
-
-
-
-
-
-
- package com.iot.util;
-
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import java.io.*;
-
-
- public class AliOSSUtil {
- // Endpoint外网访问域名,以上海为例。
- private static String endpoint = "oss-cn-shanghai.aliyuncs.com";
- // accessKeyId 和 accessKeySecret 是先前创建用户生成的
- private static String accessKeyId = "您的accessKeyId";
- private static String accessKeySecret = "您的accessKeySecret";
- private static String bucketName="您的Bucket 名称";
- public static void uploadFile(String fileName,String saveFileName) throws FileNotFoundException {
- // 创建OSSClient实例。
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
- // 上传文件流。
- InputStream inputStream = new FileInputStream(fileName);
- ossClient.putObject(bucketName, saveFileName, inputStream);
- // 关闭OSSClient。
- ossClient.shutdown();
- }
- public static void main(String[] args) throws FileNotFoundException {
- uploadFile("D:\\1\\Results.csv","app/Results.csv");
- }
-
- }
SpringBoot完整项目,见http://www.zrscsoft.com/sitepic/12151.html

在云存储中看一下,文件是否上传成功,例如上传成功的文件
