上傳圖片到阿里雲OSS


在下面的代碼之前,需要知道bucket、accessKeyId、accessKeySecret,以及域名 endpoint;

 

 
        

pom.xml:

<!-- 阿里雲存儲 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.5.0</version>
</dependency>


阿里雲配置:
private static ClientBuilderConfiguration initConf(){
// 創建ClientConfiguration。ClientConfiguration是OSSClient的配置類,可配置代理、連接超時、最大連接數等參數。
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();

// 設置OSSClient允許打開的最大HTTP連接數,默認為1024個。
conf.setMaxConnections(CONF_MAX_CONNECTIONS);
// 設置Socket層傳輸數據的超時時間,默認為50000毫秒。
conf.setSocketTimeout(CONF_SOCKET_TIMEOUT);
// 設置建立連接的超時時間,默認為50000毫秒。
conf.setConnectionTimeout(CONF_CONNECTION_TIMEOUT);
// 設置從連接池中獲取連接的超時時間(單位:毫秒),默認不超時。
conf.setConnectionRequestTimeout(CONF_CONNECTION_REQUEST_TIMEOUT);
// 設置連接空閑超時時間。超時則關閉連接,默認為60000毫秒。
conf.setIdleConnectionTime(CONF_IDLE_CONNECTION_TIME);
// 設置失敗請求重試次數,默認為3次。
conf.setMaxErrorRetry(CONF_MAX_ERROR_RETRY);
// 設置是否支持將自定義域名作為Endpoint,默認支持。
conf.setSupportCname(true);
// 設置是否開啟二級域名的訪問方式,默認不開啟。
conf.setSLDEnabled(false);
// 設置連接OSS所使用的協議(HTTP/HTTPS),默認為HTTP。
conf.setProtocol(Protocol.HTTP);

return conf;
}

初始化:
/** OSSClient實例 */
private static volatile OSS ossClient;

/** 雙重鎖單例 */
private static OSS getOssClient() {
if (ossClient == null) {
synchronized (OSS.class) {
if (ossClient == null) {
ClientBuilderConfiguration conf = initConf();
ossClient = new OSSClientBuilder().build(OSS_ENDPOINT, OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET, conf);
         //程序退出關閉ossClient
Runtime.getRuntime().addShutdownHook(new Thread(() -> ossClient.shutdown()));
            }
}
}
return ossClient;
}


上傳:
// 上傳文件
ossClient.putObject(OSS_BUCKET_NAME, objectName, input, objectMetadata);
// 獲得上傳后的url
return getImgUrl(OSS_ENDPOINT, OSS_BUCKET_NAME, objectName);

private static final String PRE_URL = "https://";
private static final String FILE_SEPARATOR = "/";
private static final String NAME_SEPARATOR = "_";
private static final String FILE_POINT = ".";

public static String getImgUrl(String endpoint, String bucketName, String objectName) {
StringBuffer sb = new StringBuffer();
sb.append(PRE_URL);
sb.append(bucketName);
sb.append(endpoint.replace(PRE_URL, FILE_POINT));
sb.append(FILE_SEPARATOR);
sb.append(objectName);
return sb.toString();
}




如果在上傳的過程中遇到:
<Error>
<Code>SecondLevelDomainForbidden</Code>
<Message>The bucket you are attempting to access must be addressed using OSS third level domain.</Message>
<RequestId>5D8340FA530E23C38F6FB369</RequestId>
<HostId>oss-cn-beijing.aliyuncs.com</HostId>
</Error>

網上的辦法是:將endpoint 設置成 bucket + 域名的方式 http://bucket-name.oss-cn-hangzhou.aliyuncs.com/object

 但是會出現一個新的問題:

<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<RequestId>5D8341C13EF51E45DBC0B383</RequestId>
<HostId>mgtv-cms-img.oss-cn-beijing.aliyuncs.com</HostId>
<OSSAccessKeyId>LTAI4FifYXAfMeWmiwpXWiKv</OSSAccessKeyId>
<SignatureProvided>QMy+ho20jDfI2KrEBaYAiBlC5aA=</SignatureProvided>



解決辦法:將下面的配置設置成false即可
// 設置是否開啟二級域名的訪問方式,默認不開啟。
conf.setSLDEnabled(false);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM