AliBuilder.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.modules.resource.builder;
  18. import com.aliyun.oss.ClientConfiguration;
  19. import com.aliyun.oss.OSSClient;
  20. import com.aliyun.oss.common.auth.CredentialsProvider;
  21. import com.aliyun.oss.common.auth.DefaultCredentialProvider;
  22. import lombok.SneakyThrows;
  23. import org.springblade.core.alioss.AliossTemplate;
  24. import org.springblade.core.oss.OssTemplate;
  25. import org.springblade.core.oss.props.OssProperties;
  26. import org.springblade.core.oss.rule.OssRule;
  27. import org.springblade.modules.resource.entity.Oss;
  28. /**
  29. * 阿里云存储构建类
  30. *
  31. * @author Chill
  32. */
  33. public class AliBuilder {
  34. @SneakyThrows
  35. public static OssTemplate template(Oss oss, OssRule ossRule) {
  36. // 创建配置类
  37. OssProperties ossProperties = new OssProperties();
  38. ossProperties.setEndpoint(oss.getEndpoint());
  39. ossProperties.setAccessKey(oss.getAccessKey());
  40. ossProperties.setSecretKey(oss.getSecretKey());
  41. ossProperties.setBucketName(oss.getBucketName());
  42. // 创建ClientConfiguration。ClientConfiguration是OSSClient的配置类,可配置代理、连接超时、最大连接数等参数。
  43. ClientConfiguration conf = new ClientConfiguration();
  44. // 设置OSSClient允许打开的最大HTTP连接数,默认为1024个。
  45. conf.setMaxConnections(1024);
  46. // 设置Socket层传输数据的超时时间,默认为50000毫秒。
  47. conf.setSocketTimeout(50000);
  48. // 设置建立连接的超时时间,默认为50000毫秒。
  49. conf.setConnectionTimeout(50000);
  50. // 设置从连接池中获取连接的超时时间(单位:毫秒),默认不超时。
  51. conf.setConnectionRequestTimeout(1000);
  52. // 设置连接空闲超时时间。超时则关闭连接,默认为60000毫秒。
  53. conf.setIdleConnectionTime(60000);
  54. // 设置失败请求重试次数,默认为3次。
  55. conf.setMaxErrorRetry(5);
  56. CredentialsProvider credentialsProvider = new DefaultCredentialProvider(ossProperties.getAccessKey(), ossProperties.getSecretKey());
  57. // 创建客户端
  58. OSSClient ossClient = new OSSClient(ossProperties.getEndpoint(), credentialsProvider, conf);
  59. return new AliossTemplate(ossClient, ossProperties, ossRule);
  60. }
  61. }