1,引入依賴
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId> </dependency> <!--AliSms--> <dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId> </dependency>
2,代碼?
/*** 獲取阿里云OSS預簽名URL下載** @param ossPath oss存放路徑*/public String getOssPreSignatureUrl(String ossPath) {OSS ossClient = null;try {CredentialsProvider credentialsProvider = new DefaultCredentialProvider(access_key, secret_key);ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);ossClient = OSSClientBuilder.create().endpoint(endpoint).credentialsProvider(credentialsProvider).clientConfiguration(clientBuilderConfiguration).region(region).build();// 設置預簽名URL過期時間,單位為毫秒。本示例以設置過期時間為1小時為例。Date expiration = new Date(new Date().getTime() + 3600 * 1000L);// 生成以GET方法訪問的預簽名URL。本示例沒有額外請求頭,其他人可以直接通過瀏覽器訪問相關內容。return ossClient.generatePresignedUrl(bucket, ossPath, expiration).toString();} catch (Exception e) {log.error("阿里云OSS實例獲取失敗: {}", e.getMessage());throw new ServiceException("阿里云OSS預簽名URL獲取失敗!");} finally {if (ossClient != null) {ossClient.shutdown();}}}