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

com.eworkcloud.oss.OssConfiguration Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package com.eworkcloud.oss;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

@Configuration
public class OssConfiguration implements EnvironmentAware {

    private static Environment environment;
    private static String endpoint;
    private static String accessKey;
    private static String secretKey;
    private static String bucketName;
    private static String roleArn;
    private static String roleSessionName;
    private static Long durationSeconds;

    @Override
    public void setEnvironment(Environment environment) {
        OssConfiguration.environment = environment;
    }

    /**
     * 获取访问域名
     *
     * @return 访问域名
     */
    public static String getEndpoint() {
        if (null == environment || StringUtils.hasText(endpoint)) {
            return endpoint;
        }
        return environment.getProperty("oss.endpoint");
    }

    /**
     * 设置访问域名
     *
     * @param endpoint 访问域名
     */
    public static void setEndpoint(String endpoint) {
        OssConfiguration.endpoint = endpoint;
    }

    /**
     * 获取访问钥匙
     *
     * @return 访问钥匙
     */
    public static String getAccessKey() {
        if (null == environment || StringUtils.hasText(accessKey)) {
            return accessKey;
        }
        return environment.getProperty("oss.accessKey");
    }

    /**
     * 设置访问钥匙
     *
     * @param accessKey 访问钥匙
     */
    public static void setAccessKey(String accessKey) {
        OssConfiguration.accessKey = accessKey;
    }

    /**
     * 获取安全密钥
     *
     * @return 安全密钥
     */
    public static String getSecretKey() {
        if (null == environment || StringUtils.hasText(secretKey)) {
            return secretKey;
        }
        return environment.getProperty("oss.secretKey");
    }

    /**
     * 设置安全密钥
     *
     * @param secretKey 安全密钥
     */
    public static void setSecretKey(String secretKey) {
        OssConfiguration.secretKey = secretKey;
    }

    /**
     * 获取存储空间
     *
     * @return 存储空间
     */
    public static String getBucketName() {
        if (null == environment || StringUtils.hasText(bucketName)) {
            return bucketName;
        }
        return environment.getProperty("oss.bucketName");
    }

    /**
     * 设置存储空间
     *
     * @param bucketName 存储空间
     */
    public static void setBucketName(String bucketName) {
        OssConfiguration.bucketName = bucketName;
    }

    /**
     * 获取全局资源描述符
     *
     * @return 全局资源描述符
     */
    public static String getRoleArn() {
        if (null == environment || StringUtils.hasText(roleArn)) {
            return roleArn;
        }
        return environment.getProperty("oss.roleArn");
    }

    /**
     * 设置全局资源描述符
     *
     * @param roleArn 全局资源描述符
     */
    public static void setRoleArn(String roleArn) {
        OssConfiguration.roleArn = roleArn;
    }

    /**
     * 获取角色会话名称
     *
     * @return 角色会话名称
     */
    public static String getRoleSessionName() {
        if (null == environment || StringUtils.hasText(roleSessionName)) {
            return roleSessionName;
        }
        return environment.getProperty("oss.roleSessionName");
    }

    /**
     * 设置角色会话名称
     *
     * @param roleSessionName 角色会话名称
     */
    public static void setRoleSessionName(String roleSessionName) {
        OssConfiguration.roleSessionName = roleSessionName;
    }

    /**
     * 获取过期时间(秒),范围:900~3600,默认值:3600
     *
     * @return 过期时间(秒)
     */
    public static Long getDurationSeconds() {
        if (null != durationSeconds) {
            return durationSeconds;
        } else if (null == environment) {
            return 3600L;
        }
        return environment.getProperty("oss.durationSeconds", Long.class, 3600L);
    }

    /**
     * 设置过期时间(秒),范围:900~3600,默认值:3600
     *
     * @param durationSeconds 过期时间(秒)
     */
    public static void setDurationSeconds(long durationSeconds) {
        OssConfiguration.durationSeconds = durationSeconds;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy