All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.oss.qiniu.QiniuConfiguration Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.oss.qiniu;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.taotao.boot.common.constant.StarterName;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.oss.common.condition.ConditionalOnOssEnabled;
import com.taotao.boot.oss.common.propeties.OssProperties;
import com.taotao.boot.oss.common.service.UploadFileService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @author shuigedeng
 * @version 2022.03
 * @since 2020/10/26 10:28
 */
@AutoConfiguration
@ConditionalOnOssEnabled
@EnableConfigurationProperties({QiniuProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "qiniu")
public class QiniuConfiguration implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        LogUtils.started(QiniuConfiguration.class, StarterName.OSS_QINIU_STARTER);
    }

    private final QiniuProperties properties;

    public QiniuConfiguration(QiniuProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public Region zone() {
        return switch (properties.getZone()) {
            case "z0" -> Region.createWithRegionId("z0");
            case "z1" -> Region.createWithRegionId("z1");
            case "z2" -> Region.createWithRegionId("z2");
            default -> Region.autoRegion();
        };
    }

    /** 华南机房 */
    @Bean
    public com.qiniu.storage.Configuration config( Region region) {
        return new com.qiniu.storage.Configuration(region);
    }

    /**
     * 构建一个七牛上传工具实例
     *
     * @since 2020/10/26 11:36
     */
    @Bean
    public UploadManager uploadManager( com.qiniu.storage.Configuration config) {
        return new UploadManager(config);
    }

    /**
     * 认证信息实例
     *
     * @since 2020/10/26 11:36
     */
    @Bean
    public Auth auth() {
        return Auth.create(properties.getAccessKey(), properties.getSecretKey());
    }

    /**
     * 构建七牛空间管理实例
     *
     * @since 2020/10/26 11:36
     */
    @Bean
    public BucketManager bucketManager( com.qiniu.storage.Configuration config,  Auth auth) {
        return new BucketManager(auth, config);
    }

    @Bean
    @ConditionalOnMissingBean
    public UploadFileService fileUpload(UploadManager uploadManager, BucketManager bucketManager, Auth auth) {
        return new QiniuUploadFileServiceImpl(uploadManager, bucketManager, auth, properties);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy