阿里雲服務OSS
阿新 • • 發佈:2022-05-20
注意這裡建立Accesskey 不要將頁面關 或者記錄下access key 關閉頁面後不在顯示
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency>
如果使用的是Java 9及以上的版本,則需要新增jaxb相關依賴。新增jaxb相關依賴示例程式碼如下:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.PutObjectRequest; import java.io.File; public class Demo { public static void main(String[] args) throws Exception {// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。 String endpoint = "https://oss-cn-nanjing.aliyuncs.com"; // 阿里雲賬號AccessKey擁有所有API的訪問許可權,風險很高。強烈建議您建立並使用RAM使用者進行API訪問或日常運維,請登入RAM控制檯建立RAM使用者。 String accessKeyId = "LTAI5tN2bUbPD4JgVw1UNj7z"; String accessKeySecret = "n1vJ1vYjN24SYqtMHKlg3aPCD55Em5"; // 填寫Bucket名稱,例如examplebucket。 String bucketName = "lcc-mall"; // 填寫Object完整路徑,完整路徑中不能包含Bucket名稱,例如exampledir/exampleobject.txt。 String objectName = "1.jpg"; // 填寫本地檔案的完整路徑,例如D:\\localpath\\examplefile.txt。 // 如果未指定本地路徑,則預設從示例程式所屬專案對應本地路徑中上傳檔案。 String filePath= "C:\\Users\\Administrator\\Desktop\\1.jpg"; // 建立OSSClient例項。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); try { // 建立PutObjectRequest物件。 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath)); // 如果需要上傳時設定儲存型別和訪問許可權,請參考以下示例程式碼。 // ObjectMetadata metadata = new ObjectMetadata(); // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString()); // metadata.setObjectAcl(CannedAccessControlList.Private); // putObjectRequest.setMetadata(metadata); // 上傳檔案。 ossClient.putObject(putObjectRequest); } 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()); } finally { if (ossClient != null) { ossClient.shutdown(); System.out.println("上傳成功"); } } } }
以上上傳圖片較為繁瑣 可以選擇下面這種方式 先註釋掉剛才匯入的依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-oss</artifactId> <version>2.1.0.RELEASE</version> </dependency>
package com.msb;
import com.aliyun.oss.OSSClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
@SpringBootTest
class MallThirdPartyApplicationTests {
@Autowired
private OSSClient ossClient;
@Test
public void testUploadFile() throws FileNotFoundException {
// yourEndpoint填寫Bucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
// String endpoint = "oss-cn-nanjing.aliyuncs.com";
// 阿里雲賬號AccessKey擁有所有API的訪問許可權,風險很高。強烈建議您建立並使用RAM使用者進行API訪問或日常運維,請登入RAM控制檯建立RAM使用者。
// String accessKeyId = "LTAI5tCHuBTi5A6yT2yC8XJu";
// String accessKeySecret = "vwzcrWKP4bPVSVY24QGgQP8ZAAVsvA";
// 建立OSSClient例項。
//OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 填寫本地檔案的完整路徑。如果未指定本地路徑,則預設從示例程式所屬專案對應本地路徑中上傳檔案流。
InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.jpg");
// 依次填寫Bucket名稱(例如examplebucket)和Object完整路徑(例如exampledir/exampleobject.txt)。Object完整路徑中不能包含Bucket名稱。
ossClient.putObject("lcc-mall", "444.jpg", inputStream);
// 關閉OSSClient。
ossClient.shutdown();
System.out.println("長傳圖片成功...");
}
}