java使用阿里云oss上传文件

java使用阿里云oss上传文件

1、oss 是什么?

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

2、开通oss

我用的是阿里的oss服务
开通oss服务后,创建bucket(可以简单理解为一个数据库)
然后回到这个界面,创建安全令牌
在这里插入图片描述
进行安全令牌配置
在这里插入图片描述
将oss管理权限赋予用户组
在这里插入图片描述

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、文件储存与对象存储的区别

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳关的美好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值