Java实现操作阿里云OSS云存储详解,含配置和完整代码
Posted 秋9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现操作阿里云OSS云存储详解,含配置和完整代码相关的知识,希望对你有一定的参考价值。
最近使用java代码操作阿里云存储,分享给大家
目录
2.获取accessKeyId和accessKeySecret
1.OSS云存储配置
登录阿里云后,找到OSS云存储,地址为:
2.获取accessKeyId和accessKeySecret
3.编写SpringBoot代码
3.1pom.xml文件引入sdk
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
完整pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springbootalioss</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootAliOSS</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.2编写代码
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
4.运行效果
在云存储中看一下,文件是否上传成功,例如上传成功的文件
以上是关于Java实现操作阿里云OSS云存储详解,含配置和完整代码的主要内容,如果未能解决你的问题,请参考以下文章