阿里云盤上傳失敗是怎么回事 阿里云盤上傳失敗是什么意思



文章插圖
阿里云盤上傳失敗是怎么回事 阿里云盤上傳失敗是什么意思

文章插圖

阿里云對象存儲上傳文件系列一
購買對象存儲請查看上一篇文章 。本文將詳細講解如何從后臺上傳文件至對象存儲!
1.導入java包,我用的是maven工程,使用j2ee工程的可以自行下載jar包導入!
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.9.2</version></dependency>2.編寫連接阿里云對象代碼,打開阿里云文件上傳通道獲取OSS對象 。
public static void main(String[] args) {ClientBuilderConfiguration conf = new ClientBuilderConfiguration();// 連接空閑超時時間,超時則關閉conf.setIdleConnectionTime(1000);// 連接超時,默認15秒conf.setConnectionTimeout(15 * 1000);// socket超時,默認15秒conf.setSocketTimeout(15 * 1000);// 失敗后最大重試次數conf.setMaxErrorRetry(2);String endpoint = "***";String accessKeyId = "*****";String accessKeySecret = "*****";String bucketName = "******";OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);}3.代碼中行涉及到的秘鑰和節點,需要在各自的阿里云賬號獲取 。endpoint,在對象存儲產品控制臺,選擇自己新建的bucket桶并點擊概覽查看
accessKeyId和accessKeySecret需要把鼠標放在右上角頭像處,選擇AccessKey 管理點擊查看secret,沒有的話就根據提示創建一個吧 。
4.完整代碼,上傳我本地的一個圖片到阿里云對象存儲 。
【阿里云盤上傳失敗是怎么回事 阿里云盤上傳失敗是什么意思】public static void main(String[] args) throws FileNotFoundException {ClientBuilderConfiguration conf = new ClientBuilderConfiguration();// 連接空閑超時時間,超時則關閉conf.setIdleConnectionTime(1000);// 連接超時,默認15秒conf.setConnectionTimeout(15 * 1000);// socket超時,默認15秒conf.setSocketTimeout(15 * 1000);// 失敗后最大重試次數conf.setMaxErrorRetry(2);String endpoint = "*****";String accessKeyId = "*****";String accessKeySecret = "*****";String bucketName = "*****";OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);ObjectMetadata metadata = http://www.mnbkw.com/jxjc/190206/new ObjectMetadata();// 本地圖片路徑File file = new File("C:\Users\范成\Desktop\微信圖片_20201223105506.png");// 遠程圖片路徑String cloudPath = "fancheng/test/"+file.getName();ossClient.putObject(bucketName, cloudPath, new FileInputStream(file), metadata);ossClient.shutdown();System.out.println("文件上傳完成");}