oos引入项目指南

350 阅读1分钟

1.引入pom文件,注意使用最新的pom文件,GitHub上的导入是个坑

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

2.当子项目有oss引入时,common项目中的oss引入会起冲突,所以一定要注释掉子项目的oss引入,否则会无法生成文件具体报错

Error creating bean with name 'ossClient' defined in class path resource [com/alibaba/alicloud/context/oss/OssContextAutoConfiguration.class]: Unsatisfied dependency expressed through method 'ossClient' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.cloud.alicloud.oss-com.alibaba.alicloud.context.oss.OssProperties': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.alibaba.alicloud.context.oss.OssProperties] from ClassLoader [sun.misc.Launcher$AppClassLoader@14dad5dc]
......
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.cloud.alicloud.oss-com.alibaba.alicloud.context.oss.OssProperties': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.alibaba.alicloud.context.oss.OssProperties] from ClassLoader [sun.misc.Launcher$AppClassLoader@14dad5dc]
......
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.alibaba.alicloud.context.oss.OssProperties] from 

3.引入的项目必须配置,否则会报错,配置为access-key,secret-key和endpoint

spring.cloud.alicloud.access-key=LTAI5tMaAXSK5EyY5S****
spring.cloud.alicloud.secret-key=rY9LwReD7NujXPsAVVtYc8***
spring.cloud.alicloud.oss.endpoint=oss-cn-shenzhen.aliyuncs.com

4.以上是使用阿里云的oss所使用的配置,务必懂得使用阿里云的oss,官方有教程,此处不做过多注释 5.以下为测试程序

@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallProductApplicationTests {

    @Resource
    OSSClient ossClient;

    @Test
    public void testUpload() throws FileNotFoundException {
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "gulimall-rider";
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String objectName = "$W$R23NZD2`S5INVDOTUB{R.png";
        // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
        String filePath = "C:\\Users\\rider\\Desktop\\image\\$W$R23NZD2`S5INVDOTUB{R.png";

        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 (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();
            }
        }


    }