1. 程式人生 > >Aliyun Oss 上傳檔案

Aliyun Oss 上傳檔案

[toc] # Aliyun OSS ## OSS 簡介 阿里雲物件儲存OSS(Object Storage Service)是阿里雲提供的海量、安全、低成本、高持久的雲端儲存服務。 ## OSS 基本概念 官方文件:[點我傳送](https://help.aliyun.com/document_detail/87728.html?spm=a2c4g.11186623.6.572.4d0b2506pMHmDW#title-3qf-u3w-nsp) - 儲存空間(Bucket) - 物件(Object) - ObjectKey - Region(地域) - Endpoint(訪問域名) - AccessKey(訪問金鑰) ## OSS 功能概述 官方文件:[點我傳送](https://help.aliyun.com/document_detail/52830.html?spm=a2c4g.11186623.6.571.5b0b2c20RPzPyA) - 建立儲存空間 - 上傳檔案 - **簡單上傳**: 包括流式上傳和檔案上傳。最大不能超過5GB。 - **表單上傳**: 最大不能超過5GB。 - **追加上傳**: 最大不能超過5GB。 - **斷點續傳上傳**: 支援併發、斷點續傳、自定義分片大小。大檔案上傳推薦使用斷點續傳。最大不能超過48.8TB。 - **分片上傳**: 當檔案較大時,可以使用分片上傳,最大不能超過48.8TB。 - 下載檔案 ## OSS 使用 使用步驟: ![p203792.jpg](https://gitee.com/HOSystem/learning-notes/raw/a89e154650c39de64f6c4f9d4205b29ba025e0f6/Java/%E9%98%BF%E9%87%8C%E4%BA%91%E4%BA%A7%E5%93%81/%E9%98%BF%E9%87%8C%E4%BA%91OSS/images/p203792.jpg) ### 建立儲存空間Bucket 官方文件:[點我傳送](https://help.aliyun.com/document_detail/31885.html?spm=a2c4g.11186623.2.10.583e5bacvXSshG) ![40F06D90-71F6-4e46-AC2F-4B5E8C718676.png](https://gitee.com/HOSystem/learning-notes/raw/a89e154650c39de64f6c4f9d4205b29ba025e0f6/Java/%E9%98%BF%E9%87%8C%E4%BA%91%E4%BA%A7%E5%93%81/%E9%98%BF%E9%87%8C%E4%BA%91OSS/images/40F06D90-71F6-4e46-AC2F-4B5E8C718676.png) ### 建立子目錄 建立目錄,更好的區分圖片存放的位置。也可以直接放在建立的Bucket上。 ![2BADC1E0-B481-4880-912C-B007F4C462E4.png](https://gitee.com/HOSystem/learning-notes/raw/a89e154650c39de64f6c4f9d4205b29ba025e0f6/Java/%E9%98%BF%E9%87%8C%E4%BA%91%E4%BA%A7%E5%93%81/%E9%98%BF%E9%87%8C%E4%BA%91OSS/images/2BADC1E0-B481-4880-912C-B007F4C462E4.png) ### Java編碼 - controller 層 ```java /** * 圖片上傳 Controller */ @RestController @RequestMapping("/images") public class ImagesController { @PostMapping("/upload") public void upload(@RequestParam("imageString") String imageString) throws UnsupportedEncodingException { String list = seafoodService.upload(imageString); } } ``` - Service層 interface Service ```java /** * 上傳圖片 service介面 */ public interface ISeafoodService { /** * 圖片上傳 * @author Hosystem * @create 2021-2-24 * @desc 圖片上傳 **/ String upload(String imageString) throws UnsupportedEncodingException; } ``` ServiceImpl ```java /** * 上傳圖片 service */ @Service public class SeafoodServiceImpl implements ISeafoodService { /** * 阿里雲 OSS 配置抽取yml */ @Autowired AliPayClientProperties aliPayClientProperties; /** * 圖片上傳 * @author HYH * @create 2021-2-24 * @desc 圖片上傳 將圖片轉換成base64編碼然後傳進來 * * 參考文件:https://help.aliyun.com/document_detail/84781.html?spm=a2c4g.11186623.2.10.e50c46a19Q1Q52 **/ @Override public String upload(String imageString) { // 建立OSSClient例項。 OSS ossClient = new OSSClientBuilder().build(appProps.getAliOssClientProperties().getEndpoint(), appProps.getAliOssClientProperties().getAccessKeyId(), appProps.getAliOssClientProperties().getAccessKeySecret()); // 建立一個Base64 物件 Base64 base64 = new Base64(); // 將傳進來的base64編碼 進行解碼 byte[] imageByte = base64.decode(imageString.toString()); // 建立位元組流 將base64解碼後進行位元組流轉換 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageByte); // 上傳路徑 bucket目錄名 + 圖片的名稱 使用UUID隨機生成 // OSS管理控制檯將所有檔名以正斜線(/)結尾的檔案顯示為資料夾; // 如:abc/efg/123.jpg這個路徑的檔案,在OSS管理控制檯上看起來就是123.jgp存放在abc資料夾下的efg子資料夾中。 // 若想上傳到bucket 則 String url = UUID.randomUUID().toString() + "." + "jpg"; String url = appProps.getAliOssClientProperties().getDirectoryPath() + UUID.randomUUID().toString() + "." + "jpg"; // 檔案元資訊(Object Meta):包括HTTP header和自定義元資訊 // 建立上傳檔案的元資訊,可以通過檔案元資訊設定HTTP header。 // 參考文件:https://help.aliyun.com/document_detail/84840.html ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("image/jpg"); // 上傳檔案 // 格式:ossClient.putObject("", "", new ByteArrayInputStream(content.getBytes()), meta); ossClient.putObject(appProps.getAliOssClientProperties().getBucketName(), url, byteArrayInputStream, objectMetadata); // 關閉OSSClient ossClient.shutdown(); // 返回上傳圖片的OSS路徑 return appProps.getAliOssClientProperties().getUrl() + url; } } ``` - yml配置抽取 ```java @Data @Component @Accessors(chain = true) @ConfigurationProperties(prefix = "aliOssClientProperties") public class AliOssClientProperties { /** * 阿里雲OSS endpoint */ private String endpoint; /** * 阿里雲OSS AccessKey */ private String accessKeyId; /** * 阿里雲OSS AccessKeySecret */ private String accessKeySecret; /** * 阿里雲OSS bucketName */ private String bucketName; /** * 阿里雲OSS url */ private String url; /** * 阿里雲OSS directoryPath 目錄路徑 */ private String directoryPath; } ``` - yml配置 ```yml # http://./+ aliOssClientProperties: endpoint: oss-cn-shenzhen.aliyuncs.com accessKeyId: #accessKey accessKeySecret: #accessKeySecret bucketName: #bucketName url: https://. directoryPath: images/ ``` ### 測試 這裡我通過swagger進行測試,也可以通過postman進行測試,直接定義常量imageString 當作引數使用。 ![E0227E46-3158-483e-ABAE-11572DDCEB6E.png](https://gitee.com/HOSystem/learning-notes/raw/a89e154650c39de64f6c4f9d4205b29ba025e0f6/Java/%E9%98%BF%E9%87%8C%E4%BA%91%E4%BA%A7%E5%93%81/%E9%98%BF%E9%87%8C%E4%BA%91OSS/images/E0227E46-3158-483e-ABAE-11572DDCEB6E.png) 參考文件1:[點我傳送](https://help.aliyun.com/document_detail/32011.html?spm=a2c4g.11186623.2.8.730f46a1Eq6LZP) 參考文件2:[點我傳送](https://help.aliyun.com/document_detail/39630.html) 參考文件3:[點我傳送](https://help.aliyun.com/document_detail/848