步驟1,2,3,4都是開(kāi)通阿里云oss
-
阿里云開(kāi)通對(duì)象存儲(chǔ)OSS服務(wù)
-
搞一個(gè)access-key
-
搞一個(gè)子用戶
-
創(chuàng)建個(gè)用戶, 并且給權(quán)限! 創(chuàng)建AccessKey同時(shí)記錄下來(lái), 關(guān)閉就沒(méi)了
-
項(xiàng)目中引入依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-oss</artifactId> </dependency>
-
在配置中配置
spring: cloud: alicloud: access-key: <用戶的access-key> secret-key: <用戶的secret-key> oss: endpoint: oss-cn-beijing.aliyuncs.com
-
方法中就可以用了文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-770021.html
@Resource OSSClient sooClient; public void test() { String objectName = "文件名"; String filePath= "文件路徑"; InputStream inputStream = new FileInputStream(filePath); // 創(chuàng)建PutObject請(qǐng)求。 try { InputStream inputStream = new FileInputStream(filePath); // 創(chuàng)建PutObject請(qǐng)求。 ossClient.putObject("edu-haozhancc", objectName, inputStream); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (ossClient != null) { ossClient.shutdown(); } } }
就ok了文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-770021.html
到了這里,關(guān)于項(xiàng)目使用阿里云OSS存儲(chǔ)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!