当前位置: 首页 > news >正文

案例-17.文件上传-阿里云OSS-入门

 一.文件上传

我们在OSS页面中找到SDK下载,点击SDK示例,找到帮助文档。

点击JAVA并安装

在安装SDK中找到在Maven项目中加入依赖项(推荐方式)

 将其中的依赖复制到pom文件中去。

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.17.4</version>
</dependency>

如果JDK版本是1.9以上,下面的也要复制到pom文件当中去。 

<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>

引入后继续找到后续参考中的“对象/文件” 

点击上传文件 

选择简单上传 

找到其中的上传文件流 

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.FileInputStream;
import java.io.InputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String objectName = "exampledir/exampleobject.txt";
        // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
        String filePath= "D:\\localpath\\examplefile.txt";
        // 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
        String region = "cn-hangzhou";
        
        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
        } 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();
            }
        }
    }
}

         // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String objectName = "exampledir/exampleobject.txt";
        // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
        String filePath= "D:\\localpath\\examplefile.txt";
        // 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
        String region = "cn-hangzhou";

endpoint:填写OSS对象存储服务的url。

bucketName:所有的数据都存储在bucket(存储空间)中,因此要指定bucketName

objectName:指定要上传的文件在bucket中的名字是什么

filePath:指定要将本地的哪一个文件上传到bucket中

将以上参数改成自己的就行,核心代码不用动。

endpoint在这里找:

二.上传测试

当我们在test测试用例中运行了上面的上传代码后,我们检查测试是否通过:

 

如果看到有上传上的文件,则证明上传的文件通过。我们可以点击文件查看上传上的文件的url,复制该url到浏览器中即可自动下载该文件。

我们可以看到该url由以下几个部分组成:

https是协议部分,web-tlias是bucket的名称,oss-cn-hangzhou.aliyuncs.com是服务所在地区,1.jpg是上传的文件名。 

相关文章:

  • git使用指南
  • 网络安全等级保护测评(等保测评):全面指南与准备要点
  • C语言基础18:函数的概述、分类、定义以及形参和实参
  • 【Leetcode 每日一题】624. 数组列表中的最大距离
  • Scrapy分布式爬虫系统
  • docker 镜像迁移到另一个服务器
  • android,flutter 混合开发,通信,传参
  • 【组态PLC】基于西门子s7-200和博图v16组态王16停车厂带烟雾报警【含PLC组态源码 M004期】
  • 一周学会Flask3 Python Web开发-http响应状态码
  • rtcwake - Linux下定时唤醒计算机
  • 【部署优化篇二】《DeepSeek服务化部署:RESTful/gRPC接口设计》
  • swupdate升级的核心机制
  • OSPF(开放路径最短优先)
  • FastGPT快速将消息发送至飞书
  • Windows桌面系统管理7:国产操作系统与Linux操作系统
  • 选项式和组合式有什么区别
  • 汇编语言与接口技术--矩阵按键
  • Go语言入门指南
  • HTTPS协议
  • 设计模式 - 单例模式
  • 做网站哪个简单点/代写文章多少钱
  • 亿码酷网站建设/目前网络推广平台
  • 怎么给网站做备案/app拉新
  • 网站建设中的时尚资讯/免费信息推广平台
  • 大连网站建设好的公司/国家市场监管总局
  • 沧州网站建设培训学校/长沙靠谱关键词优化服务