java上传阿里oss
时间: 2023-10-03 13:05:40 浏览: 132
Java下载https文件并上传阿里云oss服务器
Java上传阿里云OSS的步骤如下:
1. 首先需要开通阿里云的OSS服务,并获取到相应的AccessKey ID和AccessKey Secret。
2. 在项目的pom.xml文件中添加OSS SDK的依赖:
```xml
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
```
3. 创建OSSClient实例,并设置Endpoint、AccessKey ID和AccessKey Secret:
```java
String endpoint = "oss-cn-hangzhou.aliyuncs.com";
String accessKeyId = "your-access-key-id";
String accessKeySecret = "your-access-key-secret";
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
```
4. 使用putObject方法上传文件到OSS:
```java
InputStream inputStream = new FileInputStream("path/to/file");
ossClient.putObject("your-bucket-name", "object-key", inputStream);
```
其中,"your-bucket-name"是你的Bucket名称,"object-key"是上传后在OSS中保存的文件名。
5. 关闭OSSClient:
```java
ossClient.shutdown();
```
以上是一个简单的上传文件到阿里云OSS的示例代码。你可以根据自己的需求进行相应的修改和扩展。
#### 引用[.reference_title]
- *1* *3* [java实现上传文件到阿里云oss](https://blog.csdn.net/weixin_44740485/article/details/127766252)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [阿里云OSS使用Java上传文件](https://blog.csdn.net/weixin_46204877/article/details/124808936)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文