阿里云对象存储OSS简单使用

概念

基本概念

阿里云对象存储 OSS是一款海量、安全、低成本、高可靠的云存储服务,提供最高可达 99.995 % 的服务可用性。而且提供了多种存储类型,降低我们的存储成本。

Bucket

Bucket是管理文件的存储空间。可以创建多个Bucket达到隔离文件的目的

准备工作

访问该链接 控制台快速入门,按步骤执行以下操作
在这里插入图片描述

控制台操作对象存储OSS

  • 从首页进入控制台
    在这里插入图片描述
    在这里插入图片描述

  • 创建存储空间
    在这里插入图片描述

  • 直接上传文件
    在这里插入图片描述
    在这里插入图片描述

  • 新建目录后并上传文件
    在这里插入图片描述
    在这里插入图片描述

  • 下载文件
    在这里插入图片描述

  • 删除文件
    在这里插入图片描述

Java客户端操作对象存储OSS

  • 创建AccessKey
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 创建一个maven工程
  • 配置oss依赖
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.15.1</version>
</dependency>

如果是Java9环境,则需要引入以下依赖

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.3</version>
</dependency>
  • 创建Bucket
   /**
     * 测试创建Bucket
     */
    public static void testCreateBucket() {
        // Endpoint以华北2(北京)为例,其它Region请按实际情况自己指定。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
        String accessKeyId = ""; // 你的accessKeyId
        String accessKeySecret = ""; // 你的accessKeySecret
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        // 填写Bucket名称,例如examplebucket。Bucket名称在OSS范围内必须全局唯一。
        String bucketName = "";  // 你的Bucket
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
        try {
            // 创建存储空间。
            ossClient.createBucket(bucketName);
        } 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();
            }
        }
    }
  • 测试上传文件
    /**
     * 测试上传图片
     */
    public static void testUploadFile() {
        // Endpoint以华北2(北京)为例,其它Region请按实际情况自己指定。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
        String accessKeyId = ""; // 你的accessKeyId
        String accessKeySecret = ""; // 你的accessKeySecret
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "";  // 你的Bucket
        // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = "20240604/测试.png";

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

        try {
            // 上传一张图片,指定本地图片路径
            ossClient.putObject(bucketName, objectName, new File("C:\\Users\\cmbsysadmin\\Desktop\\测试图片\\测试.png"));
        } 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();
            }
        }
    }
  • 测试下载文件
   /**
     * 测试下载文件
     */
    public static void testDownFile() {
        // Endpoint以华北2(北京)为例,其它Region请按实际情况自己指定。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
        String accessKeyId = ""; // 你的accessKeyId
        String accessKeySecret = ""; // 你的accessKeySecret
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "";  // 你的Bucket
        // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = "20240604/测试.png";

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

        try {
            // 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元数据。
            OSSObject ossObject = ossClient.getObject(bucketName, objectName);
            // 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。
            // 将文件下载到指定目录
            try (InputStream content = ossObject.getObjectContent();
                 FileOutputStream fileOutputStream = new FileOutputStream("D:\\upload\\testFile\\测试.png");) {
                byte[] buffer = new byte[1024];
                int length = 0;
                while ((length = content.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 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();
            }
        }
    }
  • 测试删除文件
    /**
     * 测试删除文件
     */
    public static void testDeleteFile() {
        // Endpoint以华北2(北京)为例,其它Region请按实际情况自己指定。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
        String accessKeyId = ""; // 你的accessKeyId
        String accessKeySecret = ""; // 你的accessKeySecret
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        // 填写Bucket名称,例如examplebucket。
        String bucketName = ""; // 你的Bucket
        // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = "20240604/测试.png";

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

        try {
            // 删除文件。
            ossClient.deleteObject(bucketName, objectName);
        } 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();
            }
        }
    }

参考来源

相关推荐

  1. 阿里对象存储OSS使用笔记

    2024-06-08 22:28:03       22 阅读
  2. 阿里对象存储OSS)服务

    2024-06-08 22:28:03       42 阅读
  3. 阿里 OSS对象存储攻防

    2024-06-08 22:28:03       8 阅读
  4. SpringBoot中实现阿里OSS对象存储

    2024-06-08 22:28:03       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 22:28:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 22:28:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 22:28:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 22:28:03       20 阅读

热门阅读

  1. RK3588 Android13自定义一个按键实现长按短按

    2024-06-08 22:28:03       10 阅读
  2. Elasticsearch reindex用管道转换类型

    2024-06-08 22:28:03       11 阅读
  3. 代码随想录训练营Day30

    2024-06-08 22:28:03       7 阅读
  4. 推荐一个网安资源学习网站

    2024-06-08 22:28:03       9 阅读
  5. SCAU 数据结构 实验四 树

    2024-06-08 22:28:03       10 阅读
  6. tensorRT 实现推理加速(算子合并、量化)

    2024-06-08 22:28:03       9 阅读
  7. 注解 - @RestController

    2024-06-08 22:28:03       11 阅读
  8. python使用opencv实现火焰检测

    2024-06-08 22:28:03       7 阅读
  9. mysql order by后跟case when

    2024-06-08 22:28:03       9 阅读
  10. 生物神经网络 原理分析研读01

    2024-06-08 22:28:03       6 阅读
  11. 大模型的 Embedding 模型该如何进行微调?

    2024-06-08 22:28:03       8 阅读
  12. Sass详解

    2024-06-08 22:28:03       8 阅读