当前位置:网站首页>Alicloud OSS file upload system

Alicloud OSS file upload system

2022-06-12 01:49:00 Catch wind and shadow

@Test
    public void testUpload() throws FileNotFoundException {
        // Endpoint Take hangzhou for example , Other Region Please fill in according to the actual situation .
        String endpoint = "oss-cn-shanghai.aliyuncs.com";
        //  Cloud accounts AccessKey Have all API Access right , It is recommended to follow Alibaba cloud security best practices , Create and use RAM The sub account number is used for API Visit or daily operations , Please log in  https://ram.console.aliyun.com  establish .
        String accessKeyId = "LTAI4G4W1RA4JXz2QhoDwHhi";
        String accessKeySecret = "R99lmDOJumF2x43ZBKT259Qpe70Oxw";

        //  establish OSSClient example .
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        //  Upload file stream .
        InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Pictures\\timg.jpg");
        ossClient.putObject("gulimall-images", "time.jpg", inputStream);

        //  close OSSClient.
        ossClient.shutdown();
        System.out.println(" Upload successful .");
    }

Detailed usage , see : https://help.aliyun.com/knowledge_detail/108650.html

(1) Add dependency

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

(2) establish “AccessKey ID” and “AccessKeySecret”

(3) To configure key,secret and endpoint Related information

      access-key: LTAI4G4W1RA4JXz2QhoDwHhi
      secret-key: R99lmDOJumF2x43ZBKT259Qpe70Oxw
      oss:
        endpoint: oss-cn-shanghai.aliyuncs.com

(4) Inject OSSClient And upload and download files

 Insert picture description here
But it's still troublesome to do so , If the future upload tasks are assigned to gulimall-product To complete , Obviously, the coupling is high . It's best to create a new one Module To complete the file upload task .

spring:
  cloud:
    alicloud:
      access-key: LTAI4G4W1RA4JXz2QhoDwHhi
      secret-key: R99lmDOJumF2x43ZBKT259Qpe70Oxw
      oss:
        endpoint: oss-cn-shanghai.aliyuncs.com
	@Test
	public void openupload() throws FileNotFoundException{
		//  Upload file stream .
		InputStream inputStream = new FileInputStream( "C:\\Users\\liuyang\\Pictures\\cehsi\\ceshi.jpg" );
		ossClient.putObject( "gulimall-hellotest1", "ceshi.jpg", inputStream );
		//  close OSSClient.
		ossClient.shutdown();
		System.out.println( " Upload successful ." );

	}
原网站

版权声明
本文为[Catch wind and shadow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120145577420.html