1. 程式人生 > >SpringBoot + 阿里雲OSS檔案上傳服務

SpringBoot + 阿里雲OSS檔案上傳服務

分享一個關於檔案上傳服務的專案

pom.xml檔案需要新增的依賴

<dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>2.3.0</version>
        </dependency>

application.yml檔案關於OSS的配置

ali-oss:
  endpoint: #檢視你的bucketname的endpoint地址,如:http://oss-cn-shenzhen.aliyuncs.com
  bucketName: #你的bucketName
  picLocation: #你的picLocation
  accessKeyId: #你的accessKeyId
  accessKeySecret: #你的accessKeySecret

OSS配置類

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix="ali-oss")
@Component
public class ALiOSSConfig {
    private String endpoint;
    private String bucketName;
    private String picLocation;
    private String accessKeyId;
    private String accessKeySecret;

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public String getPicLocation() {
        return picLocation;
    }

    public void setPicLocation(String picLocation) {
        this.picLocation = picLocation;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }
}

OSS上傳核心程式碼

其中,上傳時putObject方法根據bucket是公有還是私有的上傳方式有所區分

/*
* 實現ali oss檔案上傳
*/
ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());

InputStream input = file.getInputStream();
ObjectMetadata meta = new ObjectMetadata();                // 建立上傳Object的Metadata
meta.setContentType(type.getContentType());        // 設定上傳內容型別
meta.setCacheControl("no-cache");                    // 被下載時網頁的快取行為

// 以yyyy-M-d的格式獲得當前日期,用於上傳資料夾區分日期
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d");
FileId fileId = FileIdFactory.Generate(sourceId, type.getValue());

// filePath根據業務需求拼接                
String filePath = ossConfig.getPicLocation() + simpleDateFormat.format(new Date()) + "/" + fileId.getFileId() + type.getFileSuffix();
PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(), filePath, input, meta);            //建立上傳請求
ossClient.putObject(putObjectRequest);

// 私有加密url
//Date expiration = new Date(new Date().getTime() + 3600 * 1000);
//String url = ossClient.putObject(file, fileType, config.getPicLocation() + fileName + "." + fileType);

// 公有url
String url = ossConfig.getEndpoint().replaceFirst("http://", "http://" + ossConfig.getBucketName() + ".") + "/" + filePath;