读取阿里OSS的txt文件

文章目录

      • 一、上代码
      • 二、Maven 配置

一、上代码

package com.xiaoqiang.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class test1Task implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(test1Task.class);

    @Override
    public void run(String... args) throws IOException {
        String accessKeyId = "XXXXX";
        String accessKeySecret = "XXXXX";
        String endpoint = "XXXXX";
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        String bucketName = "heheda-data";
        String objectName = "test.txt";
        OSSObject ossObject = ossClient.getObject(bucketName, objectName);

        InputStream inputStream = ossObject.getObjectContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuilder content = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            content.append(line);
        }

        System.out.println(content.toString());
        ossClient.shutdown();
    }
}

二、Maven 配置


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>com.aliyun.odpsgroupId>
            <artifactId>hadoop-fs-ossartifactId>
            <version>3.3.8-publicversion>
        dependency>
        
        <dependency>
            <groupId>com.taosdata.jdbcgroupId>
            <artifactId>taos-jdbcdriverartifactId>
            <version>2.0.22version>
        dependency>
    dependencies>

    <build>
        <finalName>SpringBootTestfinalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

注意:在没有 com.taosdata.jdbc 依赖时会报错:

java.lang.NoClassDefFoundError: org/apache/http/ssl/SSLContextBuilder
	at com.aliyun.oss.common.comm.DefaultServiceClient.createHttpClientConnectionManager(DefaultServiceClient.java:217) ~[aliyun-sdk-oss-2.2.1.jar:na]
	at com.aliyun.oss.common.comm.DefaultServiceClient.(DefaultServiceClient.java:78) ~[aliyun-sdk-oss-2.2.1.jar:na]
	at com.aliyun.oss.OSSClient.(OSSClient.java:268) ~[aliyun-sdk-oss-2.2.1.jar:na]
	at com.aliyun.oss.OSSClient.(OSSClient.java:193) ~[aliyun-sdk-oss-2.2.1.jar:na]
	at com.hui.demo.test1Task.run(test1Task.java:26) ~[classes/:na]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at com.hui.Application.main(Application.java:13) [classes/:na]
Caused by: java.lang.ClassNotFoundException: org.apache.http.ssl.SSLContextBuilder
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_131]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
	... 11 common frames omitted

你可能感兴趣的:(Java,OSS)