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

com.leazxl.cms.service.OssSrvice Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.leazxl.cms.service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 */
@Service
public class OssSrvice implements InitializingBean {
    // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
    // endpoint以杭州为例,其它region请按实际情况填写
    private String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private String accessKeyId = "DwlARKAEVnoh3Pf4";
    private String accessKeySecret = "aNTiUAiky9ZMhdNQ1W6TEOmooo1sBm";
    private String bucketName = "leazxl1";
    private OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    SimpleDateFormat df = new SimpleDateFormat("yyyyMM");

    /**
     * 上传到OSS文件
     *
     * @param fileName
     * @param bytes
     * @return 返回文件路径与名称
     */
    public String upload(String fileName, byte[] bytes) {
        String dir = df.format(new Date());
        // 创建OSSClient实例
        // 上传字符串
        String key = dir + "/" + fileName;
        PutObjectResult put = ossClient.putObject(bucketName, key, new ByteArrayInputStream(bytes));
        return key;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy