上传图片到阿里云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);

猜你喜欢

转载自www.cnblogs.com/jylsgup/p/11565753.html