阿里云的OSS云存储 上传文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/diypp2012/article/details/81410148

第一次使用阿里云的云存储,mark一下
1.首先,你需要申请购买到AccessKey 和 AccessKeySecret。
因权限问题,建议开通单独的RAM帐号
在云服务中开启新建一个bucket空间,用于存储上传的文件存储位置
2.编码工作开始:
1)maven中引入jar包
pom文件配置

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

2)设置oss的相关参数在properties文件中,我的文件是oss.properties

Endpoint=your endPoint
AccessKey=your accessKey
AccessKeySecret=your accessKeySecret
BucketName=your bucketName

3)java中代码上传及获取可访问的url地址

OSSClient ossClient = null;
String url = "";
String fileName = "your file name";
//上传阿里云
try {
    // 创建上传Object的Metadata
    ObjectMetadata metadata = new ObjectMetadata();
    // 指定该Object被下载时的网页的缓存行为
    metadata.setCacheControl("no-cache");
    // 指定该Object下设置Header
    metadata.setHeader("Pragma", "no-cache");
    // 指定该Object被下载时的内容编码格式
    metadata.setContentEncoding("utf-8");
    // 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
    // 如果没有扩展名则填默认值application/octet-stream,apk文件的contentType
    metadata.setContentType("application/octet-stream");
    //读取配置文件
     OSSConfigure ossCon = new OSSConfigure("/properties/oss.properties");
    //创建ossClient
    ossClient = new OSSClient(ossCon.getEndpoint(), ossCon.getAccessKeyId(), ossCon.getAccessKeySecret());
    //判断bucket是否存在
    if(ossClient.doesBucketExist(ossCon.getBucketName())) {
        //已存在,不用处理
    }else {
        // 创建存储空间 
        ossClient.createBucket(ossCon.getBucketName());
    }
    // 文件转换为输入流
    CommonsMultipartFile cf = (CommonsMultipartFile) file;
    DiskFileItem fi = (DiskFileItem) cf.getFileItem();
    InputStream fileContent;
    fileContent = fi.getInputStream();
    //oss 文件上传
    ossClient.putObject(ossCon.getBucketName(), fileName, fileContent, metadata);
    //获取其url
    Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
    url = ossClient.generatePresignedUrl(ossCon.getBucketName(), fileName, expiration).toString();
    // 关闭client
    ossClient.shutdown();
}catch(OSSException e) {
    e.printStackTrace();
}catch(ClientException e) {
    e.printStackTrace();
}catch (IOException e) {
    e.printStackTrace();
} finally { //保证是否存在异常,ossClint都要关闭,不然会在console中持续提示ossClient未关闭
    if(ossClient != null) {
        ossClient.shutdown();
    }
}

4)最后,附上自己写的OSSConfigure类

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class OSSConfigure {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    public OSSConfigure() {
    }
    //构造函数-传入properties文件路径
    public OSSConfigure(String storageConfName) throws IOException {
        Properties prop = new Properties();
        InputStream is = super.getClass().getClassLoader().getResourceAsStream(storageConfName);
        prop.load(is);
        endpoint = prop.getProperty("Endpoint").trim();
        accessKeyId = prop.getProperty("AccessKey").trim();
        accessKeySecret = prop.getProperty("AccessKeySecret").trim();
        bucketName = prop.getProperty("BucketName").trim();
    }
    //构造函数-传入oss默认配置的值
    public OSSConfigure(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
        this.endpoint = endpoint;
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        this.bucketName = bucketName;
    }
    public String getEndpoint() {
        return endpoint;
    }
    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }
    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;
    }
    public String getBucketName() {
        return bucketName;
    }
    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }
}

以上。

猜你喜欢

转载自blog.csdn.net/diypp2012/article/details/81410148