Upload images to Ali cloud OSS

Before the following code, you need to know bucket, accessKeyId, accessKeySecret, and domain endpoint;

 

 

pom.xml:

<-! Ali cloud storage -> 
<dependency>
<groupId> com.aliyun.oss </ groupId>
<artifactId> aliyun-sdk-oss </ artifactId>
<Version> 3.5.0 </ Version>
</ dependency >


Ali cloud configuration:
static ClientBuilderConfiguration initConf Private () { 
// create ClientConfiguration. ClientConfiguration is OSSClient configuration class, to configure the proxy, connection timeout parameter, the maximum number of connections and the like.
= New new ClientBuilderConfiguration the conf ClientBuilderConfiguration ();

// Set OSSClient maximum number of open connections HTTP, the default is 1024.
conf.setMaxConnections (CONF_MAX_CONNECTIONS);
// Set the timeout Socket layer to transmit data, the default is 50,000 milliseconds.
conf.setSocketTimeout (CONF_SOCKET_TIMEOUT);
// set the timeout establishing connection, the default is 50,000 milliseconds.
conf.setConnectionTimeout (CONF_CONNECTION_TIMEOUT);
// Get Connection Time set from the connection pool (Unit: ms), no default timeout.
conf.setConnectionRequestTimeout (CONF_CONNECTION_REQUEST_TIMEOUT);
// set the idle timeout. Expires, the connection is closed, the default is 60,000 milliseconds.
conf.setIdleConnectionTime (CONF_IDLE_CONNECTION_TIME);
// Set the request retries fail, the default is 3 times.
conf.setMaxErrorRetry (CONF_MAX_ERROR_RETRY);
// set supports custom domain name as Endpoint, supported by default.
conf.setSupportCname (to true);
// enable or disable access method secondary domain name, the default is not open.
conf.setSLDEnabled (to false);
// set the protocol (HTTP / HTTPS) connection OSS is used, the default is HTTP.
conf.setProtocol (Protocol.HTTP);

return the conf;
}

initialization:
/** 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;
}


Upload:
// upload files 
ossClient.putObject (OSS_BUCKET_NAME, objectName, the INPUT, objectMetadata);
// get the upload 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();
}




If you encounter in the course of uploaded:
<Error>
<Code> SecondLevelDomainForbidden </ Code>
<the Message> at The bucket you are Attempting to Access the MUST BE THIRD, Addressed a using OSS Level Domain </ the Message>.
<RequestId> 5D8340FA530E23C38F6FB369 </ RequestId >
<HostId> oss-cn-beijing.aliyuncs.com </ HostId>
</ Error>

online approach is: the endpoint is set to bucket + domain name fashion http: //bucket-name.oss-cn-hangzhou.aliyuncs .com / object

 But there will be a new problem:

<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>



Solution: The following configurations can be set to false
// enable or disable access method secondary domain name, the default is not open. 
conf.setSLDEnabled (false);

Guess you like

Origin www.cnblogs.com/jylsgup/p/11565753.html