阿里云对象存储OSS服务——上传/删除/获取图片

阿里云对象存储OSS服务——上传/删除/获取图片

1. 准备工作

  • 开通对象存储 OSS服务
  • 创建AccessKey(使用阿里云OSS SDK时需要使用到)
  • 创建Bucket(输入唯一的Bucket名字,选择合适的区域,其他选项默认)
  • 创建Spring Boot项目
  • 推荐使用Postman测试后端API,Postman的下载及使用参考 https://blog.csdn.net/fxbin123/article/details/80428216
  • 注意:使用Postman测试上传图片功能时,需要将请求体(Body)的类型设为form-data

2. 上传图片

    // 未对异常进行处理
    @PostMapping(value = "/image")
    public void uploadImage(MultipartFile file) {
    
    
        // MultipartFile代表HTML中form data方式上传的文件
        // EndPoint以上海为例
        String endPoint = "oss-cn-shanghai.aliyuncs.com";
        // 阿里云API秘钥
        String accessKeyId = "<your access key id>";
        String accessKeySecret = "<your access key secret>";
        String bucketName = "<bucket name>";
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        client.putObject(bucketName, imageName, new ByteArrayInputStream(file.getBytes()));
        client.shutdown();
    }

3. 删除图片

    // 未对异常进行处理
    @DeleteMapping(value = "/image/{imageName}")
    public void deleteImage(@PathVariable String imageName) {
    
    
        // EndPoint以上海为例
        String endPoint = "oss-cn-shanghai.aliyuncs.com";
        // 阿里云API秘钥
        String accessKeyId = "<your access key id>";
        String accessKeySecret = "<your access key secret>";
        String bucketName = "<bucket name>";
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        client.deleteObject(bucketName, imageName);
        client.shutdown();
    }

4. 获取图片

    // 未对所有异常进行处理
    @GetMapping(value = "/image/{imageName}")
    public byte[] getImage(@PathVariable String imageName) {
    
    
        // EndPoint以上海为例
        String endPoint = "oss-cn-shanghai.aliyuncs.com";
        // 阿里云API秘钥
        String accessKeyId = "<your access key id>";
        String accessKeySecret = "<your access key secret>";
        String bucketName = "<bucket name>";
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        OSSObject image = client.getObject(bucketName, imageName);
        InputStream content = image.getObjectContent();
        if (content != null) {
    
    
            try {
    
    
                // 设置一个足够大的buffer用于存储图片的比特数据
                int length = 1920 * 1260 * 3;
                byte[] buf = new byte[length];
                int size = 0;
                int temp;
                while ((temp = content.read()) != -1) {
    
    
                    buf[size] = (byte) temp;
                    size++;
                }
                content.close();
                // 对缓冲区进行裁剪后,将图片以字节数组的形式返回
                return Arrays.copyOf(buf, size);
            } catch (IOException e) {
    
    
                System.out.println("exception");
                return null;
            }
        }
        client.shutdown();
        return null;
    }

5. 测试获取图片功能

将内容拷贝后粘贴到Spring Boot项目的src/main/resources/static目录的某个文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Get Image</title>
</head>
<body style="margin: 0;">
<!-- 测试获取OSS图片功能是否正确 -->
<img src="http://localhost:80/image/<imageName>"/>
</body>
</html>

阿里云对象存储 OSS官方文档: https://help.aliyun.com/product/31815.html?spm=5176.8466032.0.0.7e8714505JnJY6

猜你喜欢

转载自blog.csdn.net/a16302010048/article/details/94355850