阿里云OSS依赖无法导入的问题

版本背景:springboot:2.4.12,spring-cloud:2020.0.1

在使用阿里云对象存储OSS服务时候,根据官方参考文档:aliyun-spring-boot/aliyun-spring-boot-samples/aliyun-oss-spring-boot-sample at master · alibaba/aliyun-spring-boot · GitHub

出现依赖无法下载的问题,无maven配置问题,编译报如下问题

 添加版本后,无编译错误,但是运行仍会报错,如下:

解决:更换依赖

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

 Java代码

 @Autowired
    private OSSClient ossClient;

    @Test
    void testUpload2() throws ClientException {
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "xw-bucket-test";
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String objectName = "xw/小王Test.txt";
        // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
        String filePath = "D:\\小王Test.txt";

        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName, inputStream);
        } 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 (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        System.out.println("upload seccess!");
    }

 application.yml

apring:
    cloud:
        alicloud:
          oss:
            endpoint: oss-cn-shanghai.aliyuncs.com
          access-key: LTAI5tMk2tq7yqzNPxTwRLks
          secret-key: ByS15Y7Xm2kaBBKYrIkrpYDT1sbsCM

测试结果

猜你喜欢

转载自blog.csdn.net/f746262041/article/details/128171723