阿里云oss文件处理工具类
生活随笔
收集整理的這篇文章主要介紹了
阿里云oss文件处理工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
maven依賴
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.0</version></dependency>工具類
package com.demo.util;import java.io.File; import java.io.InputStream; import java.net.URL; import java.util.Calendar;import cn.hutool.core.util.ObjectUtil; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.CannedAccessControlList; import com.aliyun.oss.model.CreateBucketRequest; import com.aliyun.oss.model.GetObjectRequest; import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.ObjectMetadata; import com.aliyun.oss.model.PutObjectRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.multipart.MultipartFile;/*** @author matao* @version OssUtil.java, v 0.1 2022年10月27日 16:51 matao*/ //@Component @Slf4j public class OssUtil {@Value("${metadata.aliyun.endPoint}")private String endpoint;@Value("${metadata.aliyun.accessKey.default.id}")private String accessId;@Value("${metadata.aliyun.accessKey.default.secret}")private String accessSecret;@Value("${metadata.aliyun.oss.nameSpace}")private String ossNameSpace;private OSS client;/*** 上傳文件到oss** @param file 文件* @param filePath 文檔存放路徑* @return 文件訪問地址*/public String uploadFile(MultipartFile file, String filePath) {try {buildOssClient();InputStream inputStream = file.getInputStream();// 創建PutObjectRequest對象PutObjectRequest request = new PutObjectRequest(ossNameSpace, filePath, inputStream);// 文件上傳時設置參數ObjectMetadata metadata = new ObjectMetadata();metadata.setContentLength(inputStream.available());metadata.setCacheControl("no-cache");metadata.setHeader("Pragma", "no-cache");request.setMetadata(metadata);client.putObject(request);// 設置URL過期時間為10年Calendar calendar = Calendar.getInstance();calendar.add(Calendar.YEAR, 10);URL url = client.generatePresignedUrl(ossNameSpace, filePath, calendar.getTime());return url.toString();} catch (Exception e) {return null;} finally {closeOssClient();}}/*** 下載文件流** @param filePath oss文件路徑* @return 文件流*/public InputStream downloadFile(String filePath) {InputStream inputStream;try {buildOssClient();// 判斷文件是否存在if (!client.doesObjectExist(ossNameSpace, filePath)) {return null;}OSSObject object = client.getObject(ossNameSpace, filePath);if (ObjectUtil.isNull(object)) {return null;}inputStream = object.getObjectContent();} catch (Exception e) {return null;}return inputStream;}/*** 下載文件到本地** @param remoteFilePath oss文件路徑* @param localFilePath 要存放的文件本地路徑* @param bucketName 文件目錄*/public void downloadFile(String remoteFilePath, String localFilePath, String bucketName) {try {buildOssClient();// 判斷文件是否存在if (!client.doesObjectExist(bucketName, remoteFilePath)) {return;}// 讀取文件到本地GetObjectRequest request = new GetObjectRequest(bucketName, remoteFilePath);client.getObject(request, new File(localFilePath));} catch (Exception ignored) {} finally {closeOssClient();}}/*** 創建bucket** @param bucketName 文件目錄*/private void createBucket(String bucketName) {if (!client.doesBucketExist(bucketName)) {CreateBucketRequest bucketRequest = new CreateBucketRequest(bucketName);bucketRequest.setCannedACL(CannedAccessControlList.PublicRead);this.client.createBucket(bucketRequest);}}/*** 構建client*/private void buildOssClient() {this.client = new OSSClientBuilder().build(endpoint, accessId, accessSecret);}/*** 關閉client*/public void closeOssClient() {if (!ObjectUtil.isNull(client)) {this.client.shutdown();}} }總結
以上是生活随笔為你收集整理的阿里云oss文件处理工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 声网的混沌工程实践
- 下一篇: 虚拟机web服务器配置ppt,在Linu