java使用阿里云oss上传文件

1、oss 是什么?

OSS是阿里云对象存储服务(Object Storage Service)的一个简称,它是阿里云提供的海量、安全、低成本、高可靠的云存储服务。
即开即用、无限大空间的存储集群。相较传统建服务器存储而言,OSS在可靠性、安全性、成本和数据处理能力方面都有着突出的优势。使用OSS,您可以通过网络随时存储和调用包括文本、图片和视频等在内的各种非结构化数据文件。
OSS将数据文件以对象/文件(Object)的形式上传到存储空间(Bucket)中。OSS提供的是一个Key-Value键值对形式的对象存储服务。用户可以根据Object的名称(Key)唯一地址获取该Object的内容。

2、开通oss

我用的是阿里的oss服务

开通oss服务后,创建bucket(可以简单理解为一个数据库)

然后回到这个界面,创建安全令牌

java使用阿里云oss上传文件_java


进行安全令牌配置

java使用阿里云oss上传文件_对象存储_02


将oss管理权限赋予用户组

java使用阿里云oss上传文件_服务器_03

3、使用代码操作oss

3、1 引入阿里oss的pom依赖

<!-- 阿里云oss -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

3、2创建bucket

//地域节点(Endpoint)的配置
        String endpoint = "";

        //RAM子用户的key值
        String accessKeyId = "";
        String accessKeySecret = "";

        //声明OSS云存储的Bucket名称。
        String bucketName = "";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建存储空间。
        ossClient.createBucket(bucketName);

        // 关闭OSSClient。
        ossClient.shutdown();

3、2查看访问权限

//地域节点(Endpoint)的配置
    String endpoint = "";

    //RAM子用户的key值
    String accessKeyId = "";
    String accessKeySecret = "";

    //声明OSS云存储的Bucket名称。
    String bucketName = "";

    // 创建OSSClient实例。
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

    // 获取存储空间的访问权限。
    AccessControlList bucketAcl = ossClient.getBucketAcl(bucketName);
    System.out.println("访问权限"+bucketAcl);

    // 关闭OSSClient。
    ossClient.shutdown();

3、3上传文件

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import java.io.ByteArrayInputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "";
        // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = "exampledir/exampleobject.txt";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            String content = "Hello OSS";
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

3、4上传图片

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import java.io.ByteArrayInputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "";
        // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = "exampledir/"+ LocalDateTime.now()+".jpg";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            String content = "Hello OSS";
              FileInputStream fileInputStream = new FileInputStream("C:\\Users\\xla\\Desktop\\1681565248028.jpg");
            ossClient.putObject(bucketName, objectName, fileInputStream);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

3、5更多demo参考

4、文件储存与对象存储的区别

java使用阿里云oss上传文件_java_04