各平台对象存储

一、阿里云对象存储

官方文档:https://help.aliyun.com/zh/oss/getting-started/getting-started-with-oss?spm=a2c4g.11186623.0.0.299a646c6nWWcW

1.引入maven

官网:https://help.aliyun.com/zh/oss/developer-reference/java-installation?spm=a2c4g.11186623.0.i12

bash 复制代码
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.15.1</version>
</dependency>

如果使用的是Java 9及以上的版本,则需要添加JAXB相关依赖。添加JAXB相关依赖示例代码如下:

bash 复制代码
<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>

2.Endpoint地址获取

3.环境变量配置

官网:https://help.aliyun.com/zh/oss/developer-reference/oss-java-configure-access-credentials?spm=a2c4g.11186623.0.i14

以windows为例,配置之后重启idea工具(我这配置之后没生效,重启之后生效)

4.工具类

java 复制代码
package com.dcqq.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;

public class AliYunOssUtil {

    // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
    private static String endpoint = "https://oss-cn-beijing.aliyuncs.com";

    // 填写Bucket名称,例如examplebucket。
    private static String bucketName = "examplebucket";

    private static String url = "https://"+bucketName+".oss-cn-beijing.aliyuncs.com/";

    /**
     * 文件上传
     * @param bytes
     * @param objectName 阿里云存储文件的位置,包括文件名,不包含存储桶bucket名称,如:upload/1.png ,注意:upload前面不能有斜杠
     * @return
     * @throws Exception
     */
    public static String upload(byte[] bytes,String objectName) throws Exception {
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

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

        ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
        return url+objectName;
    }


    public static void main(String[] args) throws Exception {
        File file=new File("C:\\图片\\1.png");
        FileInputStream fileInputStream=new FileInputStream(file);
        byte[] fileBytes = new byte[(int) file.length()];
        fileInputStream.read(fileBytes);
        upload(fileBytes,"upload/1.png");

    }
}
相关推荐
observe1012 分钟前
输入输出1
笔记
正经人_x27 分钟前
学习日记34:UNETR
学习
科技林总29 分钟前
【系统分析师】12.3 软件架构描述与表示
学习
wincheshe37 分钟前
AI Agent 开发学习 --- 框架开发实践(三)
人工智能·学习
麦麦鸡腿堡1 小时前
JavaWeb_请求参数,设置响应数据,分层解耦
java·开发语言·前端
没有bug.的程序员2 小时前
Serverless 弹性扩容引发的全线熔断:Spring Boot 启动耗时从 1s 压缩至 0.3s 的物理级绞杀
java·spring boot·kubernetes·serverless·扩容·线上
bearpping2 小时前
java进阶知识点
java·开发语言
独自破碎E2 小时前
【面试真题拆解】你知道ThreadLocal是什么吗
java·jvm·面试
kkkkatoq2 小时前
JAVA中的IO操作
java·开发语言
2501_918126913 小时前
学习所有6502写游戏存档的语句
汇编·嵌入式硬件·学习·游戏·个人开发