各平台对象存储

一、阿里云对象存储

官方文档: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");

    }
}
相关推荐
Eternity∞4 分钟前
基于Linux系统vim编译器情况下的C语言学习
linux·c语言·开发语言·学习·vim
周末吃鱼5 分钟前
Lambda作用域
java·开发语言
Wang15308 分钟前
Java异常处理
java·计算机网络
ybb_ymm9 分钟前
@Async修饰不生效
java·前端·数据库
qq_3597162318 分钟前
Openvla的原理学习
学习
nice_lcj52020 分钟前
数据结构之堆:从概念到应用全解析(附TOP-K经典问题)
java·数据结构·算法
无言(* ̄(エ) ̄)24 分钟前
进程---Linux/C语言
java·开发语言·算法
阿杰 AJie27 分钟前
Token 管理工具
java·spring
LaoZhangGong12327 分钟前
学习TCP/IP的第2步:ICMP数据包
网络·网络协议·学习·tcp/ip·以太网
中屹指纹浏览器30 分钟前
指纹浏览器网络隔离技术详解:IP绑定与泄漏防护的实现方案
经验分享·笔记