苍穹外卖文件上传

苍穹外卖文件上传

文件上传

  • AliOssProperties.java 项目启动会自动加载

    @Component  
    @ConfigurationProperties(prefix = "sky.alioss")  
    @Data  
    public class AliOssProperties {  
    
        private String endpoint;  
        private String accessKeyId;  
        private String accessKeySecret;  
        private String bucketName;  
    
    }
    
  • 执行上传操作, 这里 AliOssUtil 不会从容器中拿 properties

      @Data  
    @AllArgsConstructor  
    @Slf4j  
    public class AliOssUtil {  
    
        private String endpoint;  
        private String accessKeyId;  
        private String accessKeySecret;  
        private String bucketName;  
    
        /**  
         * 文件上传  
         *  
         * @param bytes  
         * @param objectName  
         * @return  
         */    public String upload(byte[] bytes, String objectName) {  
    
            // 创建OSSClient实例。  
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);  
    
            try {  
                // 创建PutObject请求。  
                ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));  
            } catch (OSSException oe) {  
                System.out.println("Caught an OSSException, which means your request made it to OSS, "  
                        + "but was rejected with an error response for some reason.");  
                System.out.println("Error Message:" + oe.getErrorMessage());  
                System.out.println("Error Code:" + oe.getErrorCode());  
                System.out.println("Request ID:" + oe.getRequestId());  
                System.out.println("Host ID:" + oe.getHostId());  
            } catch (ClientException ce) {  
                System.out.println("Caught an ClientException, which means the client encountered "  
                        + "a serious internal problem while trying to communicate with OSS, "  
                        + "such as not being able to access the network.");  
                System.out.println("Error Message:" + ce.getMessage());  
            } finally {  
                if (ossClient != null) {  
                    ossClient.shutdown();  
                }  
            }  
    
            //文件访问路径规则 https://BucketName.Endpoint/ObjectName        StringBuilder stringBuilder = new StringBuilder("https://");  
            stringBuilder  
                    .append(bucketName)  
                    .append(".")  
                    .append(endpoint)  
                    .append("/")  
                    .append(objectName);  
    
            log.info("文件上传到:{}", stringBuilder.toString());  
    
            return stringBuilder.toString();  
        }  
    }
    
  • 在自动配置类中添加 Bean 使得 AliyunUtils 初始化并加载到容器中;

    @Configuration  
    public class OssConfiguration {  
    
        @Bean  
        public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){  
            return new AliOssUtil(  
                    aliOssProperties.getEndpoint(),  
                    aliOssProperties.getAccessKeyId(),  
                    aliOssProperties.getAccessKeySecret(),  
                    aliOssProperties.getBucketName()  
                    );  
        }  
    }
    
  • 文件上传 要获得 AliossUtil 对象, 需要进行初始化 @ConfigurationProperties(prefix = "sky.alioss") 使得从配置中加载配置, @Component 使得配置实体存到容器中, public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){ 从容器中获取配置实体 @BeanAliossUtil 对象 放到容器中.